There are some steps to do after SSH is installed on a system and there is a old saying that says “A chain is only as strong as its weakest link” and if you are using a weak password for your root account (or any other account) then you are extremely vulnerable. It does not matter if the communication is secure when you are easily brute forced. All steps is used on a Ubuntu 11.10 but should be the same on OpenBSD, Debian, Linux Mint or any other Linux distribution with none or very few modifications.

We are going to do the following steps

  • Create certificate
  • Set correct credentials to .ssh folder and files
  • Shut down the possibility to log in with password
  • Prevent root to log in via SSH
  • Remove less secure encryption methods
  • Enable visual identification of the server fingerprint
  • Optional: Change SSH port (does really not not increase security)

Create certificate
We are going to use a RSA-key with a key length of 4096 bits. Open a terminal and enter the following “‘ssh-keygen -t rsa -b 4096”.  1024 bits key should be enough but better to be safe than sorry.

johan@johan-laptop:~$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.

Then you will be asked where to store the key. If you already got keys in id_dsa then you should enter another file name or your existing keys will be overwritten. If you are satisfied with the suggestion simply press enter.

Enter file in which to save the key (/home/johan/.ssh/id_rsa):

It’s now time to enter a password. Use a strong password with big and small letters, numbers and symbols. The password should also be unique and stored on a secure place like in a encrypted container like Keepass.

Enter passphrase (empty for no passphrase): 2sWf3+@/’?B>.%DpBU”r
Enter same passphrase again: 2sWf3+@/’?B>.%DpBU”r
Your identification has been saved in /home/johan/.ssh/id_rsa.

Your public key has been saved in /home/johan/.ssh/
The key fingerprint is:
The key’s randomart image is:
+–[ RSA 4096]—-+
|     o++ ..o.    |
|      Eoo ..     |
|      . o   . .  |
|     .   o o +   |
|      . S   +    |
|     . o o o     |
|    . + o .      |
|     + o .       |
|    . .          |

Enable the public key for authentication
The public key should be stored in ~/.ssh/authorized_keys and there can be more then one key for a single user. Just make a new row for each public key. If you key should be installed on the same system from where you just created the private key simply copy to authorized_keys

johan@johan-laptop:~$ cd ~/.ssh
johan@johan-laptop:~/.ssh$ cp authorized_keys

If you want to use the public key on another machine you could simply copy the public key using scp (secure copy). Please notice that you will replace existing authorized_keys if you already has one in place. To copy simply write the following command.

johan@johan-laptop:~/.ssh$ scp -p ~/.ssh/authorized_keys
johan@’s password:
authorized_keys 100% 1839 1.2MB/s 00:00

Set correct credentials to .ssh folder and files

Make sure that your working folder is your home folder, replace “johan” with your username.

johan@johan-laptop:~/.ssh$ cd ~
johan@johan-laptop:~/.ssh$ sudo chown -R johan:johan .ssh
johan@johan-laptop:~/.ssh$ sudo chmod -R 600 .ssh
johan@johan-laptop:~/.ssh$ sudo chmod +x .ssh

Do a test log in to test the public key

johan@johan-laptop:~/.ssh$ ssh johan@localhost
Enter passphrase for key ‘/home/johan/.ssh/id_rsa’:

After you entered the private key password you should have access to your machine, if not you will have to look for errors in the logs but I will not cover this in this guide.

Configure sshd
The next step is to modify sshd. All settings we will change is in the file /etc/ssh/sshd_config. Start to make a backup of sshd_config just in case.

johan@johan-laptop:/$ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config_backup


Use desired editor to edit sshd_config. I prefer vi but I will use nano in this example

johan@johan-laptop:/$ sudo nano /etc/ssh/sshd_config

The following lines is going to be added or altered:

  • PermitRootLogin yes
  • #PasswordAuthentication yes
  • Ciphers

PermitRootLogin no

root should never be used since it much more secure to use a regular user instead and then you need to perform a administrative task use the command sudo instead which gives you temporary administrative rights
We are also going to prevent the possibility to log in with password (you will be forced to use the private key). Find the rows which looks like  this:

