17.6 C
Jaipur
Wednesday, December 1, 2021

SSH Commands in Linux with Usage Examples

Must read

SSH is a network protocol for securely logging into a remote machine and executing commands. It is designed and created to provide the best security when accessing another computer remotely. Whenever data is sent by a computer to the network, ssh will automatically encrypt it.

To use SSH, the destination machine should have an SSH server application installed because SSH is a client-server model. An SSH server, by default, listens on the standard TCP port 22. SSH client is by default available on all Linux distributions.

In this tutorial, we learn SSH Commands in Linux with usage examples.

Prerequisites

  • An SSH client
  • An SSH server
  • IP address or name of the remote server

1. How to SHH to a Remote Server

A remote server is connected using an IP address or the name of the host. To connect ssh using an IP address, use the following command:

shh [ IP ADDRESS]

To connect to ssh using the name, use the following command:

ssh [ HOSTNAME ]

For example, to connect to a remote host using IP address 192.168.239.133, the command would be following.

$ ssh 192.168.239.133

When you first connect to a host, a message appears asking if you want to continue connecting. Type yes, then enter the password for your remote host.

SHH to a Remote Server

2. SSH with username

SSH uses the current user of the remote server when trying to connect. To connect to ssh with username, use the following syntax.

ssh [USERNAME]@[HOSTNAME/IP ADDRESS]

For example, to connect to the remote host with IP address 192.168.239.134 having a kali username, use the following command.

$ ssh [email protected]
 SSH with username

3. SSH with a different Port number

The SSH server listens to TCP port 22 by default but if you wish to change it, you need to specify the port in the command.

To connect to a remote host using a different port number, use the -p flag as shown in the following syntax.

ssh [ IP ADDRESS/HOSTNAME ] -p [ PORT NUMBER ]

For example, to connect to the remote host with an IP address of 192.168.239.134 using port number 223, use the following command.

$ ssh 192.168.239.134 -p 223
 SSH with Port number 223

4. SSH without password

In three simple steps, you can connect to your remote host using ssh without a password. The three steps required to log in to a remote server without entering a password are as follows.

Generate SSH key

To generate SSH keys, ssh-keygen is used which creates the public and private keys. These key pairs are used to authenticate between clients and servers.

To create a pair of keys, enter the following command on the client machine.

$ ssh-keygen -t rsa
Generate SSH key

Enter the location and paraphrase, or press enter to use the default settings.

Copy public SSH key

You need to copy the public SSH key to a remote server to use the key pair. To copy the public SSH key to the remote server, use the following syntax on the host machine.

ssh-copy-id [USERNAME]@[HOSTNAME/IP ADDRESS]

To copy the SSH key from IP address 192.168.239.134, use the following command.

$ ssh-copy-id [email protected]
Copy public SSH key

Login remotely without a password

You can now log in to the remote server without a password using the following command.

ssh [USERNAME]@[HOSTNAME/IP ADDRESS]

For example, to connect to the remote host with IP address 192.168.239.134 having a kali username, use the following command.

$ ssh [email protected]
Login remotely without password

5. Run a Command on Remote Server using SSH

The ssh command can be used to log in to the remote server. It can also be used to execute commands on the remote server.

The basic syntax for executing commands over ssh is as follows.

ssh [email protected] COMMAND1

ssh [email protected] 'COMMAND2'

ssh [email protected] 'COMMAND1 | COMMAND2'

ssh [email protected] "COMMAND1; COMMAND2; COMMAND3"

To get remote server date and time, use the following syntax:

ssh [email protected] date

For example, to get the date of the kali user from the server of IP address 192.168.239.134, use the following command.

$ ssh [email protected] date
Remote Server Date

To check the remote server disk space usage, the syntax is as follows.

ssh [email protected] 'df -H'

For example, to get disk space usage of kali user from the server of IP address 192.168.239.134, use the following command.

$ ssh [email protected] 'df -H'
Remote server disk space usage

To check the last reboot logs of the remote user, use the following syntax.

ssh [email protected] "last reboot"

For example, to get the last reboot logs of kali users from the server of IP address 192.168.239.134, use the following command.

$ ssh [email protected] "last reboot"
Last reboot logs of remote user

SSH Command line Options

Let us see some of the options available with the ssh command.

ssh -C

Use the -C option with ssh to request compression on all data received or transferred from the remote server, as seen in the following syntax.

ssh -C [USERNAME]@[HOSTNAME/IP ADDRESS]

For Example,

$ ssh -C [email protected]

ssh -v

The -v option is used with ssh command to debug the ssh client. The following is the syntax:

