Linux ssh Command
The ssh command connects to a remote host over 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 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 host name of the remote host to connect to.[command]: an optional command to execute on the remote host.
Common options
-l user: specifies the user to log in as.-p port: specifies the port number for connecting to the remote host; the default is 22.-i identity_file: specifies the identity file (private key file).-v: verbose mode, which shows debugging information.-C: enables compression.-N: does not execute a remote command, only performs port forwarding.-f: runs 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.
Example
Basic Usage
Connect to a remote host:
ssh user@hostname
Example:
ssh test@runoob.com
Connect on a specified port:
ssh -p 2222 user@hostname
Example:
ssh -p 2222 test@runoob.com
Using an identity file:
ssh -i /path/to/private_key user@hostname
Example:
ssh -i ~/.ssh/id_rsa test@runoob.com
Execute a command on the remote host:
ssh user@hostname command
Example:
ssh test@runoob.com ls -la
Verbose mode:
ssh -v user@hostname
Example:
ssh -v test@runoob.com
Enable compression:
ssh -C user@hostname
Example:
ssh -C test@runoob.com
Run in the background without executing a command:
ssh -f -N user@hostname
Example:
ssh -f -N test@runoob.com
Port forwarding
Local port forwarding:
ssh -L local_port:remote_host:remote_port user@hostname
Example:
ssh -L 8080:localhost:80 test@runoob.com
Remote port forwarding:
ssh -R remote_port:local_host:local_port user@hostname
Example:
ssh -R 8080:localhost:80 test@runoob.com
Dynamic port forwarding:
ssh -D [bind_address:]port user@hostname
Example:
ssh -D 1080 test@runoob.com
Advanced usage
Configuration file
The SSH client configuration file is located at ~/.ssh/config, where you can set commonly used options.
Example:
Host example
HostName example.com
User john
Port 2222
IdentityFile ~/.ssh/id_rsa
To use it, simply:
ssh example
SSH agent forwarding
Enable agent forwarding:
ssh -A user@hostname
Example:
ssh -A test@runoob.com
X11 forwarding
Enable X11 forwarding:
ssh -X user@hostname
Example:
ssh -X test@runoob.com
Linux Command Reference