Examples of Main SSH Commands List in Linux

Main SSH Commands List in Linux with Practical Examples

SSH stands for Secure Shell. It is a protocol that uses encryption to protect your sensitive data while transmitting over the network. So, SSH enables you to log onto a remote system securely and run commands on it. In this article, you will review the Main SSH commands in Linux with examples. For managing a Linux server or VPS, learning the Main SSH Commands is essential. It’s the best method for navigating your system and changing files or folders.

Several outdated Unix and Linux commands and protocols were replaced by SSH in the 1990s. telnet, rlogin, and rsh are a few of them. It is not that complicated to learn what is SSH port and How it works, but practicing the most important Linux SSH Commands is helpful to know what to do after accessing a remote server.

Using the SSH command, two hosts can connect securely over an insecure network. Additionally, file transfers, terminal access, and tunneling other apps are all possible with this connection. In the end of this tutorial, you will be familiar with a list of Basic SSH Commands to use for Cases of SSH, SSH configuration, correct interaction with remote hosts, and consequently, boost productivity and security while connecting to Remote Linux Host.

SSH is a client-server model, so in order to utilize it, the destination machine must have an SSH server application installed. Standard TCP port 22 is the default listening port for an SSH server. All Linux distributions come with the SSH client by default. Before diving into Main SSH Commands examples, let’s see what is required to start.

Prerequisites to Use SSH Commands in Linux

To let this tutorial works correctly, provide the options below and move on.

  • A Server running Linux VPS.
  • An SSH client.
  • An SSH server on the remote System.
  • The IP address or name of the remote server.

How to Access Your Remote Server

As we mentioned above, you need to purchase your own VPS since you must have access to a remote server before we begin. With the use of protocols like telnet, ftp, netcat, etc., there are numerous ways to communicate with remote Linux hosts. However, the lack of encryption makes these insecure. The SSH protocol can be used to enable secure communication between the hosts. In order to communicate with the remote computer, we must utilize an SSH client. For Linux, there are many GUI and CLI-based clients available. To access a remote server, you can try any of the below 8 methods.

1. You require a remote machine’s IP address or name in order to connect to it. Open a terminal or SSH client, then type the IP address and ssh:

ssh IP Address

or name:

ssh test.server.com

The first time you connect to a host, you will be asked if you are sure to continue connecting. So, type yes and hit enter and also enter your password.

2. Also, you can access the remote server by specifying a Username for SSH connection. When connecting to a remote server, SSH makes use of the current user. Run the following command to specify a user for an SSH connection:

ssh username@hostname_or_ip

3. The SSH server typically listens on port 22 for connections. You must specify the port if the SSH configuration file’s port setting has been altered. If not, you will see the ”Connection Refused” error. To use a different poet number for the SSH connection, follow this instruction.

To connect to a remote host with a custom SSH port number, run:

ssh test.server.com -p 3322

4. The fourth method teaches you how to generate SSH keys using SSH Keygen.

Utilize the Keygen utility to create a key pair to increase the security of SSH connections. A private key and a public key make up the pair. While the private key must remain private, the public key can be shared. Clients automatically authenticate to servers using SSH key pairs. Passwords are no longer required to access servers once you’ve created an SSH key pair.

Use the following command to create a key pair on the host machine’s terminal:

ssh-keygen -t rsa

Press Enter when prompted for the file location and passphrase to utilize the default settings.

5. To access your remote server, you can copy public SSH key. You must copy the public key to a server if you want to use the key pair for SSH authentication. The file id_rsa.pub that was previously created with the SSH Keygen tool serves as the key. Run the following command from the client to copy your key to a server:

ssh-copy-id hostname_or_IP

If you don’t want to utilize the current user, you can also give a username. When prompted, enter the password to authenticate. After that, connecting to the same server won’t require entering the password.

6. Previously, you learned SCP command to copy a file in Linux. You can use this method here to copy a file remotely over SSH with SCP. To copy files over the SSH protocol securely, type:

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

7. You can edit the SSH config file to SSH to a Remote Server. Through SSH, you can restrict remote users’ access to a server. To alter SSH server options, modify the sshd_config file’s settings. Only edit the options you are familiar with, please. A server’s configuration can go wrong and make it unreachable. To modify the file, use the editor of your choice. To make modifications, you’ll need superuser permissions. We use Vim on Linux:

Type the following in the command line of a remote host. Also, you need to enter the sudo password to let the shell open the file in the editor you used.

sudo vim /etc/ssh/sshd_config

8. As the latest method to access a remote server, the Restart SSH service is suggested. You must restart the Linux service after making modifications to the SSH configuration. Run one of the following commands on the computer where you changed the settings, depending on the Linux distribution:

sudo ssh service restart

OR

sudo sshd service restart

The process is finished by entering the password.

After accessing a remote server, you can also Find Active SSH Connections in Linux easily. Finally, if you faced failed SSH login attempts in Linux, you can check and fix them.

22 Main SSH Commands in Linux with Examples

Previously, you learned Basic Linux Commands. But after buying Linux VPS, you need to know how to manage and navigate it. Basic SSH commands help you to work on your remote server using SSH. Let’s go through this guide to review the main SSH commands and options to achieve this skill.

1. Display Working Directory Location

You can use the command below to show the working directory path:

pwd

2. Duplicate a File

To copy a directory or file, use the cp command. Both the file’s name and the intended location must be specified.

cp fileName /directory/path/destination

Use the command below to copy file1 from Desktop to Dir1:

cp file1 Dir1

Also, you can use the below format to modify the name of file1 while moving it to a different location:

cp file1 Dir1/Newfile1Name

In this way, file 1 will be copied to Dir1 with the name you specified. To copy a directory and its contents, type:

cp -r Directory1 NewLocation

3. Change Directory

cd is one of the main ssh commands. You can use this command and a name or path of a directory to navigate to a specific folder.

cd Desktop/Downloads/Sample

The names are case-sensitive, so keep that in mind. To get back to the user’s home directory, use the cd command without a name or path.

Now, look at some useful cd options:

  • cd / go to the root directory.
  • cd - switch to the previous directory.
  • cd .. go to the directory one level higher than your current location.

4. Move a File

To move a file to another location, for example, you can use the below command as well you learned in copy section.

mv fileName directory/path/destination

If you are interested in learning more, refer to Move Files and Directories in Linux and find out more related commands.

5. List Files and Directories

Use the ls command to see a list of a working folder’s contents. All folders, files, and URLs will have their names shown by the shell. One of the following flags should be added to obtain more details:

  • -a displays hidden Linux files and entries starting with a dot.
  • -l shows file details for directory contents. For example, the output includes permissions, ownership, date, etc.
  • -s lists the size of files, in blocks. Add -h to show the size in a humanly-readable form.

6. Create a File

To create a new file with any extension, run the following command:

touch fileName

7. View Network Information

You have read about using ifconfig command. As one of the main SSH commands, you can use the ifconfig command to see the status of each network adapter on your system. Furthermore, the output of ifconfig only shows active interfaces when no arguments are used.

8. Create a Directory

As you guess, the mkdir command is used for creating a directory. You can enter a new directory name or full path in the below format:

mkdir NewDirectoryName

OR

mkdir directory/path/NewDirectoryName

9. Delete a File/Directory

To delete a Linux file, run:

rm fileName

In addition, you can enter a full path:

rm /home/user/dir1/fileName

10. Clear the Terminal Screen

You can use the command below to Clear terminal screen in Linux. In this way, one partition of the screen will be cleared and the previous output will shift up.

clear

To remove the output from the terminal completely, run:

reset

11. Run a Command from a Local Computer on a Remote Server

A new shell is not created using this technique. Instead, it executes a command and then brings up the local prompt for the user. In this format, any SSH command can be executed, including those for creating and copying files. Add an instruction to the SSH command to remotely run a command from the local machine. To delete a file, for instance, enter:

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

If you enter the password, the file will be removed from the remote server without a new shell being created.

12. Display the Content of a File

You can use the cat command to see the content of your considered files. Simply run:

cat [file name]

It also enables you to combine many files into one single file. For instance:

cat info.txt info2.txt > mergedinfo.text

This command will save the contents of info.txt and info2.txt into mergedinfo.txt.

13. Show a Specific String in Files

The grep command looks for a given string in files. For example, to search for ”line” in a file named ”info.tx”, type:

grep 'line' info.txt

In this way, the above command will print the entire line that contains the matched text.

Note: The grep command is case-sensitive. So, if you want to ignore letter cases, use -i option.

14. Search for Files/Directories

You can use the find command to look for a file or files with a specific name, size, file type, and more criteria.

find [starting directory] [options] [search term]

Your preferred starting point for your search would be [starting directory]. You might have three main choices:

  • / (slash) — search the whole system.
  • . (dot) — search the working directory.
  • ~ (tilde) — search the home directory.

You can further narrow your search by using the argument [options]. The most well-liked choices include:

  • -name: look for files based on their names.
  • -user: search for files that belong to a given user.
  • -size: look for files based on their sizes.

[search term] is the phrase or number you use to look for files.

15. Open a File using Vi/Nano

