Customising ssh

Tired of typing the full address of shaheen when connecting via ssh? Using aliases and configuring your ssh client is the solution!

The default behavior of ssh can be configured by modifying the $HOME/.ssh/config file.  Each configuration in your config file is initiated by the keyword Host followed by an alias name you wil be now able use instead of  the full machine name.

Following this line, you have to define the configuration option you desire for this machine in particular. The most common SSH configuration options are:

  • HostName: the hostname or IP address of the machine to connect. 
  • User: the connection username.
  • Port: the port where the machine is expecting your connection (usually 22)

other interesting options are

  • Compression: a useful option for (very) slow connections.
  • ServerAliveInterval: to avoid having your session closed due to SSH timeouts. you can use this option to know if your unreliable connection is still alive.
  • StrictHostKeyChecking: this option is used to configure whether SSH automatically adds hosts to the ~/.ssh/known_hosts file. 
  • ForwardX11 yes: to systematically allow the forwarding of graphical window, without having to add -X or -Y when calling ssh
  • IdentityFile ~/.ssh/<my_ssh_key.rsa> to use a specific ssh key
  • ControlMaster
  • ControlPath  : these three Control options allow you to use ssh multiplexing, so that you only need to login with authentication once, as long as your first session remains open.
  • ControlPersist

 

An example $HOME/.ssh/config file entry for connecting to Shaheen is as follows:

Host shaheen
    HostName shaheen.hpc.kaust.edu.sa
    User my_login_name_on_shaheen
    ForwardX11 yes
    ServerAliveInterval 240
    ControlMaster auto
    ControlPath ~/.ssh/master-%r@%h:%p
    ControlPersist 4h
    

Instead of

ssh -X my_login_name_on_shaheen@shaheen.hpc.kaust.edu.sa

I now type

ssh shaheen

One last thing - to work properly,  permissions on $HOME/.ssh, as well as on $HOME/.ssh/config have to be restricted to your user only. To grant the correct permission on these files, run the following command once:

chmod 700 $HOME/.ssh

chmod 600 $HOME/.ssh/config