SSH (Secure Shell) is a widely used protocol for accessing remote servers securely. It allows you to manage and interact with remote systems over an encrypted connection. Mastering basic SSH commands can significantly improve your productivity and make managing remote servers easier. Below are 20 common SSH commands that every system administrator, developer, or anyone working with servers should know.
1. Basic SSH Connection
This command is used to connect to a remote server via SSH.
- Replace username with your username and remote_host with the server’s IP address or domain name.
Example:
2. Specify a Port
By default, SSH uses port 22. If your server uses a different port, use the -p flag to specify the port.
Example:
3. Copy Files from Local to Remote Using scp
Use scp (secure copy) to transfer files from your local machine to a remote server.
Example:
4. Copy Files from Remote to Local Using scp
To download files from a remote server to your local machine:
Example:
5. Copy Directories Recursively Using scp
To copy entire directories (including subdirectories), use the -r flag with scp.
Example:
6. Execute Commands on a Remote Server
You can execute a command on a remote server without opening an interactive SSH session.
Example:
7. Use SSH Key Authentication
To avoid entering passwords every time, you can use SSH key authentication.
- Generate an SSH key pair:ssh-keygen -t rsa
- Copy the public key to the remote server:ssh-copy-id username@remote_host
After copying the key, you can log in without a password.
8. Check SSH Connection Logs
To view SSH login attempts and connection logs on the server:
This command is useful for monitoring unauthorized login attempts.
9. SSH Tunneling (Port Forwarding)
SSH tunneling allows you to forward traffic from a local port to a remote server securely.
Local Port Forwarding:
Example: Forward local port 8080 to port 3306 on a remote server.
Remote Port Forwarding:
10. SSH into Remote Server with a Different Identity File
If you have multiple SSH keys, you can specify a particular key with the -i flag.
Example:
11. Copy Files Using rsync Over SSH
rsync is a more advanced tool than scp for copying files and directories.
Example:
The -a flag preserves file permissions, -v is for verbose output, and -z enables compression.
12. Check SSH Connection Status
To check if the SSH connection to a remote server is still active:
This command checks the connection status without interrupting it.
13. List Active SSH Connections
To list active SSH connections on a server, you can use:
Or:
These commands show which users are currently logged into the server.
14. Terminate an SSH Session
To exit an SSH session, simply type:
Or press Ctrl + D.
15. Set Up a SOCKS Proxy with SSH
SSH can act as a SOCKS proxy, allowing you to browse the internet securely through the SSH tunnel.
Example:
Then set your browser’s proxy settings to localhost:8080 to use the SOCKS proxy.
16. SSH Agent Forwarding
If you want to use your local SSH keys on a remote server:
- Start the SSH agent on your local machine:eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa - Connect with agent forwarding:ssh -A username@remote_host
This allows you to SSH from the remote server to another server using your local key.
17. SSH Config File
To simplify SSH connections, you can create a config file:
- Edit or create ~/.ssh/config:nano ~/.ssh/config
- Add an entry:Host myserver
HostName example.com
User user
Port 2222
IdentityFile ~/.ssh/id_rsa - Connect with:ssh myserver
18. Check SSH Version
To see which version of SSH is installed on your system:
Example Output:
19. Set KeepAlive Interval to Prevent Timeout
To keep the SSH connection alive and prevent it from timing out, add the following in ~/.ssh/config:
ServerAliveInterval 60
This sends a keep-alive signal every 60 seconds.
20. Check Available Disk Space on Remote Server
You can use df over SSH to check disk space on a remote server:
Example:
This command shows available disk space in a human-readable format (-h).
Summary
These 20 SSH commands cover a wide range of functions, from basic connections and file transfers to advanced tasks like tunneling and using SSH keys. By mastering these commands, you can manage remote servers more efficiently and securely. Whether you’re deploying applications, transferring files, or troubleshooting server issues, these SSH commands are essential tools in a system administrator’s toolkit.