SSHTips

From SGEES Wiki
Revision as of 00:52, 7 February 2020 by Admin (talk | contribs) (Created page with "=== Paswordless Login to Servers === === Keep Connection Active === To save network resources ITS drops any not active connection by timeout (20 minutes). If you found this a...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Paswordless Login to Servers

Keep Connection Active

To save network resources ITS drops any not active connection by timeout (20 minutes). If you found this annoying, you might add the following lines to the top of your SSH configuration file (~/.ssh/config) :

 Host *
   ServerAliveInterval 100

Aliases

SSH config file can handle abbreviations and shortcuts for preferred hosts. For example:

Host 017
  HostName sgees017.geo.vuw.ac.nz
  User doejohn 

Instead of typing ssh doejohn@sgees017.geo.vuw.ac.nz you'll need only run ssh 017


Different Environment for Different Servers

You might find useful to set some environment variables depending on what server you've logged in. Just add following lines to the end of your ~/.bashrc (and edit them according to your needs):

if [ $HOSTNAME == "co501pc02.geo.vuw.ac.nz" ]; then
    # We're logged in locally to our desktop
    export PATH=$HOME/bin:$PATH
    . ~/.bash_completion
elif [ $HOSTNAME == "sgees017.geo.vuw.ac.nz" ]; then
    echo "We are on SGEES017"
    export GMT_HOME=/usr/local/gmt
else
    echo "Logged in to: $HOSTNAME"
fi

If connection is not reliable

When you connect to the server or remote computer via SSH and start some task, you probably want to be sure that your task will not be killed because of network or power failure somewhere between your computer and server. Fortunately there are several tools which could help you to operate with your remote running programs

Generic task manipulation

Ctrl+Z - active job suspend
jobs - jobs list
fg %job_number - put job to foreground (e.g.: fg %3)
bg %job_number - put job to background
disown %job_number - detach background process from terminal 
Examples of generic job managing

Variant 1: you have started a program over ssh and realized that you need to disconnect, but keep the process running:

Ctrl+Z 
bg %1 
disown %1 

Variant 2: start the process in background with nohup:

nohup my_program 

GNU Screen

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Here are some links useful for starting:

https://gist.github.com/jctosta/af918e1618682638aa82

https://gist.github.com/fredrick/1216878

https://gist.github.com/joaopizani/2718397

https://wiki.archlinux.org/index.php/GNU_Screen

https://www.gnu.org/software/screen/manual/screen.html

For first steps you might need to remember only few screen commands:

screen # start a new screen session
screen -D -R # force attach to the running screen session if any available
screen -list # get list of running screen sessions
screen -r session.name # attach to the session by name

When in screen session:

Ctrl+a # standard command prefix
Ctrl+a c # create new window
Ctrl+a [1-9] # switch between windows
Ctrl+a " # window list
Ctrl+a d # detach session

A typical workflow with screen:

  1. Connect to the server
  2. Start new screen session or attach to existing one
  3. Run a program in screen session
  4. Detach session (and disconnect from the server)
  5. Re-attach the session to monitor the progress of running task

TMux

Tmux is a terminal multiplexer. What is a terminal multiplexer? It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. Xpra

x2Go