<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rabina Twayana</title>
    <description>The latest articles on DEV Community by Rabina Twayana (@rabinatwayana).</description>
    <link>https://dev.to/rabinatwayana</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F463294%2F800fab4b-843c-4ab7-9e17-d0b35b086a6b.jpg</url>
      <title>DEV Community: Rabina Twayana</title>
      <link>https://dev.to/rabinatwayana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rabinatwayana"/>
    <language>en</language>
    <item>
      <title>Enhance Server Security: Configuring SSH to Listen on a Non-Default Port and Enabling Public-Key Authentication</title>
      <dc:creator>Rabina Twayana</dc:creator>
      <pubDate>Thu, 06 Apr 2023 16:21:46 +0000</pubDate>
      <link>https://dev.to/rabinatwayana/enhance-server-security-configuring-ssh-to-listen-on-a-non-default-port-and-enabling-public-key-authentication-2a1a</link>
      <guid>https://dev.to/rabinatwayana/enhance-server-security-configuring-ssh-to-listen-on-a-non-default-port-and-enabling-public-key-authentication-2a1a</guid>
      <description>&lt;h2&gt;
  
  
  Configuring SSH to Listen on a Non-Default Port
&lt;/h2&gt;

&lt;p&gt;SSH is a widely used protocol for secure remote access and management of servers and network devices. By default, SSH listens on port 22, but it can be configured to listen on a different port to enhance security and reduced risk of automated attacks. &lt;/p&gt;

&lt;p&gt;However, it's essential to keep in mind that ports ranging from 0 to 1023 are reserved for system services, and therefore, it's recommended to choose a port number between 1024 and 65535.&lt;/p&gt;

&lt;p&gt;To configure SSH to listen on a non-default port on the server, follow these steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Check SSH status:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Run the following command to check the SSH status, along with other relevant information, including the port number being used by SSH:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service ssh Status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;systemctl status sshd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the SSH service is not active, run the following command to start it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service ssh start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;systemctl start sshd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Check SSH Port Number:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;After executin g the first step command, you would have already know the SSH port number. Nevertheless, it's also possible to directly check the port number by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grep -i port /etc/ssh/sshd_config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: The location of the sshd_config file can differ based on the type of operating system you are using. In this instance, I am utilizing a Debian-based Linux system:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Change SSH Port Number:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;If you want to modify the SSH port, you'll need to make changes to the SSH configuration file. To begin, open the configuration file in edit mode.&lt;/p&gt;

&lt;p&gt;To modify the SSH port, you need to make changes to the SSH configuration file. To begin, open the configuration file in text edit mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nano /etc/ssh/sshd_config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, just change the SSH port number by uncommenting the line containing Port and specifying the new port number as below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Port [NEW PORT]&lt;/code&gt;&lt;br&gt;
where, [NEW PORT] is the desired port number.&lt;/p&gt;

&lt;p&gt;Example: Port 2222&lt;br&gt;
This will set the new port number as 2222.&lt;/p&gt;

&lt;p&gt;After making the necessary changes, save the file and exit the text editor. &lt;/p&gt;

&lt;p&gt;Now, restart the SSH services by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service ssh restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;systemctl restart sshd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Add New Rule on UFW&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, you'll need to create a new rule on UFW (Uncomplicated Firewall) to allow incoming traffic on the newly specified SSH port.&lt;/p&gt;

&lt;p&gt;To verify the status of the firewall and check the current set of firewall rules, execute the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If UFW status is shown as inactive, run the following command to enable the firewall:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw enable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After enabling the firewall, you can start adding rules to it. To add a new rule on UFW, you need to run the following command, specifying the port number and the protocol you want to use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw allow [Port Number]/protocol
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace [Port Number] with the new SSH port number you set in the SSH configuration file, and [PROTOCOL] with the protocol you want to use (usually tcp)&lt;/p&gt;

&lt;p&gt;Example: sudo ufw allow 2222/tcp&lt;/p&gt;

&lt;p&gt;This will allow incoming traffic on port 2222 for the TCP protocol. &lt;/p&gt;

&lt;p&gt;To confirm that the new rule has been added to the firewall, run &lt;code&gt;ufw status&lt;/code&gt; command again.&lt;/p&gt;

&lt;p&gt;To reload the firewall rules, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To deny the rule, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw deny [port]/tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: sudo ufw deny 80/tcp&lt;br&gt;
This will block incoming traffic on port 80 for HTTP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Connect to the Server&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;You should now be able to connect to the server using the new SSH port. &lt;/p&gt;

&lt;p&gt;Use the -p option to specify the port while connecting to the server from the SSH client terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh user_name@remote_server_ip_address -p port_number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: ssh &lt;a href="mailto:rabina@192.168.1.100"&gt;rabina@192.168.1.100&lt;/a&gt; -p 2222&lt;/p&gt;

&lt;h2&gt;
  
  
  SSH to Server Using Public-Key Authentication
&lt;/h2&gt;

&lt;p&gt;Additionally, enabling public-key authentication can further increase security by requiring a private key to authenticate rather than a password based authentication&lt;/p&gt;

&lt;p&gt;Here are the steps to SSH server using public key authentication:&lt;br&gt;
&lt;strong&gt;1. Update sshd Configuration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Update and uncomment following content in sshd configuration file(i.e. /etc/ssh/sshd_config)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PasswordAuthentication no
PubkeyAuthentication yes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, restart the SSH services in the server by running the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service ssh restart 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;systemctl restart sshd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Generate a Public-Private Key Pair&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Generate a public-private key pair on your local machine using the ssh-keygen command. You will be prompted to choose a name and location for the key files, and to set a passphrase for the private key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-keygen -t rsa -b 4096 -C [user_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: ssh-keygen -t rsa -b 4096 -C rabina&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Add client SSH Public Key to Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Copy the public key and add it  to the list of authorized keys on the server, allowing you to authenticate using the private key.&lt;/p&gt;

&lt;p&gt;Run the below command to see where the AuthorizedKeysFile file is located:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grep AuthorizedKeysFile /etc/ssh/sshd_config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, open the AuthorizedKeysFile file in text edit mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nano [AuthorizedKeysFile]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace [AuthorizedKeysFile] with the actual authorized_key file path&lt;/p&gt;

&lt;p&gt;Example: nano .ssh/authorized_keys&lt;/p&gt;

&lt;p&gt;Then, add public-key in new line and save and exit the edit mode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Connect to the Remote Server using SSH&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;If you generated the public-private key pair using the default path, you can use the following command to connect to the remote host on the new port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh user_name@remote_server_ip_address -p port_number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you generated the key pair in a custom location,, specify the location of your private key using the -i option&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh -i /path/to/private_key username@remote_server_ip_address -p port_number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will authenticate you using the private key instead of a password.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>ssh</category>
      <category>security</category>
    </item>
  </channel>
</rss>
