{"id":362,"date":"2020-10-08T13:46:18","date_gmt":"2020-10-08T13:46:18","guid":{"rendered":"https:\/\/linuxways.net\/?p=362"},"modified":"2020-11-13T22:14:58","modified_gmt":"2020-11-13T22:14:58","slug":"how-to-ssh-ubuntu-server-20-04-lts","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-ssh-ubuntu-server-20-04-lts\/","title":{"rendered":"How to use SSH to connect to a remote server in Ubuntu 20.04 LTS"},"content":{"rendered":"<p>SSH (stands for secure shell) is a protocol used for securely accessing a remote system. It is the most commonly used protocol in Linux systems for remotely administering, managing, and troubleshooting the remote servers.<\/p>\n<p>This article is about how to remotely connect to Linux Server over SSH. We will use the OpenSSH utility for remote connection over SSH.<\/p>\n<h2>Prerequisites<\/h2>\n<p>Following are some prerequisites that you must be completed for the remote connection over SSH:<\/p>\n<ul>\n<li>Two Ubuntu machines (for remote server and client)<\/li>\n<li>Sudo user on both server and client machine<\/li>\n<li>Remote server\u2019s IP address or hostname<\/li>\n<li>Remote server\u2019s port number, user name, and password<\/li>\n<\/ul>\n<h2>SSH Linux Server<\/h2>\n<p>In order to SSH Linux system, the steps involved are:<\/p>\n<ol>\n<li>Installing OpenSSH on remote server<\/li>\n<li>Configuring OpenSSH on remote server<\/li>\n<li>Installing OpenSSH client on the local client machine<\/li>\n<li>Accessing remote Linux server through SSH client<\/li>\n<\/ol>\n<p><strong>Note:<\/strong> The commands discussed here have been tested on Ubuntu 20.04 LTS.<\/p>\n<h3>Step 1: Installing OpenSSH on a remote server<\/h3>\n<p>OpenSSH is a tool for remotely connecting the system over SSH. You will need to install it on the system which you want to access over SSH.<\/p>\n<p>On the remote server, open the Terminal and issue the following command:<\/p>\n<pre>$ sudo apt update<\/pre>\n<p>Then in order to install the OpenSSH server, issue the following command in Terminal:<\/p>\n<pre>$ sudo apt install openssh-server<\/pre>\n<p>Then type sudo password.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"254\" class=\"wp-image-363\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-175.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-175.png 737w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-175-300x103.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/p>\n<p>When asked for confirmation, hit <strong>y<\/strong>, after which the system will start the installation.<\/p>\n<h3>Step 2: Configuring OpenSSH on remote server<\/h3>\n<p>Once the installation of OpenSSH is completed, you will need to perform some necessary configurations. The SSH configuration file is <strong>\/etc\/ssh\/sshd_config<\/strong>. To edit the configuration file, issue the following command in Terminal:<\/p>\n<pre>$ nano \/etc\/ssh\/sshd_config<\/pre>\n<p>This is how the OpenSSH configuration file looks like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"924\" height=\"630\" class=\"wp-image-364\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-176.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-176.png 924w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-176-300x205.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-176-768x524.png 768w\" sizes=\"auto, (max-width: 924px) 100vw, 924px\" \/><\/p>\n<p>Before making any changes to this file, it is better to make a copy of the original configuration file using the following command:<\/p>\n<pre>$ sudo cp \/etc\/ssh\/sshd_config \/etc\/ssh\/sshd_config.original<\/pre>\n<p>We will only configure some of the options while leaving the rest of the options to default.<\/p>\n<h4>Changing SSH listening port<\/h4>\n<p>By default, OpenSSH runs on TCP port 22. However, if you want the OpenSSH server to listen on some other port (Between port numbers 1024 \u2013 65535), you can do so as follows:<\/p>\n<p>Edit the <strong>\/etc\/ssh\/sshd_config<\/strong> configuration file:<\/p>\n<pre>$ sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Locate \u2018<strong>port 22\u2019<\/strong> in the file. You can use Ctrl+W to search it.<\/p>\n<p>Replace <strong>22<\/strong> by the port number you want your OpenSSH server to listen to. Also, remove the <strong>#<\/strong> symbol before the line.<\/p>\n<p>For instance, to set OpenSSH listening port to 2244, the line would be:<\/p>\n<pre>port 2244<\/pre>\n<h3><img loading=\"lazy\" decoding=\"async\" width=\"792\" height=\"492\" class=\"wp-image-365\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-177.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-177.png 792w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-177-300x186.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-177-768x477.png 768w\" sizes=\"auto, (max-width: 792px) 100vw, 792px\" \/><\/h3>\n<p>Now save the configuration file.<\/p>\n<h4>Enabling root login on the SSH server<\/h4>\n<p>By default, root login is disabled in the OpenSSH server which means you cannot log in as a root user. If you need to log in as a root user to your remote SSH server, you will need to enable root login in Open SSH configuration.<\/p>\n<p>Edit the <strong>\/etc\/ssh\/sshd_config<\/strong> configuration file:<\/p>\n<pre>$ sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Locate <strong>PermitRootLogin<\/strong> <strong>prohibit-password<\/strong> and replace it with the following line. Also, remove the <strong>#<\/strong> symbol before the line.<\/p>\n<pre>PermitRootLogin yes<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"792\" height=\"492\" class=\"wp-image-366\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-178.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-178.png 792w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-178-300x186.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-178-768x477.png 768w\" sizes=\"auto, (max-width: 792px) 100vw, 792px\" \/><\/p>\n<p>Now save the configuration file.<\/p>\n<h4>Reducing number of authentication attempts<\/h4>\n<p>By default, there are 6 authentication attempts allowed to log in to the SSH server. You can reduce these authentication attempts by changing the value of parameter <strong>MaxAuthTries<\/strong>.<\/p>\n<p>For instance, to reduce the number of authentication attempts to <strong>3<\/strong>, edit the <strong>\/etc\/ssh\/sshd_config<\/strong> configuration file:<\/p>\n<pre>$ sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Then locate <strong>MaxAuthTries<\/strong> and change its value to <strong>3<\/strong>.<\/p>\n<p>Also, remove the <strong>#<\/strong> symbol before the line.<\/p>\n<pre>MaxAuthTries 3<\/pre>\n<h3><img loading=\"lazy\" decoding=\"async\" width=\"792\" height=\"492\" class=\"wp-image-367\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-179.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-179.png 792w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-179-300x186.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-179-768x477.png 768w\" sizes=\"auto, (max-width: 792px) 100vw, 792px\" \/><\/h3>\n<p>Now save the configuration file.<\/p>\n<h4>Allow or deny certain users from logging in to SSH server<\/h4>\n<p>By default, all user accounts are allowed to log in to remote system via SSH. In order to allow only certain users to log in to the SSH server, edit the <strong>\/etc\/ssh\/sshd_config<\/strong> file:<\/p>\n<pre>$ sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Then add the following line in this file by replacing <strong>user1<\/strong> and <strong>user2<\/strong> with the name of the users who are allowed to log in.<\/p>\n<pre>AllowUsers user1 user2<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<p>AllowUsers kbuzdar tin<\/p>\n<p>The above line will allow \u201ckbuzdar\u2019 and \u2018tin\u2019 to login via SSH.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"748\" height=\"446\" class=\"wp-image-368\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-180.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-180.png 748w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-180-300x179.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-180-501x300.png 501w\" sizes=\"auto, (max-width: 748px) 100vw, 748px\" \/><\/p>\n<p>Similarly, to deny any user from accessing the SSH server, add the following line in this file by replacing <strong>user1<\/strong> and <strong>user2<\/strong> with the name of the users who are not allowed to log in.<\/p>\n<pre>DenyUsers user1 user2<\/pre>\n<p>The above line will allow all users except \u2018john\u2019 to login via SSH.<\/p>\n<p>Now, save the configuration file.<\/p>\n<p>After making any changes to the configuration file, restart SSH service by using the following command:<\/p>\n<pre>$ sudo service ssh restart<\/pre>\n<h3>Step 3: Installing OpenSSH client on the local client machine<\/h3>\n<p>On the client machine that wants to connect to the Linux server via SSH, we will need to install OpenSSH client utility.<\/p>\n<p>To install OpenSSH client, issue the following command in the client machine&#8217;s Terminal:<\/p>\n<pre>$ sudo apt install openssh-client<\/pre>\n<p>When asked for the password, provide sudo password.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"956\" height=\"258\" class=\"wp-image-369\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-181.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-181.png 956w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-181-300x81.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-181-768x207.png 768w\" sizes=\"auto, (max-width: 956px) 100vw, 956px\" \/><\/p>\n<p>When asked for confirmation, hit <strong>y<\/strong>, after which the system will start the installation. Once the installation is completed, you are ready to use the OpenSSH client.<\/p>\n<h2>Step 4: Accessing Linux server through SSH client over LAN<\/h2>\n<p>Through OpenSSH client, you can connect to the SSH server over LAN either using the IP address or hostname. On the client\u2019s machine, use the following syntax to connect remote server over SSH:<\/p>\n<pre>$ ssh [username]@[remoteserver IP or hostname]<\/pre>\n<p>When prompted for the password, enter the user&#8217;s password. Once the connection is established, you will see the shell prompt for the remote server.<\/p>\n<p>In our case, the remote server\u2019s IP is \u2018192.168.72.157\u2019 and the username is \u2018kbuzdar\u2019. Therefore, the command would be:<\/p>\n<pre>$ ssh <a href=\"mailto:kbuzdar@192.168.72.157\">kbuzdar@192.168.72.157<\/a><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"424\" class=\"wp-image-370\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-182.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-182.png 696w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-182-300x183.png 300w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><\/p>\n<p>In case, you have changed the OpenSSH listening port, then the command to connect to SSH server would be:<\/p>\n<pre>$ ssh -p [port_number] [username]@[ip_address]<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre>$ ssh -p 2244 <a href=\"mailto:kbuzdar@192.168.72.157\">kbuzdar@192.168.72.157<\/a><\/pre>\n<h3>Accessing Linux server through SSH client over the Internet<\/h3>\n<p>In order to connect a remote server over the internet, you will require the Public IP of the remote server and you will also need to set up port forwarding on your router. Here are the steps to how to SSH remote server over the internet:<\/p>\n<p>1. First, you will need to find out the public IP address of the remote server. To do so, issue the following command in the remote machine&#8217;s terminal:<\/p>\n<pre>$ curl ipaddr.pub<\/pre>\n<p>2. Now, you will need to set up forwarding on the router. How to setup it differs from router to router but almost all the routers allow you to do this.<\/p>\n<p>3. Once you find the public IP address of the remote machine and setup port forwarding on the router, use the following syntax to SSH remote Linux machine:<\/p>\n<pre>$ ssh [username]@[public_ip_address]<\/pre>\n<p>In case, the remote server is configured to use a port number other than 22, then the syntax would be:<\/p>\n<pre>$ ssh -p [port_number] [username]@[public_ip_address]<\/pre>\n<p>This is how you can SSH Linux server. We have explained how to SSH remote Linux servers using OpenSSH utility. We have also discussed some basic configurations that you may find useful when connecting via SSH. Now you can easily manage remote Linux servers for administration and troubleshooting.<\/p>","protected":false},"excerpt":{"rendered":"<p>SSH (stands for secure shell) is a protocol used for securely accessing a remote system. It is the most commonly used protocol in Linux systems for remotely administering,&hellip;<\/p>","protected":false},"author":1,"featured_media":1589,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[22,21],"class_list":["post-362","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-remote-access","tag-ssh"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/362","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=362"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/362\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/1589"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}