Linux ssh Command
sshThe command is used to connect to a remote host via the SSH protocol for remote login and command execution. It encrypts all communication in the session, ensuring the security of data transmission.
SSH (Secure Shell) is an encrypted protocol used for remote login and communication between other network services. SSH provides a secure communication channel to protect the confidentiality and integrity of data.
Syntax
ssh [options] [user@]hostname [command]
Parameter Description
[user@]hostname: The username and hostname of the remote host to connect to.[command]: An optional command to execute on the remote host.
Common Options
-l user: Specify the user to log in as.-p port: Specify the port number to connect to the remote host, default is 22.-i identity_file: Specify the identity authentication file (private key file).-v: Verbose mode, displays debug information.-C: Enable compression.-N: Do not execute remote commands, only perform port forwarding.-f: Run in the background.-L local_port:remote_host:remote_port: Local port forwarding.-R remote_port:local_host:local_port: Remote port forwarding.-D [bind_address:]port: Dynamic application-level port forwarding.
Examples
Basic Usage
Connect to a remote host:
ssh user@hostname
Example:
ssh test@example.com
Connect with a specified port:
ssh -p 2222 user@hostname
Example:
ssh -p 2222 test@example.com
Use an authentication file:
ssh -i /path/to/private_key user@hostnameExample:
ssh -i ~/.ssh/id_rsa test@example.com
Execute a command on the remote host:
ssh user@hostname command
Example:
ssh test@example.com ls -la
Verbose mode:
ssh -v user@hostname
Example:
ssh -v test@example.com
Enable compression:
ssh -C user@hostname
Example:
ssh -C test@example.com
Run in the background without executing commands:
ssh -f -N user@hostname
Example:
ssh -f -N test@example.com
Port Forwarding
Local port forwarding:
ssh -L local_port:remote_host:remote_port user@hostname
Example:
ssh -L 8080:localhost:80 test@example.com
Remote port forwarding:
ssh -R remote_port:local_host:local_port user@hostname
Example:
ssh -R 8080:localhost:80 test@example.com
Dynamic port forwarding:
ssh -D [bind_address:]port user@hostname
Example:
ssh -D 1080 test@example.com
Advanced Usage
Configuration File
The SSH client configuration file is located at ~/.ssh/config, where common configurations can be set.
Example:
Host example
HostName example.com
User john
Port 2222
IdentityFile ~/.ssh/id_rsa
Simply use:
ssh example
SSH Agent Forwarding
Enable agent forwarding:
ssh -A user@hostname
Example:
ssh -A test@example.com
X11 Forwarding
Enable X11 forwarding:
ssh -X user@hostname
Example:
ssh -X test@example.comOther Extensions
Complete Linux Commands