Exploring ssh - Linux Commands

What is the SSH Command in Linux?

The SSH (Secure Shell) command in Linux is the standard tool for securely accessing and managing remote servers over an encrypted connection. It provides a secure channel over unsecured networks, enabling you to:

  • Log in to remote machines
  • Execute commands remotely
  • Transfer files securely
  • Forward network ports

All communication is encrypted, making SSH essential for system administrators, developers, and anyone working with remote servers.

Basic Syntax:

Terminal window
ssh [OPTIONS] user@hostname

Example:

Terminal window
ssh pi@192.168.1.100

This command connects to the Raspberry Pi at 192.168.1.100 as the user pi.

Options

OptionDescription
-pSpecify a custom SSH port (default is 22).
-iUse a specific SSH private key for authentication.
-LLocal port forwarding (e.g., -L 8080:localhost:80).
-RRemote port forwarding (e.g., -R 9000:localhost:3000).
-vVerbose mode.
-NNo remote command (useful for port forwarding only).
-TDisable pseudo-terminal allocation (for automation).

Common Use Cases

1. Basic Remote Login

To log in to a remote server:

Terminal window
ssh username@remote-server

Example:

Terminal window
ssh pi@192.168.1.100

2. Using a Custom SSH Port

If the remote server uses a non-default SSH port (e.g., 2222), specify it with -p:

Terminal window
ssh -p 2222 user@remote-server

3. Authenticating with an SSH Key

To log in using a specific private key (instead of a password):

  • Generate key pair (if you don’t have one):
Terminal window
ssh-keygen -t ed25519 # Recommended (or use `-t rsa -b 4096`)
  • Copy the public key to the remote server using ssh-copy-id:
Terminal window
ssh-copy-id -i ~/.ssh/id_ed25519.pub pi@192.168.1.100

This automates key installation in ~/.ssh/authorized_keys.

  • Log in securely without a password:
Terminal window
ssh -i ~/.ssh/id_rsa user@remote-server

4. Execute Remote Commands

To execute a single command on a remote server and exit:

Terminal window
ssh user@remote-server "command"

Example (check disk space):

Terminal window
ssh admin@example.com "df -h"

5. Local Port Forwarding

To forward a local port to a remote server (e.g., accessing a remote database locally):

Terminal window
ssh -L 3306:localhost:3306 user@remote-server

Now, connecting to localhost:3306 on your machine will redirect to the remote server’s port 3306.

6. Remote Port Forwarding

To expose a local service to a remote server (e.g., making a local web server public):

Terminal window
ssh -R 8080:localhost:80 user@remote-server

Now, accessing localhost:8080 on the remote server will connect to your local machine’s port 80.

7. Debugging Connections

If SSH fails, use -v (verbose mode) to troubleshoot:

Terminal window
ssh -v user@remote-server

Additional Help

To see all available SSH options, check the manual:

Terminal window
ssh --help
# or
man ssh

Recap

The SSH command is the backbone of secure remote server management in Linux. Whether you’re logging into a server, running commands remotely, forwarding ports, or debugging connections, SSH provides a reliable and encrypted way to work with remote systems. Mastering SSH is a must for any Linux user or administrator.

Happy remote computing! 🖥️🔒

Thank you

Thank you for your time and for reading this!