bosnadev

№ 12 · 2015-01-23 · 3 min · server

SSH Authentication With Keys Instead Passwords

Generating an SSH key pair with ssh-keygen, copying the public key to a server with ssh-copy-id, and turning off password logins in sshd_config.

written before the past year · entry 12 of 31 · 10 in the past year

This article is out of date
Written in 2015. Versions, APIs and advice may have changed since.

SSH is a must have tool for every System administrator or DevOps Engineer. It provides you a secure way to access remote servers. But, if you use standard password authentication there is always a chance that someone will break into system due to weak password. This potential risk increases if multiple users have SSH access to the server, because system administrator can’t influence to the users to make a better passwords.

Contents

  • 1 The Idea Of Public Key Cryptography
  • 2 Generate Keys
  • 3 Transfer The Public Key To The Server
  • 4 Disable Password Authentication

The Idea Of Public Key Cryptography

The basic idea behind key-based SSH authentication rely on asymmetric cryptography also know as public key cryptography. This type of cryptographic algorithm require two separate keys. The first key is a secret or private key, and second is a public key. Public key is used to encrypt plaintext, whereas private key is used to decrypt ciphertext. You can learn more about public-key cryptography in this MDN (Mozilla Developer Network) article.

Generate Keys

First what you need to do on the client machine is to generate private/public keys. To generate private/public key set you need to run the following command:

terminal1 line
mirzap@bosnadev:~$ ssh-keygen -t rsa

Now you will have an option to name your key pair and set password.

Public Key Encryption

Transfer The Public Key To The Server

When you have generated private/public key-pair you need to transfer public key to the remote server. This public key will allow you to identify your self to the server. SSH comes with an utility called ssh-copy-id that simply copies content of public key to the server’s ~/.ssh/authorized_keys :

terminal1 line
mirzap@bosnadev:~$ ssh-copy-id -i .ssh/mirzap.pub user@some_host_or_ip

You should have output similar to this:

Number of key(s) added: 1Now try logging into the machine, with:   "ssh '[email protected]_host_or_ip'"and check to make sure that only the key(s) you wanted were added.

If you don’t have ssh-copy-id utility on your system, you can copy content of the public key and add it to the authorized_key by your self.

Disable Password Authentication

You can now connect to the remote server using a private key you generated, but your system isn’t secure just yet. You need to disable password-based and allow only key-based authentication. You can improve your server security by applying other measures, like changing default port or enabling host-based authentication, but that is not the topic of this article.

To disable password authentication open /etc/ssh/sshd_config and find the following line:

/etc/ssh/sshd_config1 line
#PasswordAuthentication yes

uncomment it, and set to no :

/etc/ssh/sshd_config1 line
PasswordAuthentication no

Now /etc/ssh/sshd_config should look like this:

Disable Password AuthenticationSave and restart SSH server:

terminal2 lines
user@secure:~# /etc/init.d/ssh restart[ ok ] Restarting OpenBSD Secure Shell server: sshd.

If you try to connect to the server on the machine without private key, you’ll get this message:

terminal2 lines
vagrant@homestead:~$ ssh user@some-hostPermission denied (publickey).

But on your local machine, where you created private key, authentication should pass without problems:

terminal3 lines
mirzap@bosnadev:~$ ssh user@some-hostLast login: Fri Jan 23 15:24:15 2015 from some-ipuser@secure:~#

If you have created password during key generation, you’ll be asked to enter it on each login:

terminal3 lines
mirzap@bosnadev:~$ ssh user@some-hostEnter passphrase for key '/home/mirzap/.ssh/mirzap_rsa':user@secure:~#

Related

Apache2 mod_fcgid: HTTP request length exceeds MaxRequestLenserverVirtual hosts overlap on 443, the first has precedenceserverInstalling Icinga2 (fork of Nagios) on Debian 7 (Wheezy)server