As you know, Vi and Nano are two text editors. If you need to know how to choose between Nano and Vim, refer to our related article. However, to open a file, run:

vi [file name]

OR

nano [file name]

Both of the text editors will automatically generate the selected file for you if it doesn’t already exist. Unfortunately, Nano is not available by default in some Linux variants. Just read our instructions for installing and using Nano in Linux.

16. Display the Last Used Commands

One of the main SSH commands is the history command.  The most recent commands used are shown using the history command. For the results to be limited, you must input a number. For instance:

history 10

The example will display the 10 most recent instructions entered.

17. Create or Extract Files

tar command is an SSH command that creates or extracts .tar.gz files. Because the majority of third-party software binaries are in the .tar.gz format, it is very well-liked.

Use the following command to archive a folder in .tar.gz format:

tar cvzf ArchiveName.tar.gz /path/to/directory

Run the command below to unpack a .tar.gz file:

tar xvzf FileName.tar.gz

18. Download Files from the Internet

To download files from the internet, use the wget command. For instance, you can use the following command to download a file from a website and store it in your current directory:

wget http://fileurl/filename.ext

Put all URLs into a file and use the -i option to download several files at once.

19. View the Size of Files/Folders

To view the size of files and folders in a specific directory, use the du (Disk Usage) command:

du [directory path]

Unfortunately, disk block numbers rather than bytes, kilobytes, and megabytes will be displayed in the summary. Consequently, you must add the -h option to the du command in order to display it in a human-readable format:

du -h /home

20. Enable Compression

The gzip compression method, which compresses all conceivable data streams including stdin, stdout, stderr, and others, is supported by SSH. When using slow network connections, this feature is really helpful.

To enable the compression on SSH, you can use the -C option:

$ ssh -C -l root Your IP 'hostname'

21. Change Default SSH Port

As we mentioned, SSH uses TCP port 22 by default. If you prefer to change it, you can configure SSH to run on your considered port. Open the /etc/ssh/sshd_config file and use your considered port value with the Port directive. Here, we select 8088 for instance.

Port 8088

Then, to terminate the session, use the commands below to restart the sshd service:

# systemctl restart sshd
# exit

And, finally to go to the remote host, type:

$ ssh -p 8088 -l root Your IP

In this instance, the port number was specified using the -p option.

22. Disable SSH Root Login

While you are using the root user to access the remote host, you can disable the root user login since root user access is always restricted to improve security. Run the command below to disable the root user login after opening the /etc/ssh/sshd_config file and use the option no with the PermitRootLogin directive:

PermitRootLogin no

Use the following commands to restart the sshd service and terminate the session:

# systemctl restart sshd
# exit

To verify the recent action, you can create a new SSH session:

$ ssh -l root your IP

There, you must see that it is not possible to log in to the remote host with the root user. Use the option yes with the same directive to allow root user login.

SSH Command Options

SSH OptionsDescription
-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 Use this option on the local host with more than one address to set the source address of the connection.
-C
Enables data compression for all files. Only to be used with slow connections.
-c Use to select a cipher specification. List the values separated by a comma.
-E 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.

FAQ

To delete a directory, add the -r flag to the rm command.

 

It happens when the user is not authorized to log into the server. It is mainly due to the sshd_config file that contains SSH server configuration.

Troubleshooting steps:

  • Navigate to /etc/ssh/sshd_config Make sure PasswordAuthentication and PubkeyAuthentication end with yes.
  • Make sure ChallengeResponseAuthentication and PermitRootLogin end with no.
  • Restart sshd_config.

The Hostname or IP Address field in Putty should be filled up with your hostname or IP address. Port 22 will be used by default. The command line window will open once you click the Open button. At the login as prompt in the command line window, type the SSH username and press Enter on your keyboard.

 

It occurs when incorrect credentials cause an ssh connection attempt to fail. Make sure the password or user name is accurate.

From your Site Tools > Devs > SSH Keys Manager, you can allow SSH access. Once the tool is launched, you can create a fresh set of SSH keys. To configure an SSH connection from your local computer to the server, you will require these keys.

Conclusion

With an additional layer of protection, you may now administer your server remotely and have access to these instructions. Consider that you can combine the flags to obtain the desired results. All 22 Main SSH commands might help you better understand your applications, whether you are a novice developer or want to manage your own application. These SSH commands can also assist you in explaining issues to sysadmins as you try to figure out why a particular program might run correctly locally but not on a distant system. These commands work on bare metal, containers, virtual machines (VMs), and Linux development environments.

Now, doing simple operations on your remote machine, such as adding and removing files, navigating between folders, and so forth, is simple.

Leave a Reply

Your email address will not be published. Required fields are marked.