The Secure Shell protocol is a common way to connect with a remote machine via client/server applications. It makes use of a toolset such as ssh, scp, and sftp, among many others, to ensure a secure authentication process and encrypted communication that follows. Due to this, these tools replace other older remote command execution toolsets such as telnet, rcp, and rlogin.

In this guide you will learn how to install and enable OpenSSH server/client services in your machine. It also covers all the necessary commands (SSH tools) to access and remotely manage systems and transfer files in between.

Getting Started With SSH

By default, most Linux systems include the ssh client and server applications. The packages that include ssh tools in RHEL and Fedora distributions are openssh, openssh-server, and openssh-client. Use the grep command to fetch ssh tools from the installed list:

        yum list installed | grep openssh
List already available OpenSSH packages in Fedora

While Ubuntu only includes an openssh-client package that also contains an openssh package. Use the grep command to list openssh packages in Ubuntu, as follows:

        sudo dpkg --list | grep openssh
List already available OpenSSH package in Ubuntu.
        sudo apt-get install openssh-server

Initiate/Enable SSH Service

Management of openssh service can vary from distribution to distribution, and irrespective of the default configurations, it does not start automatically. Use the following set of commands to ensure the service is up and running on your Linux machine:

        systemctl status sshd.service #for Fedora and RHEL
systemctl status ssh #for Ubuntu

If it's not running, check the service status as follows:

        systemctl start sshd.service #for Fedora and RHEL
systemctl ssh start #for Ubuntu

To initiate the openssh-server as soon as the system boots:

        systemctl enable sshd.service
systemctl ssh enable

How to Use SSH Client Tools

Among many other tools to utilize SSH protocol for Linux system remote access, the most frequently used are the ssh command for remote code execution and log in, where scp and rsync are useful in copying one or more files between the client and server.

The section covers in-depth detail of the above-discussed commands for efficient remote management.

Remote Login

SSH is the command that you will most often use for the remote configuration of your Linux server running sshd service. Use the ssh command to verify if you can log in to your Linux server for command execution.

You can use another Linux machine to log in to your server, or you can get an idea of it by simulating it over your localhost as follows:

For remote login to an Ubuntu account at X.X.X.X (where X.X.X.X is the IP address of the remote device):

        ssh ubuntu@X.X.X.X
    

For remote login as a local user:

        ssh localhost
    

If you are logging in for the first time to the remote server, it will prompt you for the confirmation to connect with the system, enter yes and type the user account password.

SSH login from another machine

After logging, you can continue with the remote command execution as it is similar to the regular login, with the only difference being that the remote communication is encrypted.

Once done, type the exit command to terminate the session and return to your local system. If it fails to close the remote shell the ~. keys also do a similar task and outputs 'Connection to X.X.X.X closed'.

SSH remote exit

Remote Execution

The ssh command allows executing commands on the remote system and returns output on the local machine. For instance,

The following command runs as the user ubuntu on the remote server and returns the hostname:

        ssh ubuntu@X.X.X.X hostname
    
Output hostname of the remote machine.

To execute a command that includes options or flags, surround it in double quotes as follows:

        ssh ubuntu@X.X.X.X "cat /tmp/new_file"
    
View the content of a file via ssh

The above command returns the content of the above file on your local screen.

You can also run multiple commands without reconnecting each time by enabling X11 forwarding on your server. Open the sshd_config file inside /etc/ssh directory and set X11 Forwarding to yes as follows:

Enable X11 Forwarding in the main configuration file.

Now run the commands as follows:

        ssh -X ubuntu@X.X.X.X hostname & cat /tmp/new_file/ & exit
    
SSH X11 Forwarding example of running multiple commands.

File Copy via scp and rsync

The scp command allows you to transfer/copy files from remote to the local system and vice-versa. Its functionality is similar to the rcp command but with RSA encrypted communication. Some examples follow.

Copy the file from the /etc/demo directory of the remote machine to its /tmp folder as follows:

        scp ubuntu@X.X.X.X:/home/ubuntu/demo/file /tmp<strong>  </strong>
    

This also enables recursive copying, which means you can supply the command with a directory, and it copies all the files/folders down the hierarchy to another local directory.

        scp -r localhost:/home/ubuntu/ /tmp
    

You can also use the scp command for files and directories backup, but rsync is a better backup utility for several reasons:

  • scp has an inability to retain file/directory permissions and time/date.
  • It is also unable to identify already copied files and directories.

Now list the content of the above directories to view the file permissions and time of creation, as follows:

        ls -l /etc/demo /tmp/demo
scp changes permission and time.

Repeat the scp command above and re-list the directories to check if it replaces the already copied file/directories from its time stamp:

SCP recopies the file.

Related: Securely Copy Files in Linux With the Scp Command

The -p flag for the scp command may help preserve the timestamp or write permissions, but it still replaces the already copied files. To overcome these shortcomings, use rsync as a backup tool. Delete the files first in the /tmp directory to continue with the example below. Use the rsync command with the -a flag for recursive archival and the -v option for verbose to copy the /home/ubuntu/demo files to the /tmp directory, as follows:

        rsync -av ubuntu@X.X.X.X:/home/ubuntu/demo /tmp
Use rsync command to copy files/directories.

List the /tmp directory to note how it preserves the time of file or directory creation.

List both folders to view time for file/directory creation.

Lastly, rerun the rsync command to verify it does not copy any file.

Getting to Know SSH

The article is a guide for the most widely used protocol for remote management of Linux servers. We display how to use the most important SSH commands with some tips and tricks to ease the task of file copy and management.

Beginning to understand SSH commands/tools and their functionality can change your view of system/server management as it unlocks the capabilities of not just SSH but the Linux terminal as well. It is a powerful tool that offers considerable security, along with further functionality too advanced to cover in a single guide.