<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Diego A. Carrasco Gubernatis | Personal Website (Posts about ssh)</title><link>https://diegocarrasco.com/</link><description></description><atom:link href="https://diegocarrasco.com/categories/ssh.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2026 &lt;a href="mailto:hi@diegocarrasco.com"&gt;Diego Carrasco G.&lt;/a&gt; </copyright><lastBuildDate>Sat, 11 Apr 2026 08:03:45 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>How to configure an Ubuntu Linux server to allow SSH sessions only with a key and disable password-based </title><link>https://diegocarrasco.com/how-to-configure-a-linux-server-to-allow-ssh-sessions-only-with-a-key-and-disable-password-based-authentication/</link><dc:creator>Diego Carrasco G.</dc:creator><description>&lt;div class="toc"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://diegocarrasco.com/how-to-configure-a-linux-server-to-allow-ssh-sessions-only-with-a-key-and-disable-password-based-authentication/#context"&gt;Context&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;&lt;a href="https://diegocarrasco.com/how-to-configure-a-linux-server-to-allow-ssh-sessions-only-with-a-key-and-disable-password-based-authentication/#what-is-an-ssh-key-and-why-is-it-more-secure"&gt;What is an SSH key and why is it more secure?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://diegocarrasco.com/how-to-configure-a-linux-server-to-allow-ssh-sessions-only-with-a-key-and-disable-password-based-authentication/#steps-to-configure-the-server"&gt;Steps to configure the server&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;&lt;a href="https://diegocarrasco.com/how-to-configure-a-linux-server-to-allow-ssh-sessions-only-with-a-key-and-disable-password-based-authentication/#1-create-an-ssh-key-pair-if-you-dont-have-one"&gt;1. Create an SSH Key Pair (if you don't have one)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://diegocarrasco.com/how-to-configure-a-linux-server-to-allow-ssh-sessions-only-with-a-key-and-disable-password-based-authentication/#2-copy-the-public-key-to-the-server"&gt;2. Copy the public key to the server&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://diegocarrasco.com/how-to-configure-a-linux-server-to-allow-ssh-sessions-only-with-a-key-and-disable-password-based-authentication/#3-test-the-configuration"&gt;3. Test the configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://diegocarrasco.com/how-to-configure-a-linux-server-to-allow-ssh-sessions-only-with-a-key-and-disable-password-based-authentication/#4-configure-the-server-to-only-allow-ssh-sessions-with-a-key-and-disable-password-based-authentication"&gt;4. Configure the server to only allow SSH sessions with a key and disable password-based authentication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://diegocarrasco.com/how-to-configure-a-linux-server-to-allow-ssh-sessions-only-with-a-key-and-disable-password-based-authentication/#5-restart-the-ssh-service"&gt;5. Restart the SSH service&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id="context"&gt;Context&lt;/h2&gt;
&lt;p&gt;I have a server running Ubuntu 22.04 LTS which I use for some personal projects and I wanted to make it more secure 
by only allowing SSH sessions with a key and disabling password-based authentication.&lt;/p&gt;
&lt;h3 id="what-is-an-ssh-key-and-why-is-it-more-secure"&gt;What is an SSH key and why is it more secure?&lt;/h3&gt;
&lt;p&gt;SSH keys are a pair of cryptographic keys which are used to authenticate to an SSH server. When you generate the 
key, 2 files are created. There  is a private key (kept in the client) and a public key &lt;code&gt;.pub&lt;/code&gt; (added to the server). 
The private key is used to encrypt the data and the public key is used to decrypt it. &lt;/p&gt;
&lt;h2 id="steps-to-configure-the-server"&gt;Steps to configure the server&lt;/h2&gt;
&lt;h3 id="1-create-an-ssh-key-pair-if-you-dont-have-one"&gt;1. Create an SSH Key Pair (if you don't have one)&lt;/h3&gt;
&lt;p&gt;On your local machine, run the following command to generate a new SSH key pair. (You can use the default values)&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;ssh-keygen&lt;span class="w"&gt; &lt;/span&gt;-t&lt;span class="w"&gt; &lt;/span&gt;rsa&lt;span class="w"&gt; &lt;/span&gt;-b&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;4096&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-C&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"your_email@example.com"&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Notes:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The email is used as a label for the key. You can use any email you want.&lt;/li&gt;
&lt;li&gt;If you want additional security, add a passphrase to the key. (You will need to enter the passphrase each time you 
  use the key). 
-- There are different views on whether this is a good idea or not. I think it is a good idea, but it is up to you.
-- You can skip this step by pressing enter twice.&lt;/li&gt;
&lt;li&gt;The default key name is &lt;code&gt;id_rsa&lt;/code&gt;. You can use a different name if you want.&lt;/li&gt;
&lt;li&gt;The default location is &lt;code&gt;~/.ssh&lt;/code&gt;. You can use a different location if you want.&lt;/li&gt;
&lt;li&gt;The default permissions are &lt;code&gt;600&lt;/code&gt; for the private key. (I have a quick-note about permissions at &lt;a href="https://diegocarrasco.com/quick-note-ssh-keys-permissions/"&gt;(Quick-note) SSH 
  Keys Permissions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="2-copy-the-public-key-to-the-server"&gt;2. Copy the public key to the server&lt;/h3&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;ssh-copy-id&lt;span class="w"&gt; &lt;/span&gt;&amp;lt;username&amp;gt;@&amp;lt;server_ip&amp;gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you have more than one key (identity), you can specify the key to use with the &lt;code&gt;-i&lt;/code&gt; flag. (The default is &lt;code&gt;~/.
ssh/id_rsa.pub&lt;/code&gt;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You can also add the public key to the server manually. (This is useful if you don't have &lt;code&gt;ssh-copy-id&lt;/code&gt; installed)&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;cat&lt;span class="w"&gt; &lt;/span&gt;~/.ssh/id_rsa.pub&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;ssh&lt;span class="w"&gt; &lt;/span&gt;&amp;lt;username&amp;gt;@&amp;lt;server_ip&amp;gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"mkdir -p ~/.ssh &amp;amp;&amp;amp; cat &amp;gt;&amp;gt; ~/.ssh/authorized_keys"&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;or just copy the contents of the public key &lt;code&gt;.pub&lt;/code&gt; and paste it in the &lt;code&gt;~/.ssh/authorized_keys&lt;/code&gt; file on the server.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;ssh-copy-id&lt;span class="w"&gt; &lt;/span&gt;-i&lt;span class="w"&gt; &lt;/span&gt;~/.ssh/id_rsa.pub&lt;span class="w"&gt; &lt;/span&gt;&amp;lt;username&amp;gt;@&amp;lt;server_ip&amp;gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id="3-test-the-configuration"&gt;3. Test the configuration&lt;/h3&gt;
&lt;p&gt;Check if you can access the server with the key. If you are using the default key, you don't need to specify it.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;ssh&lt;span class="w"&gt; &lt;/span&gt;&amp;lt;username&amp;gt;@&amp;lt;server_ip&amp;gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you are using a different key, you need to specify it with the &lt;code&gt;-i&lt;/code&gt; flag.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;ssh&lt;span class="w"&gt; &lt;/span&gt;-i&lt;span class="w"&gt; &lt;/span&gt;~/.ssh/&amp;lt;key_name&amp;gt;&lt;span class="w"&gt; &lt;/span&gt;&amp;lt;username&amp;gt;@&amp;lt;server_ip&amp;gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you can access the server, you can continue with the next steps. If not, check the logs in &lt;code&gt;/var/log/auth.log&lt;/code&gt; to 
find out what is wrong. (You can use &lt;code&gt;tail -f /var/log/auth.log&lt;/code&gt; to see the logs in real time)&lt;/p&gt;
&lt;h3 id="4-configure-the-server-to-only-allow-ssh-sessions-with-a-key-and-disable-password-based-authentication"&gt;4. Configure the server to only allow SSH sessions with a key and disable password-based authentication&lt;/h3&gt;
&lt;p&gt;Edit the &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;nano&lt;span class="w"&gt; &lt;/span&gt;/etc/ssh/sshd_config
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And add or edit the following line:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;PasswordAuthentication no
&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id="5-restart-the-ssh-service"&gt;5. Restart the SSH service&lt;/h3&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;systemctl&lt;span class="w"&gt; &lt;/span&gt;restart&lt;span class="w"&gt; &lt;/span&gt;ssh
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;or, if you are using an older version of Ubuntu&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;service&lt;span class="w"&gt; &lt;/span&gt;ssh&lt;span class="w"&gt; &lt;/span&gt;restart
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You have now configured your server to allow ssh sessions only with a key 😁&lt;/p&gt;
&lt;!--
5. Disable password-based authentication for all users




6. (Optional) Disable root login
7. (Optional) Disable password-based authentication for specific users
8. (Optional) Disable password-based authentication for specific groups
9. (Optional) Disable password-based authentication for specific users and groups
--&gt;</description><category>command-line</category><category>linux</category><category>security</category><category>ssh</category><category>ssh identity</category><category>ssh key</category><category>terminal</category><category>ubuntu</category><guid>https://diegocarrasco.com/how-to-configure-a-linux-server-to-allow-ssh-sessions-only-with-a-key-and-disable-password-based-authentication/</guid><pubDate>Thu, 16 Nov 2023 09:12:35 GMT</pubDate></item></channel></rss>