{"id":477,"date":"2024-04-22T10:29:00","date_gmt":"2024-04-22T10:29:00","guid":{"rendered":"https:\/\/zahiralam.com\/blog\/?p=477"},"modified":"2024-04-22T10:31:25","modified_gmt":"2024-04-22T10:31:25","slug":"secure-and-convenient-ssh-login-a-guide-to-passwordless-authentication","status":"publish","type":"post","link":"https:\/\/zahiralam.com\/blog\/secure-and-convenient-ssh-login-a-guide-to-passwordless-authentication\/","title":{"rendered":"Secure and Convenient SSH Login: A Guide to Passwordless Authentication"},"content":{"rendered":"\n<p>SSH (Secure Shell) is an indispensable tool for secure remote administration of servers and network devices. While passwords are the traditional way to authenticate with SSH, they have several drawbacks:\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vulnerability to brute-forcing:<\/strong>&nbsp;Hackers can attempt to crack passwords with automated tools.<\/li>\n\n\n\n<li><strong>Weak passwords:<\/strong>&nbsp;Users often create passwords that are too simple to guess.<\/li>\n\n\n\n<li><strong>Password reuse:<\/strong>&nbsp;Using the same password across different services poses a larger security risk.<\/li>\n<\/ul>\n\n\n\n<p>Key-based authentication provides a more robust solution, eliminating the need for passwords and enhancing security.\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step-by-Step Guide<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Check for Existing Keys<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>OS Support:<\/strong>&nbsp;Linux, macOS, and Windows (modern versions with OpenSSH client)<\/li>\n\n\n\n<li>Open a terminal window and run:<\/li>\n<\/ul>\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-1\">ls -al ~\/.ssh\/id_*<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#ls%20-al%20~%2F.ssh%2Fid_%2A\">\n                            <button class=\"copy-button\" data-label=\"ls -al ~\/.ssh\/id_*\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you see files like\u00a0<code>id_rsa<\/code>\u00a0and\u00a0<code>id_rsa.pub<\/code>, you have an existing key pair. Try connecting with these before generating new ones. If it exists you can skip step 2.<\/li>\n<\/ul>\n\n\n\n<p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Generate a Key Pair<\/strong><\/h3>\n\n\n\n<p>Run the following command in your terminal:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-2\">ssh-keygen -t rsa -b 4096<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#ssh-keygen%20-t%20rsa%20-b%204096\">\n                            <button class=\"copy-button\" data-label=\"ssh-keygen -t rsa -b 4096\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>-t rsa:<\/strong>&nbsp;Specifies the RSA key type (a secure standard)<\/li>\n\n\n\n<li><strong>-b 4096:<\/strong>&nbsp;Sets the key size to 4096 bits (higher bit count = stronger security)<\/li>\n<\/ul>\n\n\n\n<p>You&#8217;ll be asked where to save the key (default is fine) and to enter a passphrase.\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Important:<\/strong>\u00a0Use a strong, unique passphrase. This is an extra layer of protection for your private key.<\/li>\n<\/ul>\n\n\n\n<p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Copy the Public Key to the Remote Server<\/strong><\/h3>\n\n\n\n<p><strong>Method 1: ssh-copy-id<\/strong>\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-3\">ssh-copy-id username@remote_server<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#ssh-copy-id%20username%40remote_server\">\n                            <button class=\"copy-button\" data-label=\"ssh-copy-id username@remote_server\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Replace &#8216;username&#8217; with your username on the server<\/li>\n\n\n\n<li>Replace &#8216;remote_server&#8217; with the server&#8217;s hostname or IP address<\/li>\n<\/ul>\n\n\n\n<p><strong>Method 2: Manual Copy<\/strong>\n\n\n\n<ul class=\"wp-block-list\">\n<li>View the contents of your public key (e.g.,&nbsp;<code>cat ~\/.ssh\/id_rsa.pub<\/code>)<\/li>\n\n\n\n<li>Log in to the remote server<\/li>\n\n\n\n<li>Create the&nbsp;<code>.ssh<\/code>&nbsp;directory if it doesn&#8217;t exist:&nbsp;<code>mkdir -p ~\/.ssh<\/code><\/li>\n\n\n\n<li>Edit the\u00a0<code>~\/.ssh\/authorized_keys<\/code>\u00a0file and paste your public key into it. Save and exit.<\/li>\n<\/ul>\n\n\n\n<p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Test the Connection<\/strong><\/h3>\n\n\n\n<p>Try the following:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-4\">ssh username@remote_server<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#ssh%20username%40remote_server\">\n                            <button class=\"copy-button\" data-label=\"ssh username@remote_server\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>You should log in without a password (but you might need your key&#8217;s passphrase).\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Simplify with ~\/.ssh\/config (Optional)<\/strong><\/h3>\n\n\n\n<p>Edit your local\u00a0<code>~\/.ssh\/config<\/code>\u00a0file:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-5\">Host myserver\n    HostName remote_server_hostname_or_IP\n    User your_username<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#Host%20myserver%0A%20%20%20%20HostName%20remote_server_hostname_or_IP%0A%20%20%20%20User%20your_username\">\n                            <button class=\"copy-button\" data-label=\"Host myserver\n    HostName remote_server_hostname_or_IP\n    User your_username\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>Now connect with a simple command:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-6\">ssh myserver<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#ssh%20myserver\">\n                            <button class=\"copy-button\" data-label=\"ssh myserver\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Security Considerations<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Protect your private key:<\/strong>&nbsp;Never share it and keep it secure on your local machine.<\/li>\n\n\n\n<li><strong>Strong passphrase:<\/strong>&nbsp;Your passphrase is vital if the private key is compromised.<\/li>\n\n\n\n<li><strong>Disable password authentication (optional):<\/strong>\u00a0Consider enhancing security by editing\u00a0<code>\/etc\/ssh\/sshd_config<\/code>\u00a0on the server and setting\u00a0<code>PasswordAuthentication no<\/code>. Reload the SSH service afterward.<\/li>\n<\/ul>\n\n\n\n<p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Advantages<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Enhanced security<\/strong><\/li>\n\n\n\n<li><strong>Brute-force resistance<\/strong><\/li>\n\n\n\n<li><strong>Convenient logins (especially with the .ssh\/config file)<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\n\n\n\n<p><strong>Get Started!<\/strong>\n\n\n\n<p>With passwordless SSH, you substantially improve security while enjoying streamlined access to your remote servers.\n\n\n\n<p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ:<\/h2>\n\n\n\n<p><strong>Q: What is SSH and why would I use it?<\/strong>\n\n\n\n<p><strong>A:<\/strong> SSH (Secure Shell) is a network protocol that lets you securely log into remote computers, execute commands, manage files, and more. It&#8217;s used by system administrators, network engineers, and developers who need remote access to servers and other network devices.\n\n\n\n<p><strong>Q: Why is key-based authentication better than passwords?<\/strong>\n\n\n\n<p><strong>A:<\/strong> Key-based authentication is considerably more secure than passwords for several reasons:\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Long, complex keys:<\/strong>&nbsp;The keys used are much harder to crack through brute-force attacks compared to typical passwords.<\/li>\n\n\n\n<li><strong>No password reuse:<\/strong>&nbsp;Using separate keys for each server limits security risks if one server is compromised.<\/li>\n\n\n\n<li><strong>Protection against password guessing:<\/strong>&nbsp;Unauthorized users cannot simply guess your login credentials.<\/li>\n<\/ul>\n\n\n\n<p><strong>Q: What if SSH uses a non-standard port?<\/strong>\n\n\n\n<p><strong>A:<\/strong> The default SSH port is 22. If your server uses a different port, you&#8217;ll need to specify it when connecting:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-7\">ssh username@remote_server -p port_number<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#ssh%20username%40remote_server%20-p%20port_number\">\n                            <button class=\"copy-button\" data-label=\"ssh username@remote_server -p port_number\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>(Replace &#8216;port_number&#8217; with the actual port)\n\n\n\n<p><strong>~\/.ssh\/config:<\/strong> Add the <code>Port<\/code> directive to your config file:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-8\">Host myserver\n    HostName remote_server_hostname_or_IP\n    User your_username\n    Port port_number<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#Host%20myserver%0A%20%20%20%20HostName%20remote_server_hostname_or_IP%0A%20%20%20%20User%20your_username%0A%20%20%20%20Port%20port_number\">\n                            <button class=\"copy-button\" data-label=\"Host myserver\n    HostName remote_server_hostname_or_IP\n    User your_username\n    Port port_number\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p><strong>Q: What should I do if I lose my private key?<\/strong>\n\n\n\n<p><strong>A:<\/strong> Losing your private key is serious. Unfortunately, there&#8217;s no way to recover it. You must:\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Immediately revoke the corresponding public key<\/strong>&nbsp;from the&nbsp;<code>authorized_keys<\/code>&nbsp;file on any server where it was installed. This prevents further access.<\/li>\n\n\n\n<li><strong>Generate a new key pair<\/strong>&nbsp;and re-establish secure connections to your servers.<\/li>\n<\/ol>\n\n\n\n<p><strong>Q: I&#8217;m getting a &#8216;Permission denied&#8217; error when trying to connect. What&#8217;s wrong?<\/strong>\n\n\n\n<p><strong>A:<\/strong> Several factors might cause this error:\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Incorrect username or hostname:<\/strong>&nbsp;Double-check you&#8217;re connecting to the right server with the correct username.<\/li>\n\n\n\n<li><strong>Missing public key on the server:<\/strong>&nbsp;Make sure your public key is placed correctly in the server&#8217;s&nbsp;<code>~\/.ssh\/authorized_keys<\/code>&nbsp;file.<\/li>\n\n\n\n<li><strong>Incorrect file permissions:<\/strong>&nbsp;The server&#8217;s&nbsp;<code>.ssh<\/code>&nbsp;directory and&nbsp;<code>authorized_keys<\/code>&nbsp;files need the right permissions.<\/li>\n\n\n\n<li><strong>SSH server not running:<\/strong>&nbsp;Ensure the SSH service is running on the remote server.<\/li>\n<\/ul>\n\n\n\n<p><strong>Q: Can I use passwordless login with multiple servers?<\/strong>\n\n\n\n<p><strong>A:<\/strong> Absolutely! You can use the same key pair on multiple servers. Simply copy your public key to the <code>~\/.ssh\/authorized_keys<\/code> file on each server you wish to access without a password.\n\n\n\n<p>\n","protected":false},"excerpt":{"rendered":"<p>SSH (Secure Shell) is an indispensable tool for secure remote administration of servers and network devices. While passwords are the traditional way to authenticate with [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[17,61,18],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-advanced-security","tag-mac-m1-m2-m3","tag-ssh","tag-ubuntu"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/comments?post=477"}],"version-history":[{"count":7,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts\/477\/revisions"}],"predecessor-version":[{"id":484,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts\/477\/revisions\/484"}],"wp:attachment":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/media?parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/categories?post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/tags?post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}