SSH command in Linux, with examples

This article is a follow-up to the previous 90 Linux Commands frequently used by Linux Sysadmins post. Every week, as time allows, I will publish articles on the 90 commands geared toward Linux sysadmins and Linux power users.

 

What is the SSH command?

The ssh command in Linux is an acronym for Secure Shell, which is a protocol that provides secure communication between two networked devices. It is used to log into a remote machine and execute commands on it, or to run a remote terminal session. The ssh command is one of the most widely used tools for remote administration, and it is included in most Linux distributions by default.

In this blog post, we’ll take a closer look at 5 example commands that demonstrate the power and versatility of the ssh command and how it can be used for various tasks, such as connecting to a remote machine, running a command remotely, specifying a port for the connection, using a private key for authentication, and forwarding a local port to a remote port.

 

5 SSH command examples

5 SSH command examples

  1. Connecting to a remote machine using ssh

The basic syntax for connecting to a remote machine using ssh is:

ssh username@hostname

Here, username is the username that you want to use to log into the remote machine, and hostname is the hostname or IP address of the remote machine. For example, if you want to log into a remote machine with the hostname example.com using the username john, you would run the following command:

ssh john@example.com
  1. Running a command on a remote machine

You can use the ssh command to run a single command on a remote machine, without starting a remote terminal session. The syntax for this is:

ssh username@hostname command

For example, if you want to run the ls command on a remote machine with the hostname example.com using the username john, you would run the following command:

ssh john@example.com ls
  1. Specifying a port for the ssh connection

By default, the ssh service listens on port 22. However, if the remote machine is using a different port, you can specify it using the -p option. The syntax is:

ssh -p port_number username@hostname

For example, if you want to connect to a remote machine with the hostname example.com using the username john and port number 2222, you would run the following command:

ssh -p 2222 john@example.com
  1. Using a private key for authentication

Instead of using a password for authentication, you can use a private key. To use a private key, you need to specify the path to the key file using the -i option. The syntax is:

ssh -i path_to_key username@hostname

For example, if you want to connect to a remote machine with the hostname example.com using the username john and a private key located at ~/.ssh/id_rsa, you would run the following command:

ssh -i ~/.ssh/id_rsa john@example.com
  1. Forwarding a local port to a remote port

You can use the ssh command to forward a local port to a remote port. This is useful for accessing a remote service that is not directly accessible from your local machine. The syntax is:

ssh -L local_port:remote_host:remote_port username@hostname

For example, if you want to forward the local port 8080 to the remote port 80 on a remote machine with the hostname example.com using the username john, you would run the following command:

ssh -L 8080:example.com:5544 john@example.com

By executing this command, you are telling ssh to listen on port 8080 on your local machine and forward any incoming connections to port 80 on the remote machine example.com through the ssh connection.

Also, read ssh port forwarding.

 

Related commands and reading:

  1. telnet – A text-based network protocol that provides communication with a remote machine, but does not offer encryption or security.
  2. sftp – A secure file transfer protocol that uses ssh for encryption and authentication to transfer files between networked devices.
  3. scp – A secure copy command that uses ssh for encryption to copy files between networked devices.
  4. rsync – A fast and efficient file synchronization command that transfers only the differences between source and destination files over a network.

Related reading

  1. SSH official website.
  2. SSH Security: Protecting Your Linux Server from Threats.
  3. How to Kill Inactive SSH Sessions.
  4. How to Convert OpenSSH keys to Putty (.ppk) on Linux.

 

Conclusion

These are just a few examples of the many things you can do with the ssh command in Linux. Whether you are a system administrator, a developer, or a power user, the ssh command is an essential tool that you should have in your toolkit. Whether you need to log into a remote machine, run a command, transfer files, or access a remote service, the ssh command provides a secure and reliable way to do so.

Tags: , , , , ,



Top ↑