$ ssh -v [USERNAME]@[HOSTNAME/IP ADDRESS]

For Example,

$ ssh -v [email protected]
Debug SSH client

ssh -b

The -b option is used to bind an IP address to an SSH connection. The IP address will be used as the source address of the SSH connection. This is used when a client has more than two IP addresses and you might not know which IP address is used to create a connection to the SSH server.

For example,

$ ssh -b 192.168.239.133 [email protected]

The command will bind the IP address to the remote server. We can check it using the netstat |grep ssh command to check the connection.

Bind IP address

ssh -F

The -F option is used along with ssh command to specify a per-user configuration. The default configuration file is ~/.ssh/config.

To use a specific configuration file, use the -F option in the following manner.

$ ssh -F [File Location] [USERNAME]@[HOSTNAME/IP ADDRESS]

For Example,

$ ssh -F /etc/ssh/ssh_config.d [email protected]

ssh -L

The -L option is used for local port forwarding. Local port forwarding allows us to route traffic from our host to a destination port via a proxy.

The basic syntax for local port forwarding is the following.

ssh -L [LOCAL_IP:]LOCAL_PORT:DESTINATION:DESTINATIONPORT [USERNAME]@[HOSTNAME/IP ADDRESS]

For example, run the following command to connect to the remote host on port 3306 of kali user with IP 192.168.239.134 from the localhost 192.168.239.133 on port 3336.

$  ssh -L 3336:192.168.239.133:3306 [email protected]
Local Port Forwarding

ssh -R

The -R option is used along with the SSH command to enable remote port forwarding. This means you can forward a port on the remote server to a port on your local machine, which is then forwarded to a port on the destination machine.

The basic syntax for remote port forwarding is the following.

ssh -R [REMOTE:]REMOTE_PORT:DESTINATION:DESTINATION_PORT [USERNAME]@[HOSTNAME/IP ADDRESS]

For example,

$ ssh -R 3336:192.168.239.133:3000 [email protected]

The command will make ssh listen to the ssh server in port 3336, and tunnel all traffic to 3000 port.

Remote Port Forwarding

ssh -C -D

The -D option enables dynamic port forwarding. The usual SOCKS port is 1001, however, any port number can be used; nevertheless, some programs will only work on the 1001 port.

The basic syntax for dynamic forwarding is as follows.

ssh -D [LOCAL_IP:]LOCAL_PORT [USERNAME]@[HOSTNAME/IP ADDRESS]

For example,

$ sudo ssh -C -D 1001 [email protected] 

The -D specifies dynamic port forwarding in 1001 port and -C enables compression.

Dynamic Port Forwarding

ssh -X

The -X option is used along with ssh for X11 forwarding. The following is the syntax for X11 forwarding.

ssh -X [USERNAME]@[HOSTNAME/IP ADDRESS]

Use the following command, to enable X11 forwarding on the kali user with IP address 192.168.239.134.

$ ssh -X 192.168.239.134

ssh -Y

The -Y option is used along with ssh for Trusted X11 forwarding. This means that remote X11 will have full access to the original X11 display.

ssh -Y [USERNAME]@[HOSTNAME/IP ADDRESS]

Use the following command, to enable Trusted X11 forwarding on the kali user with IP address 192.168.239.134.

$ ssh -Y 192.168.239.134
Trusted Forwarding

ssh -o

The -o option can be used with other options.

For example,

$ ssh -o "batchmode=yes" [email protected]

If you use ssh -o “batchmode=yes,” the command will run successfully on the remote machine if passwordless connectivity is enabled, otherwise, it will return an error.

ssh -o batchmode=yes

Some of the most important command-line options are shown in the following table.

Option Description
-A It enables the authentication agent connection to be forwarded.
-a It disables the authentication agent connection to be forwarded.
-b It is used to bind source addresses.
-C It is used for data compression.
-c cipher_spec It selects the cipher specification for encrypting the session.
-D It is responsible for dynamic application-level port forwarding.
-E log_file It appends debug logs to log_file instead of standard error.
-F config file It specifies a per-user configuration file.
-g It allows remote hosts to connect to local forwarded ports.
-i identity_file It reads the private key for public-key authentication.
-j It specifies a ProxyJump configuration directive.
-l login_name It specifies the user to log in to the remote machine.
-p port It is used to specify the port to connect to the remote host.
-q It is the quiet mode.
-V Verbose mode.
-X It enables X11 forwarding
-Y It enables Trusted X11 forwarding

Conclusion

In this tutorial, we learned how to use ssh command along with useful examples. Thanks for reading, please provide your feedback and suggestions in the below comment section.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article