ssh
Overview
The ssh
(Secure Shell) command is used to securely log into remote machines and execute commands. It provides encrypted communication between two hosts over an insecure network.
Syntax
ssh [options] [user@]hostname [command]
Common Options
Option | Description |
---|---|
-p port |
Port to connect to on the remote host |
-i identity_file |
Selects a file from which the identity key is read |
-L port:host:hostport |
Local port forwarding |
-R port:host:hostport |
Remote port forwarding |
-X |
Enables X11 forwarding |
-v |
Verbose mode |
-q |
Quiet mode |
Key Use Cases
- Remote system administration
- Secure file transfers
- Port forwarding
- Remote command execution
- Tunneling applications
Examples with Explanations
Example 1: Basic Connection
ssh user@remote.host
Connects to remote.host as ‘user’
Example 2: Run Remote Command
ssh user@remote.host 'ls -l'
Executes ‘ls -l’ on remote host and returns output
Example 3: Port Forwarding
ssh -L 8080:localhost:80 user@remote.host
Forwards local port 8080 to port 80 on remote host
Understanding Output
- Connection messages
- Host key verification
- Authentication methods
- Warning and error messages
- Remote command output
Common Usage Patterns
Key-based authentication:
ssh-keygen -t rsa ssh-copy-id user@remote.host
Configuration via ~/.ssh/config:
Host alias HostName remote.host User username Port 22
Persistent connections:
ssh -o ServerAliveInterval=60 user@remote.host
Performance Analysis
- Use compression (-C) for slow connections
- Enable multiplexing for multiple sessions
- Use ControlMaster for connection sharing
- Configure proper timeout values