SSH: Working on a Remote Server
SSH (secure shell) lets you open a terminal on a server far away, safely encrypted. It's how you manage cloud servers.
SSH is how you securely control a server anywhere in the world from your laptop. Key-based login is both safer and more convenient than passwords, and is required by most cloud providers.
Example
ssh asha@203.0.113.10 # user@server address
ssh -i ~/.ssh/shop-key.pem ubuntu@203.0.113.10 # with a key file (common on AWS)
exit # leave the serverSSH keys instead of passwords
Example
ssh-keygen -t ed25519 -C "asha@laptop" # makes ~/.ssh/id_ed25519 (private) and .pub (public)
ssh-copy-id asha@203.0.113.10 # puts your PUBLIC key on the server- The private key never leaves your computer - don't share it, don't commit it
- The public key is safe to put on servers and GitHub
- Key files must be private:
chmod 600 ~/.ssh/id_ed25519
Copying files
Example
scp report.csv asha@203.0.113.10:/home/asha/ # copy to the server
scp asha@203.0.113.10:/var/log/app.log . # copy from the server
rsync -avz dist/ asha@203.0.113.10:/var/www/shop/ # sync a folder (only sends changes)Basic server safety
- Turn off password sign-in once keys work (
PasswordAuthentication noin/etc/ssh/sshd_config) - Use a firewall:
sudo ufw allow OpenSSH,sudo ufw allow 'Nginx Full',sudo ufw enable - Keep the system updated, and open only the ports you need
The address 203.0.113.10 in these examples is a documentation-only address. Use your own server's address.
Key points
- ssh user@host connects to a server.
- Use key pairs; keep the private key private.
- scp and rsync copy files.
📝 Test yourself
1. Which key do you put on the server?
2. Which command copies a file to a server over SSH?
3. Fill in the command that creates an SSH key pair:
-t ed25519
