How to Connect to a Linux Server with SSH

Connect securely to a remote Linux host using username, hostname, port and SSH key.

SSH opens an encrypted remote shell. The basic form is username plus hostname, with options for ports, keys and diagnostics.

Step by step

1 Connect normally

$ ssh user@server.example.com

The default SSH service uses TCP port 22 unless configured otherwise.

2 Use another port

$ ssh -p 2222 user@server.example.com

Lowercase -p selects the destination port.

3 Use a key

$ ssh -i ~/.ssh/id_ed25519 user@server

The -i option selects a private identity file.

4 Run one remote command

$ ssh user@server "uname -a"

SSH can execute a command and then exit.

Tips worth knowing

Frequently asked questions

What is the SSH command format?

Use `ssh user@hostname`, optionally with `-p PORT` and `-i KEY`.

Why do I get Permission denied (publickey)?

The server rejected the key or account/key configuration.

If it goes wrong

Permission denied (publickey)

SSH rejected your login because no key you offered was accepted by the server.

Connection refused

You reached the machine, but nothing was listening on that port — or a firewall said no.

The commands behind it

ssh

Log into remote machines, run remote commands, use keys instead of passwords, and tunnel ports.

scp

The quickest way to move a file to or from a remote machine.

rsync

Sync directories locally or over SSH, transferring only what changed.

Concepts involved: SSH key

More how-to guides

Check disk space in Linux

See how much space is left, and find out what is using it.

Extract a .tar.gz archive

Unpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.

Free a port that is already in use

Find which process holds a port and stop it cleanly.

Find and replace text in files

Replace text in one file or across a whole project, safely.