Commands in “ssh” family are simple and if you manage Linux servers you use them every day. Only hard thing can be to remember how to set-up new ssh connection. Because it is usually not every day task. So here is small cheat sheet:

  • install ssh-client:
    • sudo apt-get install openssh-client
  • install ssh-server:
    • sudo apt-get install openssh-server
  • generate key for user:
    • ssh-keygen -t rsa
      • with standard level of security – 2048 bits in key
    • ssh-keygen -t rsa -b 4096
      • if you want to be super-safe with double bit-size in key
  • import public key of current user on remote server if you can connect using password:
    • ssh-copy-id user@hostname_or_ip
  • ssh to port:
    • ssh user@hostname -p port
  • scp using port:
    • scp -P port source_file target_file
      • remote part has format: user@hostname:/path/filename
  • running command on other side using ssh:
    • basic syntax:
      • ssh user@host “here|even|very|long|command|with pipes”
      • ssh user@host “sudo here|even|very|long|command|with pipes”
        • to avoid output from command use
          • ssh -q ….
        • and test for exit code with $?
    • using variables:
      • ssh -q $myconnectstring “$commands”

 

Some useful hints for ssh:

  • ~/.bashrc is not automatically applied when you connect with ssh. You have to specify it in ~/.bash_profile:
    if [ -f ~/.bashrc ]; then
      . ~/.bashrc
    fi
    
  • if someone has problems with connection you can check auth log:
    grep 'ssh' /var/log/auth.log

    maybe you will see lines like this:
    May 25 15:02:48 yourhosname sshguard[1142]: Blocking xx.xx.xx.xx:xx for >630secs: 40 danger in 4 attacks over 433 seconds (all: 40d in 1 abuses over 433s).
    M

  • Convert pem key to ssh-rsa format
    ssh-keygen -f pub1key.pub -i
Tags: