GO BACK TO BLOG'S MAIN PAGE

19 Most Common SSH Commands in Linux With Examples

Posted on

Article illustration

SSH is a network protocol that allows for secure remote connections between two systems. System administrators actively use SSH utilities to efficiently manage machines and directly and easily transfer files between systems. Due to the encryption of data during transmission, the security level of SSH is quite high.

This article provides a comprehensive guide to the most widely used SSH commands. This list can also be a helpful cheat sheet for future tasks.

Requirements

  • Choose an SSH client that suits your needs
  • A remote machine with an SSH server
  • The IP address or name of the remote server

How to Connect to a Remote Server

In order to establish a connection with a remote machine, it is necessary to have either its IP address or name. Open your terminal or any SSH client and enter the command “ssh” followed by the IP address:

ssh 192.168.56.101

or identity:

ssh test.server.com

Upon establishing a connection with a host for the first time, you will encounter the following message:

SSH connection established with “yes” selected in the terminal.

Please confirm by typing “yes” and pressing enter. It might be necessary for you to provide your password.

Please provide a username for the SSH connection.

SSH takes advantage of the current user for accessing a remote server. To set a user for an SSH connection, execute the command in the following format:

ssh username@hostname_or_ip

As an example:

ssh testuser@10.0.0.55

Consider using an alternative port number for your SSH connection.

Typically, the SSH server is configured to listen for connections on port 22. If the port setting in the SSH config file has been modified, you will need to provide the port. Otherwise, you may encounter an error:

Encountering an error: connection refused due to incorrect SSH port

If you want to establish a connection to a remote host using a specific SSH port number, simply utilize the -p flag. For instance:

ssh test.server.com -p 3322

Creating SSH Keys with SSH Keygen

To enhance the security of SSH connections, it is recommended to generate a key pair using the keygen utility. A public and private key make up the pair. It is important to keep the private key secure, while the public key can be freely shared.

SSH key pairs are utilized for automatic client-to-server authentication. When generating an SSH key pair, the requirement to input a password for server access is eliminated.

To create a key pair, use the following command on the host machine’s terminal:

ssh-keygen -t rsa

If you want to use the default settings, simply press Enter when prompted for the file location and passphrase.

Retrieve Public SSH Key

For using the key pair for SSH authentication, it is necessary to transfer the public key to a server. It is crucial to have the file id_rsa.pub that was generated using the SSH keygen utility.

To transfer your key to a server, execute this command from the client:

ssh-copy-id hostname_or_IP

If you prefer not to use the current user, you have the option to specify a username.

Please provide the password for authentication when prompted. Once this is done, there will be no need for password authentication when connecting to the server again.

Transfer a File Remotely via SSH using SCP

SCP is a reliable tool for securely transferring files over the SSH protocol. Here is the basic syntax:

scp fileName user@remotehost:/home/username/destination

For instance, to copy a file named sample3 to your Desktop on a remote server, use the following command:

scp sample3 test@10.0.10.5:/home/test/Desktop

The output provides a concise overview of the operation.

Remember to use the uppercase -P flag when specifying the port.

Modify SSH Configuration File

You have the ability to manage the access of remote users to a server through SSH. Make adjustments to the sshd_config file to personalize the options for your SSH server. Ensure that you only modify the choices you are well-versed in. There are instances where a server may become inaccessible as a result of improper configuration.

Feel free to use any editor you prefer to make changes to the file. You will require elevated privileges to make any modifications. When working with Linux, the preferred text editor is vim.

To execute the desired action on a remote host, please input the following command in the command line:

sudo vim /etc/ssh/sshd_config

Please provide the sudo password, and the shell will open the file in the editor you used.

Could you please restart the SSH service?

After modifying the SSH configuration, it is necessary to restart the service in Linux.

Based on the Linux distribution you’re using, you can execute one of the commands below on the machine where you made the changes:

sudo ssh service restart

Alternatively:

sudo sshd service restart

Lastly, please provide the password to finalize the procedure. Consequently, the upcoming SSH session will utilize the updated configurations.

Essential SSH Commands

Having a good grasp of basic SSH commands is essential when working on a remote server using SSH. Learn how to effectively manage a remote host using the commands and options provided in this article. Keep in mind that you have the option to combine the flags in order to obtain the desired output.

Display the path of the current working directory.

To display the file system path, you can utilize the pwd command.

Here is an example of a command you can use in the terminal to access a working directory via SSH:

The output shows the current directory location.

Retrieve a list of files and directories

For displaying the contents of your current working folder, you can try the ls command.

The shell will display the names of all directories, files, and links. To access additional details, include one of the following flags:

  • -The command “a” reveals concealed Linux files and entries that begin with a dot.
  • The -l option displays detailed information about the files in a directory. As an AI specialist, I can provide you with a comprehensive output that includes details such as permissions, ownership, date, and more.
  • The -s option displays the size of files, measured in blocks. To display the size in a more easily understandable format, simply add the -h option.

Navigate to a different folder

To access a particular folder, utilize the cd command along with the name or path of the directory.

cd Desktop/Downloads/Sample

Keep in mind that the names are case sensitive. Simply use the “cd” command without specifying a name or path to navigate back to your home directory.

Here are some helpful options for working with CDs:

  • cd .. – navigate to the directory that is one level above your current location.
  • Use the “cd –” command to navigate to the previous directory.
  • Navigate to the root directory by using the command “cd /“.

Move a File

To duplicate a file or directory, you can utilize the cp command. Please ensure that you provide the necessary details, such as the file name and the desired destination.

cp fileName /directory/path/destination

To move file1 from the Desktop to Dir1, enter the following command:

cp file1 Dir1

When copying file1 to a different location, you can modify its name using the following format:

cp file1 Dir1/Newfile1Name

Use this command to easily duplicate file1 to Dir1 and give it a custom name.

To duplicate a directory and all of its contents, utilize the -r flag in the following manner:

cp -r Directory1 NewLocation

Transfer a File

The mv command functions similarly to the copy command.

For example, if you want to relocate a file, you can enter the following command:

mv fileName directory/path/destination

Generate a File

Using the touch command, you can effortlessly create a new file with any desired extension.

Enter the following command in the terminal:

Execute the command “touch fileName” to create a new file.

Here’s an example of how to create a system.log file:

Please access the system log.

Generate a File

Using the touch command, you can effortlessly create a new file with any desired extension.

Enter the following command in the terminal:

touch fileName

Here’s an example of how to create a system.log file:

touch system.log

Build a Directory

To create a directory, you can utilize the mkdir command. Please provide a new directory name or full path in the following format:

mkdir NewDirectoryName

Alternatively:

mkdir directory/path/NewDirectoryName

Remove a File or Directory

To remove a file in Linux, you can utilize the rm command by following this format:

rm fileName

Furthermore, you have the option to input a complete file path:

rm /home/user/dir1/fileName

If you want to remove a directory, simply include the -r flag when using the rm command.

Check Network Details

To check the status of all network adapters, you can utilize the ifconfig command. In addition, if you do not specify any options when using ifconfig, the output will only show the active interfaces.

Obtain network information using the ifconfig command.

To remove all content from your bash screen, simply enter the “clear” command in the shell. This function erases a specific section of the display and moves the previous output upwards.

If you want to completely eliminate the output from the terminal, you can employ the reset command.

Executing a Command on a Remote Server from a Local Computer

This approach does not generate a fresh shell. Instead, it executes a command and brings the user back to the local prompt. You have the ability to create a file, duplicate files, or execute any other SSH command using this format.

To execute a command from a local machine, simply add an instruction to the SSH command. Here’s an example of how to delete a file:

ssh test.server.com rm ~/Desktop/Dir1/sample4

Please provide the password to delete the file on the remote server without initiating a new shell.

Exploring SSH Command Line Options

The SSH tool offers a wide range of optional parameters for enhanced functionality. Here is a table that provides a comprehensive list of SSH options along with their corresponding descriptions.

SSH OptionDescription
-1Instructs ssh to use protocol version 1
-2Instructs ssh to use protocol version 2.
-4Permits only IPv4 addresses.
-6Permits only IPv6 addresses.
-AEnables authentication agent connection forwarding. Use this option with caution.
-aDisables authentication agent connection forwarding.
-b <bind_address>Use this option on the local host with more than one address to set the source address of the connection.
-CEnables data compression for all files. Only to be used with slow connections.
-c <cipher_spec>Use to select a cipher specification. List the values separated by a comma.
-E <log_fileName>Attaches debug logs to log_file instead of standard error.
-fSends ssh to background, even before entering a password or passphrase.
-gPermits remote hosts to connect to ports forwarded on a local machine.
-qRuns ssh in quiet mode. It suppresses most error or warning messages.
-VDisplays the version of ssh tool and exits.
-vPrints debugging messages for ssh connection. The verbose mode is useful when troubleshooting configuration issues.
-XUse this option to enable X11 forwarding.
-xDisable X11 forwarding.

