Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep SSH session alive [closed]

I use ssh -p8520 username@remote_host to login remote server.

Issue:

It is always connected and works properly when I am in the work place. Unfortunately, terminal freezes in 10 - 15 minutes after I connected with the remote server from home.

There's no error/timeout report on the console but the cursor cannot move any more.

When enter w to check the login users, some zombies login users are there, and I have to kill them manually.

This is quite annoying. Can anyone help me?

like image 693
Haifeng Zhang Avatar asked Aug 01 '14 16:08

Haifeng Zhang


People also ask

How do you keep a terminal session alive?

When you log in to the server, the terminal session won't automatically close. Instead, the configuration file will keep sending the alive signal after the specific interval set in the configuration file to keep the terminal session alive.


2 Answers

The ssh daemon (sshd), which runs server-side, closes the connection from the server-side if the client goes silent (i.e., does not send information). To prevent connection loss, instruct the ssh client to send a sign-of-life signal to the server once in a while.

The configuration for this is in the file $HOME/.ssh/config, create the file if it does not exist (the config file must not be world-readable, so run chmod 600 ~/.ssh/config after creating the file). To send the signal every e.g. four minutes (240 seconds) to the remote host, put the following in that configuration file:

Host remotehost     HostName remotehost.com     ServerAliveInterval 240 

To enable sending a keep-alive signal for all hosts, place the following contents in the configuration file:

Host *     ServerAliveInterval 240 
like image 80
rockymonkey555 Avatar answered Sep 23 '22 14:09

rockymonkey555


I wanted a one-time solution:

ssh -o ServerAliveInterval=60 [email protected] 

Stored it in an alias:

alias sshprod='ssh -v -o ServerAliveInterval=60 [email protected]' 

Now can connect like this:

me@MyMachine:~$ sshprod 
like image 38
Ryan Avatar answered Sep 24 '22 14:09

Ryan