In this post I will show you how to configure Key-Based SSH authentication between a client and server system running Linux. While this may seem pretty simple to day to day Unix admins, I'm often surprised when I hear someone that I would think knows this says "Can you configure the keys for me?". In this example I will be configuring the user brian to connect from a system named client to a system named server. However, in the real world you can use this key to authenticate to any number of servers.
Assuming you do not already have a valid private/public key pair, you can easily generate one on the client system as follows. Note that some distro's will probably already have this done for you so you can check first.
[brian@client ~]$ ls -l ~/.ssh ls: cannot access /home/brian/.ssh: No such file or directory
[brian@client ~]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/brian/.ssh/id_rsa): Created directory '/home/brian/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/brian/.ssh/id_rsa. Your public key has been saved in /home/brian/.ssh/id_rsa.pub. The key fingerprint is: c6:a7:d8:47:77:56:36:36:82:9f:cd:13:14:49:b8:03 brian@ip-172-31-3-252 The key's randomart image is: +--[ RSA 2048]----+ | o+o| | E.... | | ....=o| | . .o*o+| | S o .o++ | | + + . o .| | . o . | | . | | | +-----------------+
[brian@client ~]$ ls -l ~/.ssh total 8 -rw-------. 1 brian brian 1675 Dec 30 17:06 id_rsa -rw-r--r--. 1 brian brian 403 Dec 30 17:06 id_rsa.pub
[brian@client ~]$ [brian@ip-172-31-3-252 ~]$ cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAouz/Oyq/Xi2HAW8I4MQIHYqiIW57YZyv3UYY/CShIIhw0JkS1QyZPJElBnbiKccEM1tScrY2CrjNLI459cxXWtbgb/sfuB4qrAa8y3/lGKT9rc7zVgeyg9XPP9dFkNwlqTnjmGY7U+9zLxgZK2/D54EwDqQ3lf7TrSV+U772n634FmzC9E/gjv4lAqBGjj+dLusolpekjl3izDpu18mOE4cvQfFSZtAGgoVKoURt0cYhCuMKYrciXRTUM5HoxK5OT1l0CbC7EH4dfWIps2foGJEDCl99A3BxwuQ1a68ier8OPmdjHgepKzciCpRTpPKoOW7dZilBwi2LLgBmoeTCiw== brian@client
[brian@server ~]$ mkdir -p ~/.ssh [brian@server ~]$ chmod 700 ~/.ssh
[brian@server ~]$ vi ~/.ssh/authorized_keys [brian@server ~]$ chmod 600 ~/.ssh/authorized_keys
[brian@client ~]$ ssh server The authenticity of host 'server' can't be established. RSA key fingerprint is 78:de:1a:c5:96:d1:79:4b:6f:db:36:aa:b9:e0:53:c7. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'localhost' (RSA) to the list of known hosts. [brian@server ~]$