Developer & Network Tools

SSH Explained for Beginners

Understand how SSH connects you to a remote machine, set up keys and copy files safely.

Key takeaways

  • SSH gives you an encrypted command line on a remote machine and checks the identity of both ends.
  • The first-connection fingerprint prompt matters; verify it instead of accepting automatically.
  • Key pairs are stronger and more convenient than passwords, and the private key never leaves your side.
  • Protect private keys with a passphrase and use an agent so you type it less often.
  • scp and sftp move files over the same protocol.

SSH, short for Secure Shell, lets you log in to another computer over a network and run commands there, with the whole session encrypted. To connect, you type something like ssh [email protected], confirm that you're talking to the right machine, and authenticate with a password or, better, a key.

It's the standard way to administer Linux servers, and it also carries file copies, Git traffic and secure tunnels.

What happens when you connect

SSH is specified in a family of standards, with RFC 4251 describing its architecture. A server program, commonly OpenSSH's sshd, listens on TCP port 22 by default. Your client connects, and a few things happen before you see a prompt.

Opening an SSH session with a key
Your client SSH server Connect and agree on algorithms 1 Usually port 22 Host key and signed key exchange 2 The client checks it knows this server Offer a public key for user ada 3 That key is allowed: prove it 4 Signature made with private key 5 The private key itself is never sent Access granted, shell starts 6 Your client SSH server Connect and agree on algorithms 1 Usually port 22 Host key and signed key exchange 2 The client checks it knows this server Offer a public key for user ada 3 That key is allowed: prove it 4 Signature made with private key 5 The private key itself is never sent Access granted, shell starts 6

The first stage sets up encryption with a key exchange, similar in spirit to the one shown in Diffie-Hellman key exchange in pictures. Only then do you prove who you are, so your credentials travel inside the protected channel.

Getting a client

macOS and Linux include the OpenSSH client. Current versions of Windows include it too, usable from PowerShell or Command Prompt; if ssh isn't recognized, it can be added through the system's optional features, whose menu names vary by version. Graphical clients such as PuTTY are another option.

ssh [email protected]
ssh -p 2222 [email protected]
ssh [email protected] "uptime"

The part before @ is the account on the remote machine. -p chooses a different port. A quoted command at the end runs that one command and returns.

Inside a session you're working on the remote computer. Type exit or press Ctrl+D on an empty line to leave. It's worth checking the prompt or running hostname before any destructive command.

The fingerprint prompt

On first contact the client shows the server's host key fingerprint and asks whether to continue. This is your chance to confirm that you reached the intended machine and not an impostor. Compare it with a fingerprint obtained another way, for example from the hosting provider's console or from an administrator.

When you accept, the key is saved in ~/.ssh/known_hosts. Later connections compare against it silently.

If you ever see a loud warning that the host identification has changed, stop. There may be a harmless reason, such as a rebuilt server or a reused address, but interception looks the same. Verify first. Only then remove the old entry:

ssh-keygen -R server.example.com

Keys instead of passwords

A key pair has two halves. The public key can be shared and is placed on the server. The private key stays on your device. Symmetric and public-key encryption explains why this works.

ssh-keygen -t ed25519 -C "ada laptop"

This creates a private file, usually ~/.ssh/id_ed25519, and a public one ending in .pub. When asked, set a passphrase. It encrypts the private key, so a stolen laptop or copied file is far less useful to a thief. If a very old server doesn't support Ed25519 keys, RSA with a long key length is the common fallback.

To install the public key, where ssh-copy-id is available:

ssh-copy-id [email protected]

Otherwise, append the single line from the .pub file to ~/.ssh/authorized_keys on the server. That directory and file need restrictive permissions, typically 700 for .ssh and 600 for authorized_keys, or the server may ignore them.

Password login and key login
Password Nothing to set up Can be guessed if weak or reused A fake server could capture it Typed at each login Key pair One-time setup per server Not practical to guess Private key stays on your device Works with an agent and automation VS Password Nothing to set up Can be guessed if weak or reused A fake server could capture it Typed at each login Key pair One-time setup per server Not practical to guess Private key stays on your device Works with an agent and automation

Basic commands once you're in

A remote Linux shell works like a local terminal. A handful of commands covers most first sessions:

Command What it does
whoami Shows which account you're using
hostname Shows which machine you're on
pwd and ls -l Show the current folder and its contents
cd /var/log Moves to another folder
less file.log Reads a file one screen at a time; q quits
df -h Shows free disk space
sudo command Runs one command with administrator rights, if allowed

