ssh command in Linux with examples

The ssh command in Linux is used to manage remote systems. Ordinarily, this would be another Linux system, but it could also be a firewall, router, or even a different operating system entirely. Using the ssh command to remotely log into another system will give you a command line terminal that you can fully access as if you were physically in front of the machine.

As you can imagine, being able to manage a countless number of remote systems without getting up from your chair is a dream for Linux administrators, or even ordinary users that have multiple systems in different locations. SSH can also be used to create port forwarding tunnels, effectively encrypting and securing connections made through any type of application on your Linux system.

The ssh command has a few different options that we can specify which allow us to manage our connection with the remote machine. Some of these are definitely worth learning, such as how to specify a username or port number with the ssh command.

In this guide, you’ll learn how to use the ssh command in Linux through examples. Follow along below to learn about the various options that you can use with this command. Before beginning, you may want to check out our guide on getting the most out of OpenSSH, as it explains how to get the protocol installed on any type of Linux distribution.

In this tutorial you will learn:

  • How to use the ssh command on Linux
ssh command in Linux with examples
ssh command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software ssh
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The ssh command is essential if you want to manage remote systems. Follow along with our examples below to learn about some of the most common and useful options to use with the command.

Logging into a remote system by using the ssh command in Linux
Logging into a remote system by using the ssh command in Linux

ssh command in Linux Basic Examples

  1. To login to a remote system with ssh, simply specify the host name or IP address of the remote system in your ssh command. As an example, we will show the command to SSH into hey server named linuxconfig.org that has an IP address of 10.1.1.1.
    $ ssh linuxconfig.org
    OR
    $ ssh 10.1.1.1
    
  2. Unless the username that you’re currently logged into is the same as the username on the remote system, you will want to specify the username and your ssh command. There are two different ways to do that, as seen below.
    $ ssh user@linuxconfig.org
    OR
    $ ssh -l user linuxconfig.org
    
  3. The default port for SSH to listen on is 22. If the remote system is running the SSH service on some non default port, you can specify that port with the -p option in your command. The following example shows how you would SSH into a remote system that’s running the service on port 2210.
    $ ssh -p 2210 user@linuxconfig.org
    
  4. The -v (verbose) option will give you details about the connection process of SSH. This is useful when troubleshooting a troublesome connection.
    $ ssh -v user@linuxconfig.org
    
  5. To increase verbosity even further, you can use the -vvv option.
    $ ssh -vvv user@linuxconfig.org
    
NOTE
You can always use the man command to read more about the ssh command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

The ssh command is normally pretty simple. However, it can be used in some pretty advanced ways. Probably the most advanced usage of SSH would be in the case of port forwarding. That’s outside the scope of this tutorial, but you can read about it in our guide on port forwarding with SSH.

Another useful and somewhat advanced feature of SSH is logging in without a password. It involves the configuration of RSA keys, which is actually a lot more secure than logging in with a password, and as you can imagine, a lot more convenient. This is an almost essential step for system administrators that continually login to the same remote systems and have to specify a password over and over again. You can read about configuring passwordless SSH logins in our other guide.

ssh command in Linux Advanced Examples

  1. Use the -4 or -6 options with the ssh command to specify IPv4 or IPv6 connections only, respectively.
    $ ssh -4 user@linuxconfig.org
    OR
    $ ssh -6 user@linuxconfig.org
    
  2. To enable X forwarding with SSH, use the -X option in your command.
    $ ssh -X user@linuxconfig.org
    
  3. The -C option can be used to enable compression for your SSH session. Things like standard input and standard error will be compressed with gzip before being sent to your terminal. This option is not very common nowadays because of widespread fast connections, but it was once pretty common to see on older modems.
    $ ssh -C user@linuxconfig.org
    

Closing Thoughts

In this guide, we learned all about the ssh command on Linux. The ssh command is by far the most widespread and commonly supported way to manage remote systems at the command line. We covered all the most basic and advanced options that you will need to know with the command, although even more exist. SSH is a very secure and convenient way to manage remote Linux systems as well as other network devices.