Software & OS

How to SSH Into a Server (Windows, macOS and Linux)

To SSH into a server, open a terminal and run ssh username@server-address, then confirm the server's host key the first time and enter your password or key passphrase. The address can be a hostname or an IP address, and the user name must already exist on the server.

ssh [email protected]

Replace alex and server.example.com with your own user name and address. If the login works, your prompt changes to the server's prompt and every command you type now runs on the server.

What you need before you connect

  • The server's address. A hostname such as server.example.com or an IP address. For a cloud VM, it is shown in the provider's console. For a machine on your own network, it is the machine's local IP address.
  • A user name on the server. This is the account you log in as, not your local user name. Providers often tell you the default, and it differs between images and distributions.
  • A credential. Either that account's password or a private key. Keys are the better option, and a later section shows how to set one up.
  • A running SSH server that you can reach. The server needs sshd running, and any firewall between you and it has to allow the SSH port. That port is 22 by default.

If you want the background on what happens during the connection, see what SSH is and how it works. This article sticks to getting logged in.

Step 1: open a terminal with an SSH client

macOS and most Linux distributions include the OpenSSH client, so open Terminal and use ssh directly. Check that it exists with:

ssh -V

On Windows, Microsoft's documentation says OpenSSH is available as a feature on demand beginning with Windows 10 build 1809, and that on Windows 10 it is "not installed, install and enable using optional features". If ssh -V reports that the command is not found in PowerShell or Command Prompt, add the OpenSSH Client under Settings, System, Optional features. After that, the same ssh command works in Windows Terminal, PowerShell and Command Prompt. Git Bash also ships its own copy.

Step 2: connect and accept the host key

Run the command from the top of this article. The first time you connect to a server, ssh has no record of its identity and asks whether to trust it. Example output:

The authenticity of host 'server.example.com (203.0.113.10)' can't be established.
ED25519 key fingerprint is SHA256:EXAMPLEFINGERPRINTxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

If you control the server, or the provider shows the fingerprint in its console, compare it with this one. If they match, type yes. Typing yes saves the key, and later connections check it without asking. Do not type yes on a connection you cannot verify at all, because accepting an unknown key is the one point where someone sitting between you and the server could pose as it.

Step 3: log in with a password or a key

Next, ssh asks for a password. Nothing appears on screen as you type it, not even dots. That is normal. Press Enter when you are done.

If you have a key pair, ssh uses it automatically when it is in one of the default locations. The manual lists the defaults ssh tries: ~/.ssh/id_rsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ecdsa_sk, ~/.ssh/id_ed25519 and ~/.ssh/id_ed25519_sk. If your key has a different name or lives elsewhere, point to it with -i:

ssh -i ~/.ssh/work_key [email protected]

When the key itself is protected by a passphrase, ssh asks for that passphrase instead of the server password. It unlocks the key on your computer and is never sent to the server.

How to SSH into a server on a different port

Some administrators move SSH off port 22. If the connection hangs or is refused on the default port, ask for the right one, then add -p:

ssh -p 2222 [email protected]

The same login can also be written with the user as an option: ssh -l alex server.example.com. The -l option sets the user to log in as on the remote machine, which is the same job the alex@ prefix does.

How to set up key login so you stop typing a password

Password logins work, but keys are easier day to day and harder to guess. The process has two parts: create a key pair on your own computer, then install the public half on the server. Our guide to generating an Ed25519 SSH key covers the first part on all three operating systems.

For the second part, ssh-copy-id is the usual tool on Linux and macOS. The manual describes it as a script that uses ssh to log into a remote machine and install your keys so logins with them are allowed. By default it appends them to ~/.ssh/authorized_keys on the server, creating the file and directory if needed:

ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]

It asks for your password once, because that is how it gets in to install the key. If the server uses another port, add -p 2222. After that, ssh [email protected] logs you in with the key. Windows does not include ssh-copy-id. There, you append the contents of your .pub file to ~/.ssh/authorized_keys on the server by hand, while logged in with your password.

Run one command without staying logged in

Anything after the destination is treated as a command. The manual's synopsis ends with destination [command [argument ...]], and ssh exits when the command finishes:

ssh [email protected] uptime

This is handy in scripts. The manual also says ssh exits with the status of the remote command, or with 255 if an error occurred in ssh itself, so a script can tell a failed command from a failed connection.

How to disconnect

Type exit or press Ctrl+D. If the session is frozen, for example after the network dropped, press Enter first, then type ~ followed by .. The manual says the escape character followed by a dot closes the connection, that the default escape character is ~, and that it is only recognised at the start of a line.

Reaching a server through a jump host

Some servers have no public address and can only be reached through a bastion host. The -J option connects to the target by first making an ssh connection to the jump host:

ssh -J [email protected] alex@internal-host

If you often use the same options, an SSH config file lets you shorten all of this to ssh work.

If you cannot connect

Add -v and run the command again. The manual says verbose mode prints debugging messages about ssh's progress, and repeating the flag raises the detail up to a maximum of 3:

ssh -v [email protected]

The last line before the failure tells you which stage broke. These are the usual cases:

  • Connection timed out. Packets are not getting through. Check the address, that the machine is on, and whether a firewall or cloud security group blocks the SSH port.
  • Connection refused. The machine answered, but nothing listens on that port. The SSH server is not running, or it uses a port other than 22.
  • Permission denied. You reached the server, but it rejected your credentials. Check the user name, retype the password, and make sure the public key is in authorized_keys for that user. With Permission denied (publickey), the server accepts only keys, so a password will never work.
  • Host key verification failed. The key the server presented does not match the one you stored earlier, or ssh was not allowed to prompt. The manual says that if a host's identification ever changes, ssh warns about it and disables password authentication. A reinstalled or rebuilt server has a new key, which is harmless, but confirm that with whoever runs it before removing the old entry with ssh-keygen -R server.example.com.

Connecting to the wrong machine is another common cause. Cloud providers often reassign IP addresses, so an address that used to be your server can belong to a different one.

Frequently asked questions

What do I need before I can SSH into a server?
You need three things: the server's hostname or IP address, a user name that exists on that server, and a way to prove who you are, either that user's password or a private key whose public half is installed on the server. The server must also be running an SSH server (sshd) and be reachable on its SSH port, which is 22 unless the administrator changed it.
How do I SSH into a server on a different port?
Use the -p option: ssh -p 2222 [email protected]. The ssh manual describes -p as the port to connect to on the remote host. If you connect often, put the port in your SSH config file so you do not have to type it.
How do I SSH into a server with a key file?
Use the -i option to name the private key: ssh -i ~/.ssh/id_ed25519 [email protected]. Without -i, ssh tries the default key files in ~/.ssh, such as id_ed25519 and id_rsa. The matching public key must be listed in ~/.ssh/authorized_keys on the server.
How do I leave an SSH session?
Type exit or press Ctrl+D to end the shell, and the connection closes with it. If the session is frozen, press Enter, then type ~ followed by a dot. The default escape character is ~, and it is only recognised at the start of a line.

Sources