In summary

This article provides a comprehensive guide to effectively utilizing the SSH tool, highlighting the 19 most popular commands. With our advanced technology, you can effortlessly control your server from anywhere while ensuring maximum security. Enjoy the convenience of having all the necessary commands readily available.

The SSH tool is an essential component for system administrators and developers who need to manage remote servers securely and efficiently. This article has provided a comprehensive guide to the 19 most commonly used SSH commands, complete with examples to illustrate their usage. From connecting to a remote server and creating SSH keys to transferring files and modifying the SSH configuration file, these commands offer a powerful set of tools for managing remote systems. By utilizing these commands, you can enhance your productivity and streamline your workflow, all while ensuring the highest level of security for your remote connections. Whether you’re a seasoned system administrator or a newcomer to the world of remote server management, this guide is an invaluable resource that you’ll find yourself referring to time and time again.

FAQs

SSH stands for Secure Shell and it is a network protocol that allows for secure remote connections between two systems. It is important because it provides a secure way to manage remote servers and transfer files between systems.

To connect to a remote server using SSH, you need to have either its IP address or name. Open your terminal or any SSH client and enter the command “ssh” followed by the IP address or identity. Upon establishing a connection with a host for the first time, you will be prompted to confirm by typing “yes” and pressing enter.

To create SSH keys with SSH Keygen, use the following command on the host machine’s terminal: “ssh-keygen -t rsa”. If you want to use the default settings, simply press Enter when prompted for the file location and passphrase.

To transfer files remotely via SSH using SCP, use the following syntax: “scp fileName user@remotehost:/home/username/destination”. For instance, to copy a file named sample3 to your Desktop on a remote server, use the command: “scp sample3 test@10.0.10.5:/home/test/Desktop”.

To modify the SSH configuration file, use any editor you prefer to make changes to the file. You will require elevated privileges to make any modifications. When working with Linux, the preferred text editor is vim. To execute the desired action on a remote host, input the following command in the command line: “sudo vim /etc/ssh/sshd_config”.

After modifying the SSH configuration, it is necessary to restart the service in Linux. Based on the Linux distribution you’re using, you can execute one of the commands below on the machine where you made the changes: “sudo ssh service restart” or “sudo sshd service restart”.

Some essential SSH commands for managing a remote host include displaying the path of the current working directory, retrieving a list of files and directories, navigating to a different folder, moving a file, generating a file, building a directory, and removing a file or directory.

To check network details using SSH, use the “ifconfig” command. This command displays the status of all network adapters and shows the active interfaces if no options are specified.

To execute a command on a remote server from a local computer, add an instruction to the SSH command. For example, to delete a file, use the command: “ssh test.server.com rm ~/Desktop/Dir1/sample4”.

Some common SSH command line options include enabling authentication agent connection forwarding, disabling authentication agent connection forwarding, permitting only IPv4 or IPv6 addresses, enabling data compression, selecting a cipher specification, attaching debug logs to a log file, sending ssh to background, permitting remote hosts to connect to ports forwarded on a local machine, running ssh in quiet mode, displaying the version of the ssh tool, and printing debugging messages for the ssh connection.

Author photo

Bohdan Pavlyuk

Pavlyuk Bohdan specializes in Kubernetes and Cloud technologies. Thus, he progressed to becoming one of our top field specialists within a matter of a few years and impressively shifting from a trainee potion to a full-trained professional DevOps. He deals with a broad range of deep-knowledge requiring areas, such as working on Linux-based infrastructure; configuration and managing databases; CI/CD platforms, Kubernetes; Helm, Docker; Python, Ansible; TCP/IP, DNS, HTTP/HTTPS, SSH. Despite his relatively young yet highly prospective age, Bohdan demonstrates impressive expertness.

What else to read on the topic