PermitRootLogin yes

Modify it to look like this

PermitRootLogin no

Find the row which look like this

#PasswordAuthentication yes

Modify it to look like this

PasswordAuthentication no

At the end Cipers is going to be added and it may not apply never installations but the default ciphers has not always been the best choices and sshd should be forced to only use the strongest ones.

Ciphers aes128-ctr,aes256-ctr,arcfour256,arcfour,aes128-cbc,aes256-cbc

More information about why to alter the ciphers can be found here:

Verify these entries:

  • Protocol 2
  • UsePrivilegeSeparation yes
  • StrictModes yes
  • RSAAuthentication yes
  • PubkeyAuthentication yes

Save and exit

Restart to active the settings.

johan@johan-laptop:~/.ssh$ sudo service ssh restart
ssh start/running, process 2212

Enable visual identification of the servers fingerprint (Visual Host Key)
It’s not easy to verify and remember the fingerprint of a host since it’s a long hexadecimal string that may look like this one: ” 31:b0:be:0b:5b:7c:f1:79:65:e4:72:42:18:08:c4:8d” , some one may have altered the DNS record so that you in fact are trying to authenticate to a rouge server and to remember that string is near impossible. . It’s more easy to remember a visual fingerprint but it’s still not bulletproof. It’s absolute best to verify the exact string every time and that is done by most SSH clients and for example openssh stored them in ~/.ssh/known_hosts and gives you a warning if it has changed.

Do the following to enable visual host key

Edit eider /etc/ssh/ssh_config witch effects all users on the system or ~/.ssh/config to enable it for a single user.

Add the following lines (“Host * is already at top of ssh_config)

Host *
VisualHostKey yes

Test and verify
It’s now time to test and verify. You should not be able to log in without your private key and password authentication should been disabled. You should also see your visual finger print when you tries to log in.

Your SSH should be more safe now but remember that SSH probably was the most secure software from the beginning with default settings and MySQL, Apache or any other system also has to be secured.

—  Johan Ryberg

6 thoughts on “Configure SSH for high security

  1. Njae, att köra sin sshd på säg 10022 skulle jag snarare se som en försvagad säkerhet, då man helt plötsligt öppnar sig för scenariot att man ansluter till en demon startade utav en användare. Nej, ska man lägga sin sshd på en alternativ port så behåller jag den gärna på en privilegierad port, säg 1022.

    Vad gäller root-login så tycker jag det om inte annat kan var rimligt för en del backupjob, även om det strikt sett även kan gå att lösa med sudo. Hursom tycker jag då vara en rimlig tradeoff där är att sätta PermitRootLogin till forced-commands-only.

    Vad gäller att veta att man ansluter till rätt maskin så är ju annars DNSSEC och SSHFP en fin kombination.

    1. Du har bra synpunkter som vanligt =)

      I min omarbetade engelska version så tar jag knappt upp byte av port eftersom det blir någon form av “säkerhet via oordning” som inte ger något i praktiken förutom försvårar spridningen om tjänsten råkar ut för något 0-day och i värsta fall i kombination med en mask som gör att man klarar sig för att man har tur. Jag skall ändra lite i min formulering.

      // Johan

  2. Toppenbra genomgång.

    Det finns ett script med i ssh-paketet som heter ssh-copy-id som för över den publika nyckeln och fixar rättigheterna på mapp och fil. Problemet är att den (liksom din genomgång) använder ~/.ssh/authorizedkeys som “databas” över publika nycklar, vilket ställer till problem om användaren krypterar sin hemkatalog (det går ju inte läsa ~/.ssh/authorizedkeys förrän den är avkrypterad och den avkrypteras inte förrän användaren loggat in, vilket kräver ~/.ssh/authorizedkeys).

    Jag har läst att det ska gå att fixa detta moment 22 genom att ändra i /etc/ssh/sshd_config, men en sådan ändring ignorerar ju skriptet ssh-copy-id typiskt nog. Vet du hur man får det att fungera magiskt själv?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.