Work as an ordinary user and use sudo only for the steps that need it. Logging in directly as the all-powerful root account makes every typing mistake more expensive and makes logs less informative about who did what.

Running commands without logging in

Because SSH can run a single command and return its output, it fits neatly into scripts:

ssh web "df -h /"
ssh web "tail -n 50 /var/log/app.log" > recent.log

Quoting decides where things happen. In the second line, the redirection sits outside the quotes, so the file is created on your own computer. Inside the quotes, it would be created on the server. For unattended jobs, use a dedicated key with the narrowest permissions that work. An authorized_keys line can restrict what a key may do, for instance forcing one command or limiting source addresses; the sshd manual describes the options.

Using an agent and a config file

An SSH agent holds your unlocked key in memory so you enter the passphrase once per session:

ssh-add ~/.ssh/id_ed25519

Most desktop systems start an agent for you or can be set to.

A config file at ~/.ssh/config saves typing:

Host web
    HostName server.example.com
    User ada
    Port 22
    IdentityFile ~/.ssh/id_ed25519

Now ssh web does the rest. The same alias works with scp, sftp and Git.

Copying files

scp report.pdf [email protected]:/home/ada/
scp [email protected]:/var/tmp/app.log .
scp -r site/ [email protected]:/home/ada/site/
sftp [email protected]

The colon separates the remote host from the path. Note that scp takes -P (capital) for a port, while ssh takes -p. sftp opens an interactive session with commands such as ls, get and put. Where installed on both sides, rsync over SSH transfers only changes, which helps with repeated deployments.

SFTP is a different protocol from the older FTP, which sends passwords and data unencrypted unless TLS is added. If you publish a site this way, deploying a website from laptop to live shows other options too. Hosting services for Git also commonly accept SSH keys.

Port forwarding in brief

SSH can carry other traffic. Say a database on your server listens only on its own loopback address. This forwards a port on your computer to it through the encrypted session:

ssh -L 15432:127.0.0.1:5432 [email protected]

While connected, a program on your machine can reach the database at 127.0.0.1:15432, and nothing new is exposed to the internet. Use forwarding only where you're permitted to; many organizations have rules about tunnels.

When it doesn't work

  • Connection timed out: wrong address, a firewall, or no route. Try testing the port.
  • Connection refused: nothing listening there; check the service and port.
  • Permission denied (publickey): wrong user, wrong key, public key not installed, or bad file permissions on the server.
  • Unprotected private key file: the key is readable by others; tighten it with chmod 600.

Run ssh -v to see each step, including which keys are offered. The server's authentication log usually states why it said no.

Long-idle sessions sometimes drop because a router forgets them. Setting ServerAliveInterval 60 in your config sends a periodic keepalive. For long jobs, a terminal multiplexer such as tmux on the server keeps work running if the connection breaks.

What SSH protects, and what it doesn't

Encryption keeps people on the network from reading or altering your session, and the host key check protects you from being quietly redirected to a different machine. An observer can still see that your address connected to the server's address, when, and roughly how much data moved.

SSH can't help if either end is already compromised. Malware on your laptop could use an unlocked agent, and an attacker with administrator rights on the server can watch what you do there. Keep both ends updated, lock your screen, and treat a server you don't control as a place where your keystrokes may be recorded. When you leave a team or retire a device, remove its public key from every server and hosting account that listed it. Keeping a short inventory of which keys live where turns that from a search into a checklist.

Common questions

Is SSH the same as a VPN?

No. SSH provides logins, file transfer and forwarding of chosen ports. A VPN typically routes a device's network traffic through a tunnel.

Can I use SSH from a phone?

Yes, with a client app from a source you trust. Give the phone its own key, protect it with the device lock, and remove that key from your servers if the phone goes missing.

Staying safe

Never send anyone your private key. Use a separate key per device, so a lost laptop means removing one line from your servers. Review authorized_keys now and then and delete entries you can't account for.

Agent forwarding, the -A option, lets the remote machine ask your agent to sign things while you're connected, so enable it only for hosts you trust fully. If you run a server that faces the internet, expect constant login attempts; SSH security basics covers disabling password and root logins and other hardening. Before changing server settings, keep one working session open and test a new login in another window, so a mistake doesn't lock you out.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Developer & Network Tools →

Latest news on this topic