SSH in Linux: log in to a remote server and use keys

Updated November 1, 2026

ssh opens a secure session on a remote server over the network.

Syntax

ssh user@host

Common cases

ssh alice@server             # log in to the server
ssh -p 2222 alice@server     # a non-standard port
ssh-keygen -t ed25519        # create a key pair
ssh-copy-id alice@server     # put your key on the server
scp file.txt alice@server:~  # copy a file to the server
scp alice@server:~/log.txt . # and back

Sample output

$ ssh alice@localhost 'whoami; hostname'
The authenticity of host 'localhost (::1)' can't be established.
ED25519 key fingerprint is SHA256:64RfudhiZkw/zHcWJKU3s8pc5QhlaGKnmcVgfBSTZKY.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'localhost' (ED25519) to the list of known hosts.
alice
790d71c435e8

How it works: log in with a key, not a password

A key pair is a private key (kept only by you, in ~/.ssh/) and a public key (placed on the server). You sign a challenge from the server with the private key, and the server checks the signature with the public key - so login needs no password and is safer. ssh-copy-id puts the public key into ~/.ssh/authorized_keys on the server. Never show or copy the private key to anyone. Servers you use often go in ~/.ssh/config, so you type a short name instead of a long line.

Common mistakes

ssh checks the permissions on your private key itself and refuses to read it. After chmod 644 ~/.ssh/id_ed25519 the login breaks like this: Permissions 0644 for '/home/alice/.ssh/id_ed25519' are too open., then This private key will be ignored., and at the very bottom alice@localhost: Permission denied (publickey,password). The error you notice is the last line; the cause is three lines above. Fix it with chmod 600 ~/.ssh/id_ed25519 and chmod 700 ~/.ssh. It usually happens after copying keys from another machine or a shared folder.

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! followed by Host key verification failed. more often means the server was reinstalled or its IP was handed to another machine than that someone is intercepting you. ssh prints the fix inside the message: ssh-keygen -f '/home/alice/.ssh/known_hosts' -R 'localhost'. Do not wipe the whole known_hosts, -R removes one entry. Check the new fingerprint with whoever owns the server.

Two similar failures mean different things. bob@localhost: Permission denied (publickey,password). means you reached sshd and it turned you away; the brackets list the login methods it will accept. ssh: connect to host localhost port 2222: Connection refused means nothing is listening on that port at all, and the server software is not involved. You fix the first with a key or a user name, the second with the port and the firewall.

In the book

This topic is covered in full in the chapter SSH and remote management.

See also

Sync: rsync.