Connect to another computer securely from your terminal.
SSH (Secure Shell) lets you connect to and work on another computer over a network.
A very common use is connecting to a Linux server.
Think:
Your Computer
↓
SSH
↓
Remote Server
Once connected, commands you type run on the remote machine.
1. Connect to a Server
The basic command:
ssh username@server
Example:
ssh ubuntu@192.168.1.20
You can also use a hostname:
ssh ubuntu@example.com
2. First Connection
The first time you connect, you may see a message asking whether you trust the server.
Type:
yes
You may then be asked for a password.
Your password will not appear while you type.
That's normal.
3. Connect Using a Different Port
SSH normally uses port 22.
If the server uses another port:
ssh -p 2222 username@server
For example:
ssh -p 2222 ubuntu@example.com
4. Using an SSH Key
Instead of a password, servers commonly use an SSH key.
Connect normally if your key is already configured:
ssh ubuntu@example.com
Or specify a particular private key:
ssh -i ~/.ssh/my-server.pem ubuntu@example.com
Never share your private key.
5. Generate an SSH Key
If you don't have one:
ssh-keygen
Press Enter to accept the defaults if you're unsure.
Your key will normally be stored under:
~/.ssh/
You'll typically have:
id_ed25519
id_ed25519.pub
The important difference:
id_ed25519 → Private key 🔒
id_ed25519.pub → Public key
Keep the private key private.
6. Copy Your Public Key to a Server
If password authentication is available:
ssh-copy-id username@server
After that, you can usually connect without entering the account password each time.
7. Run a Command Remotely
You don't have to start an interactive session.
Run a command directly:
ssh ubuntu@example.com "ls -la"
Or:
ssh ubuntu@example.com "df -h"
The command runs on the server and then SSH disconnects.
8. Copy a File to the Server
Use scp:
scp file.txt username@server:/home/username/
Example:
scp app.jar ubuntu@example.com:/home/ubuntu/
9. Copy a File from the Server
scp username@server:/path/to/file .
The . means:
Copy it into my current directory.
Example:
scp ubuntu@example.com:/home/ubuntu/app.log .
10. Copy a Directory
Use -r for recursive copying:
scp -r my-project username@server:/home/username/
11. Disconnect
When you're finished:
exit
Or press:
Ctrl + D
You're back on your own computer.
12. The Most Important Server Workflow
Connect:
ssh ubuntu@example.com
Check where you are:
pwd
See what's there:
ls -la
Do your work:
cd /var/www/myapp
When finished:
exit
13. SSH + Tmux
If you're running something that takes a long time, don't rely on a normal SSH session.
Start Tmux after connecting:
ssh ubuntu@example.com
Then:
tmux
Run your long-running command:
./deploy.sh
Detach:
Ctrl + B, D
You can safely disconnect:
exit
Later, reconnect:
ssh ubuntu@example.com
Then:
tmux attach
Your session is still there.
SSH gets you to the server. Tmux keeps your work alive.
14. Useful SSH Files
SSH configuration and keys normally live here:
~/.ssh/
Common files:
~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub
~/.ssh/known_hosts
~/.ssh/config
Don't edit these until you understand what you're changing.
15. Make Connections Easier with SSH Config
Instead of repeatedly typing:
ssh ubuntu@203.0.113.10
you can create an SSH shortcut in:
~/.ssh/config
Example:
Host myserver
HostName 203.0.113.10
User ubuntu
Then simply:
ssh myserver
Much nicer.
16. Common Problems
Permission denied
Permission denied (publickey)
Possible causes:
- Wrong username
- Wrong SSH key
- Key isn't installed on the server
- Wrong permissions on the key
- Server doesn't allow the authentication method you're using
Connection refused
Connection refused
Possible causes:
- SSH isn't running
- Wrong port
- Firewall is blocking the connection
- You're connecting to the wrong machine
Connection timed out
Connection timed out
Usually means the server cannot be reached or traffic to the SSH port is being blocked.
The Commands to Remember
| What you want | Command |
|---|---|
| Connect | ssh user@server |
| Different port | ssh -p 2222 user@server |
| Use key | ssh -i key user@server |
| Generate key | ssh-keygen |
| Copy public key | ssh-copy-id user@server |
| Run remote command | ssh user@server "command" |
| Copy to server | scp file user@server:/path |
| Copy from server | scp user@server:/path . |
| Copy directory | scp -r folder user@server:/path |
| Disconnect | exit |
The Golden Rules
1. Your private key is private.
Never send it to someone or commit it to Git.
2. Know where you are.
When working on a server:
pwd
is your friend.
3. Be careful with sudo.
You are changing the server, not your own computer.
4. Use Tmux for long-running work.
SSH → Tmux → Work
5. Don't blindly paste commands.
Understand what a command does before running it—especially commands involving:
sudo
rm
chmod
chown
Remember
SSH is essentially:
CONNECT
↓
WORK
↓
DISCONNECT
And when the work needs to survive your connection:
SSH
↓
Tmux
↓
Work
↓
Detach
↓
Come back later
That's the SSH workflow you'll use again and again.
