Essential SSH and SCP Command Cheat Sheet
Classified in Computers
Written on in
English with a size of 4.22 KB
Connecting via SSH
Connect to a server (default port 22):
$ ssh [email protected]Connect on a specific port:
$ ssh [email protected] -p 6222Connect via PEM file (requires 0400 permissions):
$ ssh -i /path/file.pem [email protected]See: SSH Permissions
Executing Remote Commands
Execute a remote command:
$ ssh [email protected] 'ls -l'Invoke a local script:
$ ssh [email protected] bash < script.shCompress and download from a server:
$ ssh [email protected] "tar cvzf - ~/source" > output.tgzSCP File Transfers
- Copy from remote to local:
scp user@server:/dir/file.ext dest/ - Copy between two servers:
scp user@server:/file user@server:/dir - Copy from local to remote:
scp dest/file.ext user@server:/dir - Copy a whole folder:
scp -r user@server:/dir dest/ - Copy all files from a folder:
scp user@server:/dir/* dest/ - Copy from server folder to current folder:
scp user@server:/dir/* .
Configuration Locations
| Path | Description |
|---|---|
/etc/ssh/ssh_config | System-wide config |
~/.ssh/config | User-specific config |
~/.ssh/id_{type} | Private key |
~/.ssh/id_{type}.pub | Public key |
~/.ssh/known_hosts | Logged in host |
~/.ssh/authorized_keys | Authorized login key |
SCP Options
| Option | Description |
|---|---|
-r | Recursively copy directories |
-C | Compresses data |
-v | Prints verbose info |
-P | Uses a specific port |
-B | Batch mode (prevents password prompt) |
-p | Preserves times and modes |
SSH Config Sample
Host server1
HostName 192.168.1.5
User root
Port 22
IdentityFile ~/.ssh/server1.keyLaunch by alias:
$ ssh server1See: Full Config Options
ProxyJump
$ ssh -J proxy_host1 remote_host2
$ ssh -J user@proxy_host1 user@remote_host2Multiple jumps:
$ ssh -J user@proxy_host1:port1,user@proxy_host2:port2 user@remote_host3SSH Copy ID
Copy public key to server:
$ ssh-copy-id user@server
$ ssh-copy-id server1
$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@serverSSH Keygen
Generating Keys
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"| Flag | Description |
|---|---|
-t | Type of key |
-b | Number of bits |
-C | Add a comment |
Key Management
- Generate interactively:
ssh-keygen - Specify filename:
ssh-keygen -f ~/.ssh/filename - Generate public from private:
ssh-keygen -y -f private.key > public.pub - Change comment:
ssh-keygen -c -f ~/.ssh/id_rsa - Change passphrase:
ssh-keygen -p -f ~/.ssh/id_rsa
Supported Key Types
- rsa
- ed25519
- dsa
- ecdsa
Known Hosts Management
Search known_hosts: ssh-keygen -F <ip/hostname>
Remove from known_hosts: ssh-keygen -R <ip/hostname>
Key Formats
- PEM
- PKCS8