en
  English
Phone :  +370 (5) 204-1903
Email :  sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. What is SSH command

What is SSH command

If you are a Linux administrator, you might have used SSH commands to carry out various tasks. It is a secure network protocol that will help establish secure connections between different machines connected over the internet. Many system administrators use the SSH utility to manage devices and securely transfer the files between the systems. 

Security Tutorials Sep 24, 21 by Nisal N 10 min Read
What is SSH command

This article mentions various SSH commands in Linux that will help you perform multiple functions on the Linux system. You can run these commands using the command-line interface. Let’s go through a comprehensive list of useful commands.

Prerequisites

Before starting, you need to maintain some prerequisites for running the SSH commands via the command line. 

  • An SSH client that is suitable for working.
  • An SSH server is installed and configured on the remote machine
  • The IP address of the remote connecting server

Accessing a Remote Server via SSH

If you want to establish a secure connection to a remote machine, then all you need is the IP address of the remote machine. Then it would be best if you opened the command-line terminal (SSH command line) or the SSH client for running the below command, followed by the IP address or the name of the client machine.

ssh 192.168.56.101

or name:

ssh test.server.com

If you are connecting to the remote machine for the first time, you will get the message below. If you want to proceed, then you need to type yes and hit enter. Then you will be asked to enter the password for the client machine.

Specifying a Username for SSH connection

The SSH will use the current user whenever you try to access the remote server. If you want to specify a particular user for an SSH connection, you can run the below-mentioned command from the terminal.

ssh username@hostname_or_ip

Example- 

ssh testuser@10.0.0.52

Using a Different Port Number for establishing an SSH Connection

By default, any server on the network will listen to port 22 while establishing a connection. But, if there is a change in the port setting in the SSH configuration file, you have to mention the changed port in the command specifically; otherwise, you will get an error connection refused, as shown below.

If you want to establish a connection to a remote host with a specified SSH port number, then you can use the -p flag as shown below.

ssh test.server.com -p 3324

Generating the SSH Keys Using SSH Keygen

To improve the SSH connections security, you can even generate a key pair using the keygen utility via SSH command. The key pair will include both the public and the private keys. The public key will be shared while the private key is kept secure. 

These keys are used for authenticating the connecting clients to the server. Whenever you create an SSH key pair, you do not have to enter the password each time you establish a connection to the server. 

For creating the key pair, you can run the following SSH keygen command on the terminal of the host machine. You can use the same command to create an SSH key in Ubuntu and other Linux distributions. 

ssh-keygen -t rsa

If you want to have the default settings, you can hit enter whenever you get the prompt for the file location and passphrase.

Copying the Public SSH Key

To use the key pair authentication, you must copy the available public key to the server. The key is in the form of the file id_rsa.pub, which is created using the keygen utility in the previous command.

If you want to copy the key to the server, you can run the following command from the client machine.

ssh-copy-id hostname_or_IP

If you do not want to use the current user, then you can specify the particular user you want to use. You will be requested to enter the password for the first and last time then the connection will be made without any password requirement. 

Copying a File Remotely over SSH using SCP

If you want to securely copy files from SSH to local over the SSH protocol, you can do it using the SCP tool. Below is the basic syntax that you can use.

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

Suppose you want to copy a file named “sample3” to your desktop on the remote server with the specific username, then you can use the following command.

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

Make sure you use the uppercase -P flag for specifying the particular port when you want to copy file SSH.

Editing the SSH Config File

By making the required changes to the SSH config file, you can control how the users can access a server using SSH. You can customize the SSH server options using the sshd_config file. Make sure to make the correct changes; else, the server will be inaccessible to you.

For making the changes, you can use the Vim editor. But, you need to have superuser permission to make the required changes. You can run the following command on the remote host to proceed with the change.

sudo vim /etc/ssh/sshd_config

Now you need to provide the sudo password, and the shell will open the file in the editor you want to change.

Restarting the SSH service

Once you make the SSH config changes, you need to restart the services in Linux to reflect the latest changes to the system. You can run one of the following commands depending on the type of Linux distro you are using.

sudo ssh service restart

or:

sudo sshd service restart

Then, you need to provide the password for completing the process.

Basic SSH Commands

If you are about to work on the remote server, you must know some of the basic commands that will be beneficial to managing the remote host.

1. Checking Working Directory Path

You can use the following command if you want to check the current working directory of the user. The output will display the directory’s location.

2. Listing all the Files and Directories

If you want to list the contents of the current working directory, you can use the ls command. You can use the below-mentioned flags with the ls command for different functionality.

  • -a will help display the hidden files and entries that start with a dot.
  • -l will help in showing the complete file details for directory contents such as permissions, ownership, date, etc.
  • -s it will list the size of files in blocks. You can even add the flag -h to display the size in a humanly readable form.

If you want to navigate from one folder to another, you can use the cd (change directory) command as shown below.

cd Desktop/Downloads/Test

Always remember that the names are case-sensitive. 

3. Changing Directory

You can use the cd command carefully, and it comes in variants, as shown below.

  • cd .. it allows you to navigate to the directory one level higher than your current location.
  • cd - it will enable you to navigate back to the previous directory.
  • cd / it will enable you to navigate to the root directory.

4. Copying a File

If you want to copy a file from one location to another, you can use the cp command along with the file name, source, and destination as given below.

cp file_Name /directory/path/destination

If you want to copy the directory and its content, you need to use the -r flag with the command below, where r specifies the recursive.

cp -r Directory1 New_Location

5. Moving a File

The mv command will work almost in the same manner as the cp command. You can use the following syntax for the mv command.

mv file_Name directory/path/destination

6. Creating a File

You can use the touch command along with the new file name to create a new file. You also need to specify the file extension to specify the type of file to be created. Run the following command to create a file.

touch new_file_Name

7. Creating a Directory

For creating a directory, you can use the mkdir command along with a new directory name or full path in the below-mentioned format:

mkdir New_Directory_Name

Or:

mkdir directory/path/New_Directory_Name

8. Delete a File or Directory

For deleting a Linux file, you can use the rm in the below-mentioned format:

rm file_Name

or

rm /home/user/dir1/file_Name

9. Checking Network Information

If you want to check the network adapters’ status, you can use the ipconfig command. If you do not specify any option along with the ipconfig, then the output will show the active interfaces only.

10. Clearing the Terminal Screen

To clear the current working area of the bash screen, you can type clear on the screen and hit enter.

Running a Command on a Remote Server from a Local Computer

This method will not create a new shell but run a command and then return the user to the local prompt. Then you can run the desired commands.

If you want to run a command remotely from the local machine, then you need t make changes to the SSH command, as shown below.

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

Then, you need to enter the required password for the first time. Then the remote file will get deleted on the remote server without creating a new shell.

Conclusion

SSH is a common method for securely transferring data between devices over the network. You can use this command for executing various useful tasks that will ensure the security of the complete process. It allows you to manage your remote server with the appropriate access on the remote server. 

We have mentioned some of the commonly used SSH commands that will be used daily by the system administrator. Go through these commands, and you will understand how SSH commands work.

People also read: 

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment