How to Enable SSH (Secure Shell) in Ubuntu 22.04 [Updated]

Image by Pete Linforth

This is a beginner’s guide shows how to install and setup SSH server and client in Ubuntu 22.04 LTS

SSH, also known as Secure Shell or Secure Socket Shell, is one of the most have network services for securely remote login, file transfer. Ubuntu includes OpenSSH is its universe repositories. It’s the open-source version of the SSH tools used by administrators of Linux and other non-Windows.

Install OpenSSH in Ubuntu:

Install OpenSSH:

Ubuntu ships with the SSH client out-of-the-box. To install SSH server along with SFTP access from remote machines, open terminal and run command:

sudo apt install ssh

Start, restart, stop SSH service:

After installed the network services, it should run silently in the background. To check out the status, run command:

systemctl status sshd.service
SSH Service is running

You can replace status in the code with reload, restart, start, or stop to do relevant actions. For instance, the command below will reload the configuration:

systemctl reload sshd.service

Change SSH server configuration:

To configure the SSH server rules, e.g., listening port, allowed users, etc, open terminal and run command to edit the /etc/ssh/sshd_config file with nano text editor.

sudo nano /etc/ssh/sshd_config

When the file opens, you can do:

  • Change listening port by removing # at the beginning of line “#Port 22” and changing the number.
  • Add user authentication by AllowUsers username1 username2 usernameX. By default, all users is allowed to login. If specified, only users in the list can login.
  • Disable root login via “PermitRootLogin no”
  • Specify the local addresses ssh should listen on via ListenAddress.

And there are many more other configuration rules available. You can get the documentation by running command:

man sshd_config

After saving the changes via Ctrl+X keyboard shortcut, follow with Y, and Enter, remember to reload the SSH service by running the previous systemctl command.

Connect to Remote Server via SSH:

In case you don’t even know how to login remote server via SSH, you may run the command below in local machine to make connection:

ssh [email protected]_server_ip -p 1234

Here, you need to replace username with username in your remote server. Of course, replace the “remote_server_ip” with the IP address, and ‘1234’ with port number. For the default listening port, use command:

ssh [email protected]_server_ip

For the first time, you need to answer ‘yes’ to confirm connection, and then input the password of the remote user to get pass.

To transfer a file from remote server to local machine in current directory, use command:

scp -P 1234 [email protected]_server_ip:/PATH/TO/FILE ./

And, upload a file from local machine to remote server (for example, to remote user’s home directory) via:

scp -P 1234 /PATH/TO/FILE [email protected]_server_ip:~/

Skip -P 1234 if the default port 22 is in use.

Merilyn Ne
Hi, I'm Merilyn Ne, a computer geek working on Ubuntu Linux for many years and would like to write useful tips for beginners. Forgive me for language mistakes. I'm not a native speaker of English.