How to Use the ssh Command to Connect to a Remote Server

Published 2026-08-05 · Learn Linux

The ssh command opens a secure, encrypted connection to a remote computer over the network. You use it to run commands on a server as if you were sitting in front of it. The basic form is ssh [user]@[host] — after you connect, everything you type is sent to the remote machine. This guide covers the syntax you need for everyday server work.

The basic ssh syntax

To connect to a server, give ssh the username you want to log in as and the hostname or IP address. If the username is the same on both machines you can leave the user@ part out.

$ ssh [user]@[host]

The first time you connect, ssh asks you to accept the host key. Type yes and then enter your password. On success you see the remote shell prompt, for example user@server:~$. See the ssh reference page for every option.

Connect on a different port

Many servers listen for SSH on a non-standard port to cut down on automated attacks. Use -p to pick the port.

$ ssh -p 2222 [user]@[host]

If you connect to the same port constantly, add it to ~/.ssh/config so a bare ssh my-server works.

Run a single command over SSH

You do not always need an interactive session. Pass a command after the host and ssh runs it remotely, prints the output, and closes the connection.

$ ssh [user]@[host] uptime
$ ssh [user]@[host] uname -a

This is how automation scripts check a service, read a log or trigger a deployment on several machines from one command line.

Connect with an SSH key

SSH keys replace passwords with a key pair. Your public key sits on the server and your private key stays on your laptop. Generate a key with ssh-keygen, then point ssh at it with -i.

$ ssh -i ~/.ssh/id_ed25519 [user]@[host]

Log out and recover from a hung session

End the session cleanly with exit or logout, or press Ctrl+D at the prompt. If the network drops and the session freezes, press Enter, then ~, then . to close the dead connection.

$ exit

For long-running work that survives disconnects, run your tasks inside tmux.

Keep learning

Related guides: How to Set Up SSH Keys for Passwordless Login · How to Check Open Ports with ss and netstat · How to Sync Files with rsync. Read all tutorials on the Learn Linux blog.