How to Connect to a Server with SSH
Open a terminal session on a remote server with ssh, handle the first-connection host key prompt, use a custom port or key file, and disconnect.
You have a server's address and a login, and you want a shell on it. The ssh command is preinstalled on macOS, Linux, and Windows 10 and later.
Connect with a username and host
ssh user@example.com
Replace user with your account on the server and example.com with its hostname or IP address, such as ssh user@203.0.113.10. If your local username matches the remote one, ssh example.com is enough.
If the server listens on a port other than 22:
ssh -p 2222 user@example.com
Accept the host key on first connection
The authenticity of host 'example.com (203.0.113.10)' can't be established.
ED25519 key fingerprint is SHA256:Jr7v0X3Cq9b2cM8Yk4Ff1sT6qP0wLxZ5vN1aH2dE3gU.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Type yes. SSH records the fingerprint in ~/.ssh/known_hosts and will not ask again for that host. If your provider published the server's fingerprint, compare it before answering.
Authenticate
If the server accepts passwords, it prompts for one; nothing is shown as you type. If it uses key authentication, SSH tries the keys in ~/.ssh automatically (id_ed25519, id_rsa, and others). To point at a specific key file:
ssh -i ~/.ssh/my-server-key user@example.com
A key file must be readable only by you, or SSH refuses to use it:
chmod 600 ~/.ssh/my-server-key
Work on the server
Once connected, the prompt changes to the remote machine's:
user@server:~$
Every command you type now runs on the server, not on your laptop.
Disconnect
exit
Closing the terminal window also ends the session, but exit (or Ctrl+D) is cleaner and confirms you are back on your own machine.
If you connect to the same server often. Create ~/.ssh/config with an entry for it:
Host myserver
HostName example.com
User user
Port 2222
IdentityFile ~/.ssh/my-server-key
After that, ssh myserver applies all of those settings, and scp and rsync recognize the alias too.
If the connection is refused or times out. The server may not be running SSH, the port may be wrong, or a firewall may be blocking it. ssh -v user@example.com prints each step of the connection so you can see where it stops.
More Coding how-tos
- How to Add a .gitignore File
- How to Check Which Node.js Version Is Installed
- How to Check Which Python Version Is Installed
- How to Clone a Git Repository
- How to Create a New Git Branch
- How to Create a Python Virtual Environment
- How to Delete a Git Branch
- How to Install Node.js with nvm
- How to Install Python Packages with pip
- How to Make a Script Executable in the Terminal
- How to Print Hello World in JavaScript
- How to Print Hello World in Python
- How to Read a JSON File in Python
- How to Resolve a Merge Conflict in Git
- How to Run a Local Web Server in One Command
- How to Set an Environment Variable
- How to Stash Changes in Git
- How to Undo the Last Git Commit
- How to Write Your First Bash Script