
Implement SSH key-based authentication on Linux
SSH (Secured Shell) is a protocol which creates a cryptographically secured connection between the SSH client and remote servers.
Using SSH you can manage and administer remote servers securely. This can be helpful in many ways.
- Multi-server deployment
- Stop/start services remotely
- Automation
and all your creativity (hopefully)…
As a sysadmin, this is kind of basic stuff to know.
Let’s learn how…
I will generate a private key and a public key. The private key should be stored on your ssh client machine and must be kept secured. The public key must be copied to the remote server to login to that server from the SSH client machine with no password required.
You’ll learn the following.
- Install SSH (not required if already installed)
- Generate SSH Keys
- Copy SSH Key to a remote server
- Log in to the remote server using SSH
For demonstration purpose, I have 2 servers with below IP addresses, one system is a client and the other one is a server on which I will log in through ssh from a client machine.
- Client (user -> geekflare) : 192.168.56.102
- Remote (user -> ubuntu) : 192.168.56.101
Installing SSH
Before you follow the steps mentioned in this article, make sure openssh-server
is installed on the servers. If it is not installed, run the commands below to install.
sudo apt-get install openssh-server
sudo systemctl enable ssh
sudo systemctl start ssh
You can run ssh command to check whether it is installed on the system or not.
[email protected]:~$ ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
ย ย ย ย ย ย ย ย ย ย [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
ย ย ย ย ย ย ย ย ย ย [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
ย ย ย ย ย ย ย ย ย ย [-i identity_file] [-J [[email protected]]host[:port]] [-L address]
ย ย ย ย ย ย ย ย ย ย [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
ย ย ย ย ย ย ย ย ย ย [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
ย ย ย ย ย ย ย ย ย ย [-w local_tun[:remote_tun]] destination [command]
Generate SSH Keys
This needs to be done on a client server.
Run the ssh-keygen
command to generate a SSH key. Just press enter when it asks for the file, passphrase, same passphrase. It generates a pair of keys in ~/.ssh directory by default. Id_rsa is the private key and id_rsa.pub is the associate public key.
[email protected]:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/geekflare/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/geekflare/.ssh/id_rsa.
Your public key has been saved in /home/geekflare/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:3XDm62tzJegGm8oAmFYCyeFZovJOuU42zNgyn9GzH30 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|o+..ย ย ย ย ย ย ย ย ย ย ย ย |
|+o+ย ย ย ย ย ย ย ย ย ย ย ย ย |
|oo. .ย ย ย ย . oย ย ย |
|.. *ย ย ย ย . *ย ย ย ย |
|ย B .ย ย S . o.ย ย |
| O o . .ย . ... .|
|+ @ o o . E=.ย o |
| B + o + .o.= .ย |
|ย + ... o. oo+ย ย |
+----[SHA256]-----+
It will generate two new files in ~/.ssh
directory.
Copy SSH Key to Remote Server
The private key should be copied ~/.ssh
folder on a remote server. Most of the servers should already have this folder if not, you need to create a folder.
And, to do so:
- Log in to the remote server with the user you would like to get connected. In my case, its
ubuntu
- Ensure the present working directory is the user’s home directory and then create a .ssh folder. You can also use the following single command to create one
mkdir -p ~/.ssh
If you already have .ssh folder then take a backup of it.
Next, let’s push the public key from a client server.
On the client machine (192.168.56.102), run the command below to copy the public key on the remote server inside an authorized_keys
file in .ssh directory.
[email protected]:~$ cat .ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'
[email protected]'s password:
Run the command below to set permissions on the authorized_keys file on remote server.
[email protected]:~$ ssh [email protected] "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
Great, this concludes key is exchanged and permission is all set.
Login to Remote Server using SSH
Let’s test to see if it works!
Let’s login to the remote server (192.168.56.101) from a client machine (192.168.56.102) as geekflare user.
Run the command below to test, it wonโt ask password this time.
[email protected]:~$ ssh [email protected]
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-151-generic i686)
ย * Documentation:ย https://help.ubuntu.com
ย * Management:ย ย ย ย https://landscape.canonical.com
ย * Support:ย ย ย ย ย ย ย https://ubuntu.com/advantage
346 packages can be updated.
11 updates are security updates.
Last login: Mon Jun 17 00:10:32 2019 from 192.168.56.101
Here you go…
I have logged in to remote server successfully. Running below command will give the IP details of the remote machine.
[email protected]:~$ ifconfig
enp0s3ย ย ย Link encap:Ethernetย HWaddr 08:00:27:9b:47:86ย
ย ย ย ย ย ย ย ย ย inet addr:10.0.2.15ย Bcast:10.0.2.255ย Mask:255.255.255.0
ย ย ย ย ย ย ย ย ย inet6 addr: fe80::5c62:3267:b752:fe5d/64 Scope:Link
ย ย ย ย ย ย ย ย ย UP BROADCAST RUNNING MULTICASTย MTU:1500ย Metric:1
ย ย ย ย ย ย ย ย ย RX packets:20239 errors:0 dropped:0 overruns:0 frame:0
ย ย ย ย ย ย ย ย ย TX packets:5406 errors:0 dropped:0 overruns:0 carrier:0
ย ย ย ย ย ย ย ย ย collisions:0 txqueuelen:1000
ย ย ย ย ย ย ย ย ย RX bytes:22678039 (22.6 MB) ย TX bytes:701710 (701.7 KB)
enp0s8ย ย ย Link encap:Ethernetย HWaddr 08:00:27:a9:4a:6bย
ย ย ย ย ย ย ย ย ย inet addr:192.168.56.101ย Bcast:192.168.56.255ย Mask:255.255.255.0
ย ย ย ย ย ย ย ย ย inet6 addr: fe80::54a9:761c:9034:21a2/64 Scope:Link
ย ย ย ย ย ย ย ย ย UP BROADCAST RUNNING MULTICASTย MTU:1500ย Metric:1
ย ย ย ย ย ย ย ย ย RX packets:330 errors:0 dropped:0 overruns:0 frame:0
ย ย ย ย ย ย ย ย ย TX packets:197 errors:0 dropped:0 overruns:0 carrier:0
ย ย ย ย ย ย ย ย ย collisions:0 txqueuelen:1000
ย ย ย ย ย ย ย ย ย RX bytes:42847 (42.8 KB)ย TX bytes:32774 (32.7 KB)
loย ย ย ย ย ย ย Link encap:Local Loopbackย
ย ย ย ย ย ย ย ย ย inet addr:127.0.0.1ย Mask:255.0.0.0
ย ย ย ย ย ย ย ย ย inet6 addr: ::1/128 Scope:Host
ย ย ย ย ย ย ย ย ย UP LOOPBACK RUNNINGย MTU:65536ย Metric:1
ย ย ย ย ย ย ย ย ย RX packets:997 errors:0 dropped:0 overruns:0 frame:0
ย ย ย ย ย ย ย ย ย TX packets:997 errors:0 dropped:0 overruns:0 carrier:0
ย ย ย ย ย ย ย ย ย collisions:0 txqueuelen:1
ย ย ย ย ย ย ย ย ย RX bytes:79654 (79.6 KB)ย TX bytes:79654 (79.6 KB)
Conclusion
Setting up SSH key exchange is very straightforward as you can see. I hope this helps you and interested in learning Linux administration and troubleshooting then check out this Udemy course.
More great readings on Sysadmin
-
Looking For Traceroute on RHEL 8? Try TracepathAbhishek Nair on June 14, 2022
-
6 Best Switch Port Monitoring ToolsDurga Prasad Acharya on June 12, 2022
-
Windows 10/11 Random Shutdown: How to Find the CauseHitesh Sant on May 30, 2022
-
7 Best Server Configuration Monitoring and Auditing ToolsTalha Khalid on May 28, 2022
-
8 Best FTP Server Software for Windows for Secure TransferSatish Shethi on May 24, 2022
-
OSI Model Layers: An Introduction GuideAmrita Pathak on May 13, 2022
Join Geekflare Newsletter
Every week we shareย trending articlesย andย toolsย in our newsletter. More than 10,000 people enjoy reading, and you will love it too.