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:
ssh [OPTIONS] user@hostname
Example:
ssh pi@192.168.1.100
This command connects to the Raspberry Pi at 192.168.1.100
as the user pi.
Options
Option | Description |
---|---|
-p | Specify a custom SSH port (default is 22). |
-i | Use a specific SSH private key for authentication. |
-L | Local port forwarding (e.g., -L 8080:localhost:80). |
-R | Remote port forwarding (e.g., -R 9000:localhost:3000). |
-v | Verbose mode. |
-N | No remote command (useful for port forwarding only). |
-T | Disable pseudo-terminal allocation (for automation). |
Common Use Cases
1. Basic Remote Login
To log in to a remote server:
ssh username@remote-server
Example:
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
:
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):
ssh-keygen -t ed25519 # Recommended (or use `-t rsa -b 4096`)
- Copy the public key to the remote server using
ssh-copy-id:
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:
ssh -i ~/.ssh/id_rsa user@remote-server
4. Execute Remote Commands
To execute a single command on a remote server and exit:
ssh user@remote-server "command"
Example (check disk space):
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):
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):
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:
ssh -v user@remote-server
Additional Help
To see all available SSH options, check the manual:
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!