{"id":46172,"date":"2024-04-14T14:01:27","date_gmt":"2024-04-14T14:01:27","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=46172"},"modified":"2024-04-14T14:01:27","modified_gmt":"2024-04-14T14:01:27","slug":"install-ssh-ubuntu-2404","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2024\/04\/install-ssh-ubuntu-2404\/","title":{"rendered":"How to Install &#038; Use SSH Service in Ubuntu 24.04"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/04\/ssh-putty-icon.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-38590\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/04\/ssh-putty-icon-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/04\/ssh-putty-icon-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/04\/ssh-putty-icon-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/04\/ssh-putty-icon-600x600.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/04\/ssh-putty-icon-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/04\/ssh-putty-icon.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This simple tutorial shows how to enable and setup Secure Shell (SSH) service in Ubuntu 24.04, so you can login remotely and transfer files securely in command console.<\/p>\n<p>Debian and Ubuntu use OpenSSH (OpenBSD Secure Shell) to provide secure shell channel for making changes, transferring data, and performing other operations on server from a client remotely.<\/p>\n<p>The SSH service is NOT enabled by default. And, here I&#8217;m going to how to enable and configure it in Ubuntu 24.04<\/p>\n<p><!--more--><\/p>\n<h3>Step 1: Install OpenSSH Server<\/h3>\n<p>First connect to your Ubuntu server, or press <code>Ctrl+Alt+T<\/code> to open up a terminal window on Ubuntu Desktop.<\/p>\n<p>When terminal opens, run command to refresh system package cache:<\/p>\n<pre>sudo apt update<\/pre>\n<p>Then, install the SSH server via command:<\/p>\n<pre>sudo apt install ssh<\/pre>\n<p><i>For choice, you may use <code>sudo apt install openssh-server<\/code> command that will do the same thing.<\/i><\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/apt-ssh-noble.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46173\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/apt-ssh-noble-700x508.webp\" alt=\"\" width=\"610\" height=\"443\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/apt-ssh-noble-700x508.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/apt-ssh-noble-300x218.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/apt-ssh-noble-768x558.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/apt-ssh-noble.webp 866w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Step 2: Configure Listening Port, Authentication, etc<\/h3>\n<p>Since Ubuntu 22.10, OpenSSH is configured to use systemd socket activation by default. Meaning that the ssh service will be NOT started until an incoming connection is received.<\/p>\n<p>This can save at least 3MiB of memory, which is useful when running multiple Ubuntu instances in VMs or LXD containers.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-socket.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46174\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-socket-700x506.webp\" alt=\"\" width=\"610\" height=\"441\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-socket-700x506.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-socket-300x217.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-socket-768x555.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-socket.webp 870w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>Due to this change, <code>Port<\/code> and <code>ListenAddress<\/code> etc changes in <code>\/etc\/ssh\/sshd_config<\/code> did NOT work. User had to either edit config files under <code>\/etc\/systemd\/system\/ssh.socket.d<\/code> instead, or <a href=\"https:\/\/discourse.ubuntu.com\/t\/sshd-now-uses-socket-based-activation-ubuntu-22-10-and-later\/30189\" target=\"_blank\" rel=\"noopener\">revert to the previous non-socket-activated behavior<\/a>.<\/p>\n<p><b>In Ubuntu 24.04, user again can edit the <code>\/etc\/ssh\/sshd_config<\/code> file to configure SSH. The only change is re-starting the socket instead of ssh.service for applying changes.<\/b><\/p>\n<p>And, to edit the file, run command:<\/p>\n<pre>sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>When the file opens, remove &#8220;#&#8221; at the beginning for any line you want to enable, then change the value according to your need. For example:<\/p>\n<ul>\n<li>Set <code>Port 12345<\/code> to listen on port 12345 instead of default 22.<\/li>\n<li>Set listening IP address <code>ListenAddress 172.67.181.180<\/code> if more than one available. By default, all the server IP addresses are allowed for SSH connection.<\/li>\n<li><code>LoginGraceTime 30<\/code> &#8211; set 30s timeout waiting for a password.<\/li>\n<li><code>PermitRootLogin no<\/code> &#8211; disable root login.<\/li>\n<li><code>MaxAuthTries 3<\/code> &#8211; max authentication failures.<\/li>\n<li><code>PasswordAuthentication no<\/code> &#8211; disable password login. In the case, you need to enable a key-based authentication.<\/li>\n<\/ul>\n<p>For more, see the official <a href=\"https:\/\/www.ssh.com\/academy\/ssh\/sshd_config\" target=\"_blank\" rel=\"noopener\">SSH documentation<\/a>.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/configure-ssh.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46175\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/configure-ssh-700x508.webp\" alt=\"\" width=\"610\" height=\"443\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/configure-ssh-700x508.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/configure-ssh-300x218.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/configure-ssh-768x558.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/configure-ssh.webp 866w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Step 3: Apply SSH Server Config Changes<\/h3>\n<p>After making changes, press Ctrl+S to save and Ctrl+X to exit for nano command line text editor.<\/p>\n<p>Then, as mentioned, restart the socket instead to apply changes:<\/p>\n<pre>sudo systemctl daemon-reload<\/pre>\n<pre>sudo systemctl restart ssh.socket<\/pre>\n<p>In my case, I change the port number to 12345, which can be verified by running <code>systemctl status ssh.socket<\/code> command.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/restart-ssh-socket.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46176\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/restart-ssh-socket-700x508.webp\" alt=\"\" width=\"610\" height=\"443\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/restart-ssh-socket-700x508.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/restart-ssh-socket-300x218.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/restart-ssh-socket-768x558.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/restart-ssh-socket.webp 866w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Step 4: Open the listening port in firewall<\/h3>\n<p>If you changed the SSH port and enabled firewall (<i>firewall is NOT enabled by default in Ubuntu Desktop<\/i>), then run command to allow the new port:<\/p>\n<pre>sudo ufw allow 12345<\/pre>\n<p><i>Change number 12345 to yours. Then, verify by running command:<\/i><\/p>\n<pre>sudo ufw status verbose<\/pre>\n<h3>Step 5: Use SSH to Login Remotely or Transfer Data<\/h3>\n<p>Finally, you can run the ssh command to connect to a remove server. For example:<\/p>\n<pre>ssh ji@192.168.0.107 -p 22345<\/pre>\n<p>In the command:<\/p>\n<ul>\n<li><code>ji<\/code> is the username in remote server here I&#8217;m trying to log in with. Replace to yours.<\/li>\n<li><code>192.168.0.107<\/code> is the remote server&#8217;s IP address. Replace it to yours.<\/li>\n<li><code>-p 22345<\/code> specifies the port. Change the number, or skip it if the default port 22 in use.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-login.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46178\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-login-700x475.webp\" alt=\"\" width=\"610\" height=\"414\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-login-700x475.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-login-300x203.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-login-768x521.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/ssh-login.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>To upload file from local to remote SSH server, use command for example:<\/p>\n<pre>scp -P 22345 ~\/Downloads\/librivox.zip ji@192.168.0.107:~\/Documents<\/pre>\n<p><i>In the case, it will upload the <code>librivox.zip<\/code> file from my local Downloads folder to remote server&#8217;s Documents folder for <code>ji<\/code>.<\/i><\/p>\n<p>To download file from remote, run command for example:<\/p>\n<pre>scp -P 22345 ji@192.168.0.107:~\/Documents\/librivox.zip ~\/Downloads<\/pre>\n<p><i>It will download the <code>librivox.zip<\/code> file from remote ji&#8217;s Documents folder, to my local Downloads folder<\/i>.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/scp-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46179\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/scp-command-700x337.webp\" alt=\"\" width=\"610\" height=\"294\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/scp-command-700x337.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/scp-command-300x144.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/scp-command-768x369.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/04\/scp-command.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>For more about using SSH command, see the <a href=\"https:\/\/www.ssh.com\/academy\/ssh\/command\" target=\"_blank\" rel=\"noopener\">official documentation<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>This simple tutorial shows how to enable and setup Secure Shell (SSH) service in Ubuntu 24.04, so you can login remotely and transfer files securely in command console. Debian and Ubuntu use OpenSSH (OpenBSD Secure Shell) to provide secure shell channel for making changes, transferring data, and performing other operations on server from a client [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":38590,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,1],"tags":[347,24],"class_list":["post-46172","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-ubuntu-server-2","tag-openssh","tag-ssh"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/46172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=46172"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/46172\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/38590"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=46172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=46172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=46172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}