{"id":7693,"date":"2021-06-28T03:06:53","date_gmt":"2021-06-28T03:06:53","guid":{"rendered":"https:\/\/linuxways.net\/?p=7693"},"modified":"2021-06-28T03:06:53","modified_gmt":"2021-06-28T03:06:53","slug":"how-to-enable-ssh-on-centos","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-enable-ssh-on-centos\/","title":{"rendered":"How to Enable SSH on CentOS"},"content":{"rendered":"<p>SSH is a widely used protocol used mostly by system administrators to securely login to remote servers. It allows them the command-line access to work on the remote system as if they are sitting right at the server. In this post, we will describe how you can enable SSH on the CentOS system and connect to it using an SSH client. We will also describe how to configure the SSH server.<\/p>\n<p>For Ubuntu OS, visit this post for <a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-ssh-ubuntu-server-20-04-lts\/\">how to enable SSH on Ubuntu<\/a>.<\/p>\n<p><strong>Note<\/strong>: We have demonstrated the procedure on <strong>CentOS 8 <\/strong>machine<strong>.<\/strong><\/p>\n<h2>Step 1: Installing OpenSSH Server<\/h2>\n<p>First, you will have to install an OpenSSH server on the machine you want to remotely access via SSH. Use the command below to install the OpenSSH server on your CentOS machine:<\/p>\n<pre>$ sudo dnf install openssh-server<\/pre>\n<p>Enter sudo password and then you will see the installation started. Press <strong>y<\/strong> if prompted for confirmation.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"783\" height=\"532\" class=\"wp-image-7694\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-790.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-790.png 783w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-790-300x204.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-790-768x522.png 768w\" sizes=\"auto, (max-width: 783px) 100vw, 783px\" \/><\/p>\n<p>This should install the OpenSSH server on your system.<\/p>\n<h2>Step 2: Start SSH Daemon and Verify Status<\/h2>\n<p>Now that SSH has been installed, you can start its service. Use the command below to start SSH daemon:<\/p>\n<pre>$ sudo systemctl start ssh<\/pre>\n<p>Then to verify if the SSH daemon has started, use the command below:<\/p>\n<pre>$ sudo systemctl status sshd<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"896\" height=\"135\" class=\"wp-image-7695\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-791.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-791.png 896w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-791-300x45.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-791-768x116.png 768w\" sizes=\"auto, (max-width: 896px) 100vw, 896px\" \/><\/p>\n<h2>Step 3: Allow SSH through Firewall<\/h2>\n<p>If the firewall is enabled on your CentOS system, it will block the incoming SSH traffic. To permit incoming SSH traffic, you will need to add a rule in the firewall. The default port used by SSH is 22. So the rule should open port 22.<\/p>\n<p>Use the command below to allow incoming SSH traffic on port 22:<\/p>\n<pre>$ sudo firewall-cmd --permanent --add-port=22\/tcp<\/pre>\n<p>This command will add a rule in your firewall that will allow traffic on port 22. If you have configured SSH for a port other than 22, make sure to use that port in the firewall rule.<\/p>\n<h2>Step 4: Test SSH access<\/h2>\n<p>To test if SSH is working and you can connect to it, open your client machine. This machine should have an SSH client. If you are using a CentOS client, you can install SSH client as follows:<\/p>\n<pre>$ sudo dnf install openssh-clients<\/pre>\n<p>Here we are going to test it using our Ubuntu client machine.<\/p>\n<p>To connect to an SSH server, you will require an IP address and a username of the remote system:<\/p>\n<pre>$ ssh remote_username@remote_ip_address<\/pre>\n<p>For instance, in our case, the IP address of the remote machine (where the SSH server is installed) was \u201c192.168.72.192\u201d and the username was \u201ckbuzdar\u201d, so we ran the command:<\/p>\n<pre>$ ssh <a href=\"mailto:kbuzdar@192.168.72.192\">kbuzdar@192.168.72.192<\/a><\/pre>\n<p><strong>Note<\/strong>: If you don\u2019t know how to find the IP address of a machine, visit <a href=\"https:\/\/linuxways.net\/de\/centos\/6-ways-to-find-your-private-ip-address-in-centos-8\/\">this<\/a> post.<\/p>\n<p>After you have connected to the SSH server, it will ask you to enter the password. Enter the password and then you will be login to your remote system. Now you will also notice the Terminal prompt has been changed to reflect the remote system username and system name.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"397\" class=\"wp-image-7696\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-792.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-792.png 816w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-792-300x146.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-792-768x374.png 768w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/p>\n<h2>Configuring SSH<\/h2>\n<p>The SSH configuration file is located at <strong>\/etc\/ssh\/sshd_config<\/strong>. This file contains different configuration options which you can change according to your needs. Here we will look at few common options that might need modifications e.g. changing the default SSH port and the listening address, disabling root login, configuring max sessions and password tries, etc.<\/p>\n<p>To configure or change any option, you will need to edit the <strong>sshd_config<\/strong> file:<\/p>\n<pre>$ sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Remember, after changing any settings in the <strong>sshd_config<\/strong> file, make sure to reload the SSH server to implement the changes:<\/p>\n<pre>$ sudo systemctl reload ssh<\/pre>\n<h3>Changing SSH Port<\/h3>\n<p>The SSH by default uses port 22 for listening to incoming connections. If for any reason you want to change the SSH port, you can do so by editing the <strong>sshd_config<\/strong> file and searching for a line that says:<\/p>\n<pre># Port 22<\/pre>\n<p>Uncomment the line by removing the hash (#) and the replace <strong>22<\/strong> with the port number you want to set for SSH.<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" width=\"741\" height=\"249\" class=\"wp-image-7697\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-793.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-793.png 741w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-793-300x101.png 300w\" sizes=\"auto, (max-width: 741px) 100vw, 741px\" \/><\/h3>\n<p>Once done, save and close the sshd_config file.<\/p>\n<h3>Changing Listening Address<\/h3>\n<p>By default, SSH listens on all local interfaces. To configure SSH to listen on only a specific network interface, edit the <strong>sshd_config<\/strong> file:<\/p>\n<pre>$ sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Search for the line that says:<\/p>\n<pre>#ListenAddress 0.0.0.0<\/pre>\n<p>Uncomment the above line and replace 0.0.0.0 with the local address on which you want SSH to listen on.<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"243\" class=\"wp-image-7698\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-794.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-794.png 750w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-794-300x97.png 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/h3>\n<p>Once done, save and close the sshd_config file.<\/p>\n<h3>Disable Root Login<\/h3>\n<p>By default, the <strong>root<\/strong> user is allowed to log in using SSH. This is controlled by the PermitRootLogin parameter in the sshd_config file which is by default \u201cyes\u201d which means root login is allowed.<\/p>\n<p>To disable root login, edit the sshd_config file:<\/p>\n<pre>$ sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Look for the line that says:<\/p>\n<pre><strong># <\/strong>PermitRootLogin no<\/pre>\n<p>Uncomment (if commented) the above line and replace \u201cno\u201d by \u201cyes\u201d to disable root login using SSH.<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" width=\"758\" height=\"202\" class=\"wp-image-7699\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-795.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-795.png 758w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-795-300x80.png 300w\" sizes=\"auto, (max-width: 758px) 100vw, 758px\" \/><\/h3>\n<p>Once done, save and close the sshd_config file.<\/p>\n<h3>Change Number of Allowed Sessions and Authentication Attempts<\/h3>\n<p>By default, the maximum number of sessions allowed per network connection is 10. It can be changed by the <strong>MaxSessions<\/strong> parameter in the <strong>sshd_config<\/strong> file. Similarly, the maximum number of authentication attempts for one login session is 6 which can be changed using the <strong>MaxAuthTries<\/strong> parameter.<\/p>\n<p>To change the number of sessions allowed per network connection, search for the line that says:<\/p>\n<pre># MaxSessions 10<\/pre>\n<p>Uncomment the above line and replace its value with your desired value.<\/p>\n<p>To change the number of authentication attempts allowed per login session, search for the line that says:<\/p>\n<pre># MaxAuthTries 6<\/pre>\n<p>Uncomment the above line and replace its value with your desired value.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"741\" height=\"176\" class=\"wp-image-7700\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-796.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-796.png 741w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-796-300x71.png 300w\" sizes=\"auto, (max-width: 741px) 100vw, 741px\" \/><\/strong><\/p>\n<p>Once done, save and close the sshd_config file.<\/p>\n<h3>Change Grace Time<\/h3>\n<p>When you connect to the SSH server, it gives you 120 seconds (default time) to complete your authentication using a username and password. If you do not login during this time, the server drops the connection and you have to login again. You can set this time a little higher than it takes you to complete login or set it lower to prevent DoS attacks where multiple sessions are kept open for a long time.<\/p>\n<p>To change this time, search for the line that says:<\/p>\n<pre># LoginGraceTime 120<\/pre>\n<p>Uncomment the above line and replace its value with your desired time (in seconds). If you do not want to set any time limit, set it value to 0. <img loading=\"lazy\" decoding=\"async\" width=\"738\" height=\"202\" class=\"wp-image-7701\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-797.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-797.png 738w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-797-300x82.png 300w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/p>\n<p>Once done, save and close the sshd_config file.<\/p>\n<p>In this post, we covered how to enable SSH on a CentOS machine and connect to it using a client machine. We also looked at various configuration options for the SSH server. If you want to make your remote access secure, visit our post on <a href=\"https:\/\/linuxways.net\/de\/centos\/11-best-ways-to-secure-your-ssh-server\/\">11 Best Ways to Secure Your SSH Server<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>SSH is a widely used protocol used mostly by system administrators to securely login to remote servers. It allows them the command-line access to work on the remote&hellip;<\/p>","protected":false},"author":1,"featured_media":7831,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[93,21],"class_list":["post-7693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","tag-centos-8","tag-ssh"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7693","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=7693"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7693\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/7831"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=7693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=7693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=7693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}