{"id":7336,"date":"2021-06-16T08:03:03","date_gmt":"2021-06-16T08:03:03","guid":{"rendered":"https:\/\/linuxways.net\/?p=7336"},"modified":"2021-06-16T08:03:03","modified_gmt":"2021-06-16T08:03:03","slug":"how-to-change-ssh-port-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-change-ssh-port-in-linux\/","title":{"rendered":"How to Change SSH Port in Linux"},"content":{"rendered":"<p>By default, SSH protocol is known to listen to port 22. While it&#8217;s perfectly OK, in a security sense, it\u2019s recommended to change the default port to another port. This greatly minimizes the risks of hackers launching automated brute-force attacks using SSH. In this guide, we will demonstrate how you can change the default SSH port to something else in Linux.<\/p>\n<h2><strong>Prerequisites<\/strong><\/h2>\n<p>As you get started out, ensure that you have an instance of a Linux server. For this guide, we are using Ubuntu 20.04 server.<\/p>\n<p>Ensure that you also have an SSH client ( such as Putty ) or even a client Linux system to set the connection to the server using the new port.<\/p>\n<h2><strong>Step 1: Check if SSH is running on the default port<\/strong><\/h2>\n<p>Firstly, we need to verify that the SSH service is running on the server. To accomplish this, run the following command:<\/p>\n<pre><strong>$ sudo systemctl status ssh<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"790\" height=\"220\" class=\"wp-image-7337\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-525.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-525.png 790w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-525-300x84.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-525-768x214.png 768w\" sizes=\"auto, (max-width: 790px) 100vw, 790px\" \/><\/p>\n<p>The output is a confirmation that the SSH service is active. Let\u2019s even go further and ensure that it is running on the default port. Run the following netstat command.<\/p>\n<pre><strong>$ sudo netstat -pnltu | grep 22<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"801\" height=\"110\" class=\"wp-image-7338\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-526.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-526.png 801w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-526-300x41.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-526-768x105.png 768w\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" \/><\/p>\n<h2><strong>Step 2: Select a port and configure SSH<\/strong><\/h2>\n<p>As you might know, TCP ports range from port 0 &#8211; 65535. However, the ports from 0 &#8211; 1024 are regarded as privileged ports designated for special services such as HTTP ( Port 80 ), FTP ( 21 ), and SMTP ( 25 ) to mention just a few.<\/p>\n<p>To avoid any conflict with privileged ports, we are going to pick a port that\u2019s above port 1024. In this example, we have chosen to change the default port to port 5466. To enable this, we will modify the <strong> \/etc\/ssh\/sshd_config<\/strong> SSH configuration file.<\/p>\n<pre><strong>$ sudo vim \/etc\/ssh\/sshd_config<\/strong><\/pre>\n<p>Locate the line that starts with <strong>#Port 22<\/strong> as shown.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"514\" height=\"190\" class=\"wp-image-7339\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-527.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-527.png 514w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-527-300x111.png 300w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/p>\n<p>Uncomment and specify the port number.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"490\" height=\"193\" class=\"wp-image-7340\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-528.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-528.png 490w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-528-300x118.png 300w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/p>\n<p>Thereafter, save the changes made and exit the configuration file. For the changes to persist, restart the SSH service.<\/p>\n<pre><strong>$ sudo systemctl restart ssh<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"153\" class=\"wp-image-7341\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-529.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-529.png 649w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-529-300x71.png 300w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<p>To verify that SSH is listening to the newly configured port, run:<\/p>\n<pre><strong>$ sudo netstat -pnltu | grep 5466<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"112\" class=\"wp-image-7342\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-530.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-530.png 800w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-530-300x42.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-530-768x108.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<h2><strong>Step 3: Allow the new SSH port on the UFW firewall<\/strong><\/h2>\n<p>If the UFW firewall is enabled, be sure to open the configured port as shown.<\/p>\n<pre><strong>$ sudo ufw allow 5466\/tcp<\/strong><\/pre>\n<pre><strong>$ sudo ufw reload<\/strong><\/pre>\n<p>For CentOS \/ RHEL, open the port as follows.<\/p>\n<pre><strong>$ sudo firewall-cmd --add-port=5466\/tcp --add-zone=public --permanent<\/strong><\/pre>\n<pre><strong>$ sudo firewall-cmd --reload<\/strong><\/pre>\n<p>At this point, you can remotely access the server, and this is what we will demonstrate shortly.<\/p>\n<h2><strong>Step 4: Logging in using the new port<\/strong><\/h2>\n<p>To log in to the server, head over to the SSH client. Specify the port number using the <strong>-p<\/strong> option followed by the SSH port and the remote server identity.<\/p>\n<pre><strong>$ ssh -p 5466 james@192.168.2.104<\/strong><\/pre>\n<p>As you would expect, you will be prompted to provide the password after which you will be granted access to the remote server.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"293\" class=\"wp-image-7343\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-531.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-531.png 831w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-531-300x106.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-531-768x271.png 768w\" sizes=\"auto, (max-width: 831px) 100vw, 831px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>This brings this topic to a close. For better security, consider using<a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-set-up-passwordless-ssh-authentication-on-linux\/\"> SSH passwordless authentication<\/a> with SSH public\/private key pair.<\/p>","protected":false},"excerpt":{"rendered":"<p>By default, SSH protocol is known to listen to port 22. While it&#8217;s perfectly OK, in a security sense, it\u2019s recommended to change the default port to another&hellip;<\/p>","protected":false},"author":1,"featured_media":7356,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[21,100],"class_list":["post-7336","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\/7336","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=7336"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7336\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/7356"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=7336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=7336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=7336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}