{"id":5512,"date":"2021-04-02T18:07:14","date_gmt":"2021-04-02T18:07:14","guid":{"rendered":"https:\/\/linuxways.net\/?p=5512"},"modified":"2021-04-04T11:19:47","modified_gmt":"2021-04-04T11:19:47","slug":"how-to-set-up-passwordless-ssh-authentication-on-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-set-up-passwordless-ssh-authentication-on-linux\/","title":{"rendered":"How to set up passwordless SSH Authentication on Linux"},"content":{"rendered":"<p>Passwords are becoming vulnerable with the ever-evolving cybersecurity environment. No more than ever, systems are becoming prone to brute-force attacks. Users are also making it easy for hackers to infiltrate their systems by securing systems with weak and often guessable passwords. 2 Factor authentication is now commonplace and it provides an extra layer of protection using OTP codes to ensure that it\u2019s actually the user logging into the system.<\/p>\n<p>Another way you can secure your server is by setting up passwordless SSH authentication on your server. This uses public-key SSH authentication where an SSH key pair is generated on the local system. The key pair is made up of a private and a public SSH key. The private key remains on the local system whilst the public key is uploaded to the remote system. During authentication, a key exchange occurs between the public key saved on the remote system and the private key on the local system. Subsequent connections are thus secured and no password will be required.<\/p>\n<p>In this guide, we take you through how to configure password SSH authentication in 3 simple steps.<\/p>\n<h2><strong>Passwordless SSH setup Lab<\/strong><\/h2>\n<p>For demonstration purposes, we will use the following lab setup:<\/p>\n<pre>Local Linux system - 192.168.2.104<\/pre>\n<pre>Remote Linux system - 192.168.2.108<\/pre>\n<p>It would be prudent to also ensure that you can access the remote system from your local system using SSH password authentication.<\/p>\n<p>Let\u2019s get started!<\/p>\n<h2><strong>Step 1. Generate SSH key pair<\/strong><\/h2>\n<p>Right off the bat, we will generate an SSH key-pair which will be stored in the home directory. So, invoke the <strong>ssh-keygen <\/strong>command as shown. Here, we are creating a 4096-bit key pair.<\/p>\n<pre><strong>$ ssh-keygen -b 4096<\/strong><\/pre>\n<p>Accept the defaults by simply pressing ENTER. You can find the SSH key pair in the <strong>~\/.ssh <\/strong>directory which is a hidden directory in your home folder.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"479\" class=\"wp-image-5513\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-521.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-521.png 737w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-521-300x195.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/strong><\/p>\n<p>Just to be cocksure use the ls command to check the presence of the SSH keys.<\/p>\n<pre><strong>$ ls .ssh<\/strong><\/pre>\n<p>The private key is denoted by <strong>id_rsa <\/strong>while <strong>id_rsa.pub <\/strong>is the public key. These are cryptographic keys that are encrypted. We will, later on, copy the public key to the remote system.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"153\" class=\"wp-image-5514\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-522.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-522.png 736w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-522-300x62.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/p>\n<h2><strong>Step 2. Copy the SSH key pair to the remote system<\/strong><\/h2>\n<p>Next, you need to copy and save the public key to the remote Linux system. Why are we doing this? In public-key authentication, an authentication handshake takes place between the public key on the remote system and the private key residing on the local system. This ensures that all subsequent traffic between the local host system and the remote system is encrypted.<\/p>\n<p>To copy the SSH public key, run the <strong>ssh-copy-id<\/strong> command as follows.<\/p>\n<pre><strong>$ ssh-copy-id user@remote-ip<\/strong><\/pre>\n<p>In our case, this will be:<\/p>\n<pre><strong>$ ssh-copy-id james@192.168.2.102<\/strong><\/pre>\n<p>To continue connecting, type \u2018Yes\u2019 and press ENTER. Thereafter, provide the password to the remote system to log in and copy the SSH public key.<\/p>\n<p>The public SSH key is added to a unique file called <strong>authorized_keys<\/strong> in the home directory of the user account you are logging into. The full path to this file is <strong>~\/.ssh\/authorized_keys<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"826\" height=\"342\" class=\"wp-image-5515\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-523.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-523.png 826w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-523-300x124.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-523-768x318.png 768w\" sizes=\"auto, (max-width: 826px) 100vw, 826px\" \/><\/p>\n<p>After successfully copying the SSH key, you can now log in to the remote system using the public key authentication as follows.<\/p>\n<pre><strong>$ ssh james@192.168.2.102<\/strong><\/pre>\n<p>From the snippet below, you can see that we have logged in to the remote system without even being prompted for a password. Awesome!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"832\" height=\"444\" class=\"wp-image-5516\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-524.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-524.png 832w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-524-300x160.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-524-768x410.png 768w\" sizes=\"auto, (max-width: 832px) 100vw, 832px\" \/><\/p>\n<p>As mentioned earlier, the public key is saved in the <strong>~\/.ssh\/authorized_keys <\/strong>path.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"272\" class=\"wp-image-5517\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-525.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-525.png 831w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-525-300x98.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-525-768x251.png 768w\" sizes=\"auto, (max-width: 831px) 100vw, 831px\" \/><\/p>\n<h2><strong>Step 3. Disable password authentication (Optional)<\/strong><\/h2>\n<p>If you want to boost the security of your remote system, you might want to consider disabling password authentication. In doing so, you ensure that you are the only one who can access the system using the private key sitting on your local system. The key should always be a top secret. Never share it with anyone lest your system becomes compromised.<\/p>\n<p>On the remote host, open access the<strong> \/etc\/sshd_config<\/strong> file a shown.<\/p>\n<pre><strong>$ sudo vim \/etc\/sshd_config<\/strong><\/pre>\n<p>Locate the <strong>PasswordAuthentication <\/strong>parameter and set it to <strong>no<\/strong> as shown.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"105\" class=\"wp-image-5518\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-526.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-526.png 710w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-526-300x44.png 300w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/p>\n<p>Next, ensure that Public Key authentication is enabled as shown.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"839\" height=\"177\" class=\"wp-image-5519\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-527.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-527.png 839w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-527-300x63.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-527-768x162.png 768w\" sizes=\"auto, (max-width: 839px) 100vw, 839px\" \/><\/p>\n<p>For the changes to persist, perform a restart of the SSH service as follows.<\/p>\n<pre><strong>$ sudo systemctl restart ssh<\/strong><\/pre>\n<p>To demonstrate that password authentication is disabled, we will try and access the server using the Putty SSH client. Provide the remote system\u2019s address and hit \u2018Open\u2019<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"454\" height=\"448\" class=\"wp-image-5520\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-528.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-528.png 454w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-528-300x296.png 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/p>\n<p>Provide the remote username and hit ENTER. This time around, you get an authentication since we disabled password authentication.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"419\" class=\"wp-image-5521\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-529.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-529.png 663w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-529-300x190.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/strong><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Public key authentication is a tamperproof way of connecting and hardening your server. It eliminates attack vectors such as brute force password attacks which are ominous when your system has password authentication.<\/p>","protected":false},"excerpt":{"rendered":"<p>Passwords are becoming vulnerable with the ever-evolving cybersecurity environment. No more than ever, systems are becoming prone to brute-force attacks. Users are also making it easy for hackers&hellip;<\/p>","protected":false},"author":1,"featured_media":5569,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[21,100],"class_list":["post-5512","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-ssh","tag-ubuntu-20-04"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=5512"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5512\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/5569"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=5512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=5512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=5512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}