In one minute
- SSH (Secure Shell) lets you log in to another machine and run commands on it, over an encrypted connection.
- It is how you log in to Linux servers, how Git pushes code to GitLab, how Ansible sets up machines, and how CI pipelines deploy.
- You make a key pair: a private key (stays secret on your laptop) and a public key (copied to the server). When you connect, the server checks that you hold the matching private key. No password is sent.
- 👉 Every time you work on a server, push code over SSH, or reach a private database through a tunnel.
Think of it like: a padlock and its key. You hand out copies of the open padlock (public key) to every server. Only your key (private key) can open them. Anyone may see the padlock. Nobody else gets your key.
Connecting
👉 Logging in to a server, or running one command on it.
ssh ubuntu@10.0.1.25ssh user@host # default port 22ssh -p 2222 user@host # custom portssh -i ~/.ssh/prod.pem user@host # a specific keyssh user@host "df -h" # run one command and returnThe first connection: SSH shows the server's host key fingerprint (the server's ID) and asks you to confirm. It saves it in ~/.ssh/known_hosts. If it ever changes unexpectedly, you get a loud warning. Either the server was rebuilt, or someone is in the middle of your connection. Find out which before you continue.
Key-based login
Logging in with a key pair instead of a password.
Keys are far harder to guess or steal than passwords, and they let scripts log in without typing.
| Part | File | Who has it |
|---|---|---|
| Private key | ~/.ssh/id_ed25519 | Only you. Never share it, never commit it |
| Public key | ~/.ssh/id_ed25519.pub | Copied to every server you log in to |
Remember it: .pub = public = OK to share. No
.pub= private = never share.
On the server, each user's allowed public keys are listed in ~/.ssh/authorized_keys.
ssh-keygen -t ed25519 -C "leela@laptop"Generating public/private ed25519 key pair.Your identification has been saved in /home/lynuxpro/.ssh/id_ed25519Your public key has been saved in /home/lynuxpro/.ssh/id_ed25519.pub- ed25519 is the modern default: small, fast and secure. Use
-t rsa -b 4096only for very old systems. - Set a passphrase. Then a stolen key file is useless on its own.
Copy your public key to a server:
ssh-copy-id ubuntu@10.0.1.25# or by hand:cat ~/.ssh/id_ed25519.pub | ssh ubuntu@10.0.1.25 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"Permissions SSH insists on
| Path | Mode |
|---|---|
~/.ssh | 700 |
~/.ssh/id_ed25519 (private) | 600 |
~/.ssh/authorized_keys | 600 |
~/.ssh/*.pub | 644 |
If others can read your private key, SSH refuses it with "UNPROTECTED PRIVATE KEY FILE". Fix it with chmod 600.
ssh-agent: type the passphrase once
ssh-agent holds your unlocked key in memory.
👉 So you type your passphrase once per session, not on every connection.
eval "$(ssh-agent -s)"ssh-add ~/.ssh/id_ed25519ssh-add -l # list loaded keysThe config file: ~/.ssh/config
A file of short names and settings for the servers you use.
It saves you typing long commands with IPs, users and key paths.
Host bastion HostName 3.110.20.15 User ubuntu IdentityFile ~/.ssh/prod.pemHost orders-* User ubuntu ProxyJump bastion IdentityFile ~/.ssh/prod.pemHost gitlab.com IdentityFile ~/.ssh/gitlab_ed25519Now ssh orders-1 just works, and it goes through the bastion automatically.
Bastion (jump) hosts
A bastion is a single, well-protected server that you log in through to reach private servers.
Private servers have no public IP, so you cannot reach them directly from the internet. That is good for security.
ssh -J ubuntu@bastion ubuntu@10.0.48.15-J (ProxyJump) connects through the bastion without copying your private key onto it. Never put private keys on a bastion.
Think of it like: the reception desk of a secure building. You sign in at reception (bastion), then walk to the office you need (private server).
In AWS: SSM Session Manager often replaces bastions completely. Port 22 stays closed, access is controlled by IAM, and every session is logged. Many companies now require it.
Copying files
scp app.tgz orders-1:/tmp/rsync -avz ./dist/ orders-1:/opt/app/Both use your SSH config, so short names and jump hosts just work.
Tunnels (port forwarding)
A tunnel sends a port on your laptop through SSH to a service on the other side.
👉 You need to reach a private database or dashboard as if it were running on your laptop.
ssh -L 5432:db.internal:5432 bastion# now connect your DB tool to localhost:5432| Flag | Direction | Use |
|---|---|---|
-L local:target:port | Local → remote | Reach a private database or dashboard |
-R remote:target:port | Remote → local | Show your laptop's port to the server (be careful) |
-D 1080 | Dynamic SOCKS proxy | Browse through the server |
-N | No command, just forward | Tunnel only |
Remember it:
-L= bring a far-away service to my Laptop.
Making the SSH server safer
Server settings live in /etc/ssh/sshd_config.
👉 On every new server, before it goes live.
PermitRootLogin noPasswordAuthentication noPubkeyAuthentication yesAllowGroups ssh-usersMaxAuthTries 3ClientAliveInterval 300| Setting | Why |
|---|---|
PermitRootLogin no | Log in as a normal user, then use sudo, so actions are traceable |
PasswordAuthentication no | Stops password-guessing attacks |
AllowGroups | Only these groups may log in |
MaxAuthTries 3 | Limits guessing |
Apply it safely:
sudo sshd -t # test config syntaxsudo systemctl reload ssh # 'sshd' on RHEL familyKeep your current session open, and test a new login in a second terminal before you close it. One typo in sshd_config can lock everyone out.
Also add fail2ban (it blocks IPs that keep failing), and allow port 22 in the security group only from known IPs or the VPN.
SSH for Git and CI
- GitLab and GitHub use the same kind of keys. Add your public key in your profile settings, then test with
ssh -T git@gitlab.com. - Deploy keys are keys for one repository, read-only by default. They are for servers that only need to pull code.
- In CI, store private keys as masked, protected variables, never in the repository.
Real-world scenario: "Permission denied (publickey)"
A teammate cannot log in to a new server.
ssh -v ubuntu@10.0.1.25ls -la ~/.sshWork through it:
ssh -vshows which keys were offered. Was the right one tried?- Is the username right? Ubuntu servers use
ubuntu, Amazon Linux usesec2-user. - On the server: is the public key in that user's
authorized_keys? Are the permissions700/600? - Check
journalctl -u sshon the server (through the console or SSM) for the reason.
Common mistakes
- Committing private keys to Git. If it happens, replace the key right away. Git history keeps the old one.
- Ignoring host key warnings by deleting
known_hostslines without checking why. - Copying private keys onto bastions or shared servers.
- Turning off password login before testing key login, then being locked out.
Practice
- Make an ed25519 key pair and say which file is private.
- Write a
~/.ssh/configentry sossh webconnects to10.0.1.10asubuntuthroughbastion. - Write the command to forward local port 8443 to
grafana.internal:3000through the bastion. - List the three
sshd_configsettings you would change first on a new server.
Cheat sheet
| Command | What it does |
|---|---|
ssh-keygen -t ed25519 | Make a key pair |
ssh-copy-id user@host | Install your public key on a server |
ssh -J bastion host | Connect through a jump host |
ssh -L 5432:db:5432 bastion | Bring a remote port to your laptop |
ssh -v user@host | Debug a failing login |
sshd -t | Test the server config |
Key takeaways
- SSH lets you log in to a server safely over the network.
- You have two keys: the private one stays with you, the public one goes on servers.
~/.ssh/configsaves you from typing long commands.- To reach private servers, you go through a bastion, or use SSM in AWS.
- On servers, turn off root login and password login, and test before you log out.