Bash ssh Command - OpenSSH SSH Client
Using the ssh Command
The ssh command is used to connect to a remote machine securely.
Basic Usage
To connect to a remote host, use ssh user@hostname:
Example
ssh user@example.com
Linux raspberrypi 6.6.74+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.6.74-1+rpt1 (2025-01-27) aarch64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Apr 10 13:09:32 2025 from 203.0.113.10
user@example.com:~ $
SSH Options
Here are some common options you can use with the ssh command:
-p- Specify the port-i- Use a private key file-v- Enable verbose mode-C- Enable compression-X- Enable X11 forwarding-o- Specify options directly on the command line
Option: -p (Port)
The -p option lets you specify the port to connect to on the remote host.
By default, SSH uses port 22.
Example: Specify Port
ssh -p 2222 user@example.com
user@example.com's password:
Option: -i (Identity File)
The -i option allows you to specify a private key file for authentication.
This is useful when you have a specific key for a server.
Example: Use Private Key
ssh -i /path/to/private_key user@example.com
user@example.com:~ $
Option: -v (Verbose Mode)
The -v option enables verbose mode, which provides detailed information about the SSH connection process.
This is helpful for debugging.
Example: Verbose Mode
ssh -v user@example.com
OpenSSH_9.6p1, OpenSSL 3.2.1 30 Jan 2024
debug1: Reading configuration data /home/user/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to example.com port 22.
debug1: Connection established.
.....
Option: -C (Compression)
The -C option enables compression, which can speed up data transfer by reducing the amount of data sent over the network.
Example: Enable Compression
ssh -C user@example.com
user@example.com's password:
Option: -X (X11 Forwarding)
The -X option allows X11 forwarding, enabling you to run graphical applications on the remote server and display them locally.
Example: X11 Forwarding
ssh -X user@example.com
user@example.com's password:
Option: -o (Specify Options)
The -o option allows you to specify SSH options directly on the command line.
This is useful for overriding configuration settings.
Example: Specify Options
ssh -o StrictHostKeyChecking=no user@example.com
user@example.com's password:
Warning: StrictHostKeyChecking=no turns off host key checking. SSH will then connect even if the server's key has changed, which leaves you open to man in the middle attacks. Only use it for test machines you trust.
Troubleshooting Common Issues
Common issues like "Connection Refused" or "Host Key Verification Failed" can occur. Here are some steps to troubleshoot these problems:
- Connection Refused: Ensure the SSH service is running on the remote server and the correct port is being used. Check firewall settings to make sure they are not blocking the connection.
- Host Key Verification Failed: This happens when the server's host key changes. Verify the server's identity and update the known_hosts file by removing the old key entry.
- Permission Denied: Check permissions and username. Check the server's SSH configuration for restrictions.
- Timeouts: Check network connectivity and server responsiveness. Adjust the SSH timeout settings if necessary.