How to Use SSH to Connect to a Remote Server on Ubuntu
SSH (Secure Shell) is the standard method for signing in to remote Linux servers through an encrypted connection. From an Ubuntu workstation or another client device, run ssh username@server_ip, approve the host key during the first connection, and then authenticate with either a password or, preferably, an SSH key pair.
This guide explains SSH client usage, enabling OpenSSH on Ubuntu 24.04 LTS and 26.04 LTS, key-based authentication, sshd configuration, and common connection problems. For a broader explanation of the protocol, see SSH Essentials: Working with SSH Servers, Clients, and Keys.
Deploy frontend applications from GitHub through an application platform and let the platform handle application scaling.
Version note: The commands below were validated on Ubuntu 22.04 LTS, 24.04 LTS (Noble Numbat), and 26.04 LTS (Resolute Raccoon). On current LTS versions, the systemd unit is ssh.service and can be managed with systemctl … ssh. OpenSSH on Ubuntu 24.04 and 26.04 processes drop-in configuration files from /etc/ssh/sshd_config.d/ before values in the main sshd_config file. Cloud server images frequently include openssh-server by default and may inject your SSH public key when the server is created.
Key Takeaways
- Connect from any client using
ssh user@host. Use-pwhen connecting through a non-default port and-iwhen selecting a particular private key. - On Ubuntu, install the SSH server with
sudo apt install openssh-server, and then runsudo systemctl enable --now ssh. - On 24.04 and 26.04, store custom
sshdoptions in/etc/ssh/sshd_config.d/so package updates do not overwrite them. - Run
sudo sshd -tbeforesudo systemctl reload sshto identify configuration syntax problems and reduce the risk of locking yourself out. - For new keys, prefer
ssh-keygen -t ed25519instead of RSA, and transfer the public key withssh-copy-id. - If UFW is active, permit SSH traffic with
sudo ufw allow OpenSSH. - Once key authentication is working, use
PasswordAuthentication noandPermitRootLogin noon production systems. - If an SSH configuration problem prevents normal access, use your hosting provider’s web-based console for emergency access.
Quick Start: Connect to a Remote Server with SSH in 5 Steps
- Open a terminal.
- On Linux/macOS: open Terminal.
- On Windows: use PowerShell, WSL, or Git Bash.
- Enter the SSH command:
ssh username@your_server_ip
- Check and approve the host fingerprint when it appears during the first connection.
- Authenticate by entering your password or using an SSH key.
- End the connection with
exit.
Successful SSH connection showing the Ubuntu welcome screen and shell prompt.
Enable SSH on Ubuntu 24.04 and 26.04 LTS
Ubuntu Server cloud images commonly include OpenSSH. Ubuntu Desktop often does not, so you may need to install it manually. The procedure is the same for Ubuntu 22.04, 24.04, and 26.04, with relevant differences noted where necessary.
Install and Start the OpenSSH Server
On the system that should accept remote connections:
sudo apt update
sudo apt install openssh-server
sudo systemctl enable --now ssh
Verify that the SSH service is listening:
sudo systemctl status ssh
ss -tlnp | grep ':22'
The expected result is active (running), with port 22 shown in the LISTEN state.
Open the Firewall with UFW
UFW may be disabled on local Ubuntu installations, but it is frequently enabled on servers. When UFW is active, permit SSH before attempting to connect from another system:
sudo ufw allow OpenSSH
sudo ufw status
The OpenSSH application profile corresponds to port 22. If SSH is configured to use another port, explicitly permit that port instead.
Ubuntu Version Differences at a Glance
| Topic | Ubuntu 22.04 LTS | Ubuntu 24.04 / 26.04 LTS |
|---|---|---|
| systemd unit | ssh.service |
ssh.service |
| Config drop-ins | Include /etc/ssh/sshd_config.d/*.conf |
Same; preferred location for custom rules |
| Typical OpenSSH | 8.9.x | 9.6.x (24.04), 10.x (26.04) |
Default PermitRootLogin |
often prohibit-password |
prohibit-password (root access with keys only) |
| Desktop: SSH server preinstalled | No | No |
| Cloud server: SSH on first boot | Usually yes, with your SSH key | Usually yes, with your SSH key |
Official reference: OpenSSH server (Ubuntu Server documentation).
For hardening a VPS for the first time, follow Initial Server Setup with Ubuntu after SSH access is working.
Core SSH Syntax
Use the ssh command to establish a connection to a remote machine.
On Windows, use OpenSSH, which is included with current Windows 10 and Windows 11 versions, or use WSL or Git for Windows for a Bash environment that provides ssh. For PowerShell setup instructions, see Microsoft’s OpenSSH documentation.
On macOS and Linux, the ssh command is normally available from the terminal by default.
Basic syntax:
ssh remote_host
Replace remote_host with the remote system’s IP address or domain name. When your remote account name is different from your local username, include it explicitly:
ssh remote_username@remote_host
After the connection is established, SSH may request your password unless key-based authentication has already been configured. Close the session with:
exit
How Does SSH Work?
SSH creates a connection between a client (ssh) and a server (sshd).
On most Ubuntu installations, sshd starts automatically after openssh-server is installed. If the service has stopped, start it manually:
sudo systemctl start ssh
If the machine cannot be reached through the network, use the out-of-band console supplied by your hosting provider. A browser-based server console is useful when SSH keys, firewall settings, or SSH configuration prevent a normal login.
Access the server through the hosting provider’s browser-based console when emergency access is required without an SSH client.
How to Configure SSH on Ubuntu
SSH server configuration is stored in /etc/ssh/sshd_config. On Ubuntu 24.04 and 26.04, the first line commonly contains:
Include /etc/ssh/sshd_config.d/*.conf
For most directives, OpenSSH uses the first value it encounters. Whenever possible, save your custom settings in a drop-in file such as /etc/ssh/sshd_config.d/99-custom.conf instead of modifying the primary configuration file.
Create a backup before making changes:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F)
Common settings to examine include:
Port 22
Keep port 22 unless you have a documented reason for changing it. Using a nonstandard port can reduce automated scanning noise, but it does not replace SSH keys or firewall controls.
PermitRootLogin prohibit-password
On current Ubuntu LTS releases, direct root login using a password is generally disabled, while root authentication through an SSH key can remain possible. For production environments, use PermitRootLogin no after confirming that a sudo-enabled user can log in with a key.
PubkeyAuthentication yes
PasswordAuthentication yes
KbdInteractiveAuthentication no
Some older instructions use ChallengeResponseAuthentication. Starting with OpenSSH 8.4+, use KbdInteractiveAuthentication instead.
LogLevel INFO
Temporarily change the level to DEBUG while investigating authentication issues, and restore the previous setting afterward.
After changing the configuration, validate it and then reload SSH:
sudo sshd -t
sudo systemctl reload ssh
Leave a second terminal connection open during testing so that you can undo changes if the new configuration prevents access.
How to Use ~/.ssh/config for Multiple SSH Connections
Create or modify ~/.ssh/config on your local computer:
Host dev-server
HostName 192.168.1.10
User devuser
Port 2222
IdentityFile ~/.ssh/dev_key
Then connect with:
ssh dev-server
This is useful when working with multiple users, SSH keys, or ports.
How to Log In with SSH Keys
Password authentication is suitable for testing, but key-based authentication is quicker to use and provides greater resistance to brute-force login attempts.
How SSH Key-Based Authentication Works
You create a private key, which remains on your computer, and a public key, which is placed on the server. The server asks the client to demonstrate that it possesses the private key corresponding to the public key stored in ~/.ssh/authorized_keys.
Create SSH Keys on Ubuntu 24.04 and 26.04
Create the SSH key pair on the computer you connect from, such as your laptop or workstation:
ssh-keygen -t ed25519
Press Enter to keep the default location, ~/.ssh/id_ed25519. You can optionally protect the private key with a passphrase.
For older systems without Ed25519 support, create an RSA key:
ssh-keygen -t rsa -b 4096
Review the permissions:
ls -l ~/.ssh/
Private SSH keys should use mode 600. On the server, authorized_keys should also use 600, while the .ssh directory should use 700.
Copy Your SSH Public Key to the Server
If password authentication is still available:
ssh-copy-id username@remote_host
Many hosting platforms also allow you to add SSH keys when creating a server or later through the provider’s control panel.
SSH Client-Side Options
Useful SSH options include:
Non-default port that must correspond to the Port setting in sshd_config:
ssh -p port_number remote_username@remote_host
Changing the SSH port provides obscurity rather than strong security. For meaningful protection, combine key-based authentication with PermitRootLogin no and appropriate firewall rules.
Execute a single command remotely:
ssh remote_host "command_to_run"
X11 forwarding, when enabled in sshd_config on both systems:
ssh -X remote_host
Verbose debugging:
ssh -vvv remote_username@remote_host
Common SSH Errors and Troubleshooting
| Error | Possible Cause | What to Try |
|---|---|---|
| Connection refused | sshd is stopped or the port is blocked |
sudo systemctl start ssh; sudo ufw allow OpenSSH |
| Permission denied (publickey) | SSH key is missing or permissions are incorrect | chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys |
| Connection timed out | Firewall, incorrect IP address, or network problem | ping / traceroute; verify the hosting firewall and UFW |
| Host key verification failed | The server was rebuilt or its IP address was reused | ssh-keygen -R hostname_or_ip |
| Too many authentication failures | The SSH agent is attempting too many keys | ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519 user@host |
Advanced SSH Issues
| Error | Possible Cause | Advanced Fix |
|---|---|---|
| Connection closed by remote host | MaxAuthTries or an idle timeout |
Review sshd_config; run sudo journalctl -u ssh |
Bad owner or permissions on .ssh |
Permissions are too open | chmod 700 ~/.ssh; use chmod 600 for private keys |
| Authentication refused | Password and SSH key authentication are both disabled | Confirm PubkeyAuthentication yes or temporarily restore password authentication |
| Cannot resolve hostname | DNS typo | Connect using the IP address or correct /etc/hosts |
Monitor SSH logs live on the server:
sudo journalctl -fu ssh
Disable SSH Password Authentication
After confirming that SSH key authentication works correctly, turn off password-only authentication.
Before disabling passwords, verify that your public key exists in ~/.ssh/authorized_keys. Keep your hosting provider’s web console available until you have confirmed that SSH key login succeeds.
On Ubuntu 24.04 and 26.04, create or edit a configuration drop-in:
sudo nano /etc/ssh/sshd_config.d/99-disable-password.conf
Add the following settings:
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
Validate the configuration and reload SSH:
sudo sshd -t
sudo systemctl reload ssh
Test the new configuration from a new terminal window before ending your existing SSH session.
SSH Security Best Practices
- Use Ed25519 or 4096-bit RSA SSH keys and avoid relying only on passwords for publicly accessible servers.
- After setting up a sudo-enabled user, configure
PermitRootLogin no. - Keep
openssh-serverupdated withsudo apt upgrade openssh-server. - Where possible, restrict SSH access to known IP addresses with UFW or hosting-provider firewalls.
- Consider fail2ban as an additional defense against brute-force attempts.
- Transfer files securely through SSH with SFTP/SCP.
How to Use SSH on Windows, Linux, and macOS
Windows: PowerShell, WSL, and Git Bash all support ssh user@server_ip and ssh-keygen.
macOS / Linux: Use the built-in terminal. The same SSH commands apply.
Create an SSH key:
ssh-keygen -t ed25519
SSH FAQs
1. How Do You Use SSH in the Ubuntu Terminal?
Open Terminal and enter:
ssh username@server_ip
Replace username with the account name on the remote machine and server_ip with the server’s public IP address or hostname. During the first connection, type yes to accept the host key. Then provide your password or authenticate with an SSH key when one has been configured.
2. How Do You Set Up SSH on Ubuntu Linux?
On the server, install OpenSSH and enable the service:
sudo apt update
sudo apt install openssh-server
sudo systemctl enable --now ssh
If UFW is enabled, run sudo ufw allow OpenSSH. On the client computer, create a key with ssh-keygen -t ed25519, and then use ssh-copy-id user@server_ip to copy the public key to the server.
3. How Do You Enable OpenSSH on Ubuntu?
The SSH server package is openssh-server, while client utilities are included in openssh-client, which is commonly already installed. Install the server package using apt, start ssh.service, and check its status with sudo systemctl status ssh. On Ubuntu Desktop 24.04 or 26.04, the SSH server must be installed before remote login is available.
4. How Do You Check Whether SSH Is Enabled on Ubuntu?
sudo systemctl status ssh
Check for active (running). Also confirm that the SSH port is listening:
ss -tlnp | grep ':22'
From another computer, test the connection with ssh -v user@host or nc -zv host 22.
5. Why Is SSH Not Working on Ubuntu?
Typical causes include:
openssh-serveris not installed, particularly on Ubuntu Desktop. Install it withapt.sshdis not running. Start it withsudo systemctl start ssh.- The firewall blocks port 22. Run
sudo ufw allow OpenSSHor permit the SSH port through your hosting firewall. - The IP address, username, or SSH key is incorrect. Run
ssh -vvvto view additional diagnostic information. - The
sshd_configconfiguration is invalid. Runsudo sshd -tand correct any reported errors before reloading SSH.
If SSH is unavailable while the virtual machine is still running, use the hosting provider’s server console.
Conclusion
SSH continues to be the standard method for administering Linux systems remotely because it provides encryption, has low overhead, and works across cloud environments as well as local networks. On Ubuntu 24.04 and 26.04, install openssh-server, keep custom settings in sshd_config.d, favor Ed25519 SSH keys, and validate configuration changes with sshd -t before reloading the SSH service.


