SSH, or Secure Shell, is the standard protocol for secure remote access on Linux. It encrypts all data transferred between your machine and the remote host.
This guide walks you through installing and enabling SSH on Ubuntu 24.04, configuring UFW, connecting remotely, and setting a custom port if needed.
Quick Answer
Run sudo apt install ssh to install it, then sudo systemctl enable –now ssh to enable and start the service, and sudo ufw allow ssh to open the firewall port.
Method 1: Install and Enable SSH on Ubuntu 24.04
Step 1: Install the SSH Package
Install the OpenSSH server and client on Ubuntu 24.04 with the following command. The -y flag skips the confirmation prompt automatically.
sudo apt install ssh -y

Step 2: Enable and Start the SSH Service
Enable the SSH service to start automatically on boot and activate it immediately with the systemctl enable command shown below.
sudo systemctl enable ssh.service

Step 3: Check SSH Status
Verify the SSH service is running by checking its status. The Active line should show active (running) if the service started correctly.
sudo systemctl status ssh

If the Active status shows inactive (dead), start SSH manually using the init.d script as shown in the command below.
sudo /etc/init.d/ssh start
Step 4: Configure UFW to Allow SSH
Enable the UFW firewall and add a rule to allow incoming SSH connections. Run all three commands in sequence to check status, enable UFW, and permit SSH.
sudo ufw status
sudo ufw enable
sudo ufw allow ssh

Method 2: Connect to a Remote System via SSH
Step 1: Get the Remote Machine’s IP Address
On the machine you want to connect to, run ip a to display all network interfaces and their IP addresses. Note the inet address under your active interface.
ip a

Step 2: Initiate the SSH Connection
From your local machine, use the ssh command with the remote username and IP address to start an encrypted connection to the target system.
ssh [email protected]

The remote terminal is now active. Run commands on the target machine as if you were physically there. To end the session, type exit and press Enter.
exit
Method 3: Change the Default SSH Port on Ubuntu 24.04
SSH uses port 22 by default. Switching to a non-standard port reduces the volume of automated brute-force login attempts from bots scanning the internet.
Step 1: Back Up the SSH Config File
Before editing sshd_config, create a backup so you can restore it if something goes wrong. Use the mv command shown below to revert if needed.
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
To restore the original config file at any time, run sudo mv /etc/ssh/sshd_config.bak /etc/ssh/sshd_config and then restart the SSH service.
Step 2: Set a Custom Port in sshd_config
Open sshd_config in nano, find the commented Port 22 line, remove the # sign, and replace 22 with your chosen port number. This example uses 33000.
sudo nano /etc/ssh/sshd_config

Step 3: Update UFW Rules and Restart SSH
Remove the old SSH firewall rule, add a new rule for your custom port, then restart SSH so the daemon reads the updated configuration file.
sudo ufw delete allow ssh
sudo ufw allow 33000
sudo systemctl restart ssh
Connect to the remote host using the -p flag followed by your custom port number. Replace 33000 and the IP address with your actual values.
ssh -p 33000 [email protected]