SSH Security

Jul 30, 2011·
Julio Batista Silva
Julio Batista Silva
· 4 min read
blog Linux

Use strong passwords

Strong passwords consist of a string that no hacker could work out, either by guessing or by a brute-force attack lasting a few hours.

Try to choose passwords with:

  • At least 15 characters.
  • A mix of letters (upper and lower case), numbers and non-alphanumeric characters.

There are programs such as pwgen, makepasswd, apg and KeePassX, and Unix commands, that generate secure random passwords:

tr -dc '_A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' < /dev/urandom | head -c25
pwgen -cnsy 25 1

pwgen options:

  • -c: at least one upper case letter.
  • -n: at least one numeric character.
  • -s: generates completely random passwords.
  • -y: at least one symbol.
  • 25: the password length.
  • 1: how many passwords to generate.

The passwords generated by these programs are indeed secure. But they are very hard to remember, which means you have to write them down somewhere. So I strongly recommend creating a long phrase (a passphrase) that you — and only you — can memorize.

Disable root login over SSH

  • Edit the following line in the file /etc/ssh/sshd_config:

    PermitRootLogin no
    
  • Restart the daemon:

    sudo /etc/rc.d/sshd restart
    

    or, if you use systemd:

    julio@acer ~> sudo systemctl restart sshd
    

When you need root access, use the su command.

Limit who can log in over ssh

This is useful when only a few users need to reach the system over ssh. Add the following line to /etc/ssh/sshd_config:

AllowUsers usr_1 usr_2 usr_3

Turn off “Protocol 1”

SSH has two protocols. Since Protocol 1 is old and insecure, uncomment the Protocol 2 line.

Change the port

By default the port used for ssh is 22. Change it to some random number, preferably above 1024. For example: Port 2345

Note that we will now always have to say which port to connect to:

ssh -p 2345 julio@casa.juliobs.com

Here I also had to open the port (TCP only) on my router.

Use a firewall

We can configure iptables to allow only a few specific IPs to connect to the port used for SSH:

iptables -A INPUT -p tcp -s 201.43.81.111 --dport 22 -j ACCEPT

Here I cannot use the option above, since I often need to connect from different IPs. So a more interesting solution is to limit the number of attempts coming from the same IP.

This records the IP that tried to reach port 22 and rejects packets if that same IP has tried to connect 4 or more times in the last minute:

iptables -A INPUT -p tcp --dport 22 -m recent --set --name ssh --rsource

iptables -A INPUT -p tcp --dport 22 -m recent ! --rcheck --seconds 60 \
         --hitcount 4 --name ssh --rsource -j ACCEPT

This drops the connection if the same IP tries to connect more than 3 times to port 22 in the same minute:

iptables -A INPUT -p tcp --dport 22 --syn -m limit --limit 1/m --limit-burst 3 -j ACCEPT

iptables -A INPUT -p tcp --dport 22 --syn -j DROP

Use public/private keys for authentication

  1. Create a key:

    $ ssh-keygen -t rsa -b 4096 -f servidorx -C "pubkey para servidor X"
        Generating public/private rsa key pair.
        Enter passphrase (empty for no passphrase):
        Enter same passphrase again:
        Your identification has been saved in servidorx.
        Your public key has been saved in servidorx.pub.
        The key fingerprint is:
        e1:5c:44:b2:f4:48:c7:22:62:16:99:fb:44:11:23:a7 pubkey para servidor X
        The key's randomart image is:
        +--[ RSA 4096]----+
        |    o+=o+o+      |
        |    *+o+.B.      |
        |   oE+ .+.o      |
        |    . .o o       |
        |     o  S        |
        |      .          |
        |                 |
        |                 |
        |                 |
        +-----------------+
    

    ssh-keygen’s default algorithm is 2048-bit RSA; in this example I used 4096-bit RSA. It may be overkill, but you never know.

    2020 update: RSA 4096 is still secure, but the recommended choice is Ed25519: ssh-keygen -t ed25519 -f servidorx -C "pubkey".

  2. Copy the key to the remote server.

    ssh-copy-id -i servidorx.pub '-p 2345 julio@servidorx.com'
    

    That command is equivalent to appending the public key servidorx.pub to the remote server’s ~/.ssh/authorized_keys file.

  3. Make sure the permissions are correct:

    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/servidorx
    
  4. Create the file ~/.ssh/config, which holds some useful per-host settings, shortcuts and aliases:

    Host github.com
        HostName        github.com
        IdentityFile    ~/.ssh/github
        User            jbsilva
    
    Host juliobs.com
        HostName        juliobs.com
        IdentityFile    ~/.ssh/juliobs
        User            julio
    
    Host servidorx
        HostName        ssh.servidorx.com
        IdentityFile    ~/.ssh/servidorx
        User            usuariox
        Port            2345
    

    This file makes life a little easier, since we no longer have to remember the user name, the server’s full address or the port.

    Instead of connecting to servidorX with:

    ssh -i servidorx -p 2345 usuariox@ssh.servidorx.com
    

    We can simply do:

    ssh servidorx
    

Julio Batista Silva
Authors
Senior Cloud Developer

I’m a Brazilian-German computer engineer based in Germany, passionate about tech, science, photography, and languages.

I’ve been programming for about two decades already, exploring everything from mobile apps and web development to machine learning. These days I focus on cloud SRE and data engineering.

I volunteer in the open source and Python communities, helping organize PyCon DE and PyData Berlin, serving on the PSF D&I Work Group, mentoring, and contributing code and translations.

On my blog, I share Linux tips, setup guides, and personal notes I’ve written for future reference. I hope others find them helpful as well. The content is available in multiple languages.

Browse my gallery for some of my photography.

Away from the keyboard, you’ll find me at concerts, playing clarinet, cycling, scuba diving, or exploring new places, cultures, and cuisines.

Always happy to connect! 🙂

comments powered by Disqus