Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
10.10.2024

20 Common SSH Commands You Should Be Using Today

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.

ssh username@remote_host
  • Replace username with your username and remote_host with the server’s IP address or domain name.

Example:

ssh user@example.com

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.

ssh -p port_number username@remote_host

Example:

ssh -p 2222 user@example.com

3. Copy Files from Local to Remote Using scp

Use scp (secure copy) to transfer files from your local machine to a remote server.

scp local_file username@remote_host:/path/to/destination

Example:

scp myfile.txt user@example.com:/home/user/

4. Copy Files from Remote to Local Using scp

To download files from a remote server to your local machine:

scp username@remote_host:/path/to/remote_file /path/to/local_destination

Example:

scp user@example.com:/home/user/remote_file.txt .

5. Copy Directories Recursively Using scp

To copy entire directories (including subdirectories), use the -r flag with scp.

scp -r /path/to/local_directory username@remote_host:/path/to/remote_destination

Example:

scp -r my_folder user@example.com:/home/user/

6. Execute Commands on a Remote Server

You can execute a command on a remote server without opening an interactive SSH session.

ssh username@remote_host ‘command_to_execute’

Example:

ssh user@example.com ‘ls -la /home/user’

7. Use SSH Key Authentication

To avoid entering passwords every time, you can use SSH key authentication.

  1. Generate an SSH key pair:
    ssh-keygen -t rsa
  2. 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:

sudo cat /var/log/auth.log | grep ‘sshd’

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:

ssh -L local_port:remote_host:remote_port username@remote_host

Example: Forward local port 8080 to port 3306 on a remote server.

ssh -L 8080:localhost:3306 user@example.com

Remote Port Forwarding:

ssh -R remote_port:local_host:local_port username@remote_host

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.

ssh -i /path/to/private_key username@remote_host

Example:

ssh -i ~/.ssh/id_rsa user@example.com

11. Copy Files Using rsync Over SSH

rsync is a more advanced tool than scp for copying files and directories.

rsync -avz /path/to/local_directory username@remote_host:/path/to/remote_directory

Example:

rsync -avz my_folder/ user@example.com:/home/user/

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:

ssh -O check username@remote_host

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:

who

Or:

w

These commands show which users are currently logged into the server.

14. Terminate an SSH Session

To exit an SSH session, simply type:

exit

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.

ssh -D local_port username@remote_host

Example:

ssh -D 8080 user@example.com

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:

  1. Start the SSH agent on your local machine:
    eval $(ssh-agent)
    ssh-add ~/.ssh/id_rsa
  2. 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:

  1. Edit or create ~/.ssh/config:
    nano ~/.ssh/config
  2. Add an entry:
    Host myserver
    HostName example.com
    User user
    Port 2222
    IdentityFile ~/.ssh/id_rsa
  3. Connect with:
    ssh myserver

18. Check SSH Version

To see which version of SSH is installed on your system:

ssh -V

Example Output:

OpenSSH_8.4p1, OpenSSL 1.1.1k 25 Mar 2021

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:

Host *
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:

ssh username@remote_host ‘df -h’

Example:

ssh user@example.com ‘df -h’

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.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills