Install SSH server

apt-get install ssh

Running ssh-keygen as root will install the host keys, ssh-keygen as <user> the keypair into directory ~/.ssh. The private key file, e.g. ~/.ssh/id_rsa needs to be copied to all clients which need access to the server, the public key file, e.g. ~/.ssh/id_rsa.pub, needs to be added to the authorized_keys file of the server to allow access to the server. In addition, you should adjust the access rights for the different files and directories. As <user>, enter the following commands

On the client:

ssh-keygen
ssh -vvv -p <port#> <servername> // check the ssh connection
sftp -oPort=<port#> <servername> // transfer the public key file
sftp > put client_id_rsa.pub

On the server:

sudo cat client_id_rsa.pub >> ~/.ssh/authorized_keys

Make sure permissions and ownership are set correctly:

sudo chmod 700 ~/.ssh
sudo chmod 600 ~/.ssh/*
sudo chown -R user:user ~/.ssh

Check the ssh access:

ssh <servername> whoami // needs to return the username under which the ssh access was established, e.g. user1
ssh -l root <servername> whoami // needs to return "root", as this establishes the ssh access as root, not as user1
ssh -vvv -p <port#> -l root <servername> whoami // same as above, but use different port number with full debug verbose output

Important

  • generate the keypair under user rights, not root, e.g. user1
  • establish ssh access under user rights by accessing that user's .ssh directory on the server (use the user1 public key)
  • establish ssh access under root rights by accessing root's .ssh directory on the server (use the user1 public key)

You might need to include the user in sshd.conf to authorize access.

SSH access with keypair