Categories: TutorialsUbuntu

Run Remote Command with SSH

SSH or Secure SHell can also be used for running the commands remotely over another system. All you need to do is to perform some prior configurations. Then, you can simply provide the username of the system on which you wish to execute a command remotely, followed by the command that you want to run on that system. This process will be explained in detail in this article.

How to Run a Command Remotely with SSH

To run a command remotely with SSH, you have to carry out the following steps:

Step 1: Generate the SSH Key Pairs

First, you need to generate the SSH key pairs with the help of the following command:

$ ssh-keygen –t rsa

The generated key’s image appears on your terminal after executing this command as shown in the following image:

Step 2: Perform Some Necessary Modifications to the SSH Configuration File

After that, you need to do some modifications within the configuration file of SSH. For that, you can access the SSH configuration file with the following command:

$ sudo nano /etc/ssh/sshd_config

Once you manage to open this file, you need to add the following lines to this file:

PasswordAuthentication yes

PermitRootLogin yes

After adding these lines to the SSH configuration file, you can save this file and exit from the Nano editor by pressing the Ctrl+ X key combination.

Step 3: Restart the SSH Service

Then, you need to restart the SSH service with the following command:

$ sudo service ssh restart

Step 4: Run the LS Command Remotely with SSH

Now, you are all set to run any command of your choice remotely by using SSH. We run the “ls” command on a remote machine in the following manner:

$ ssh system2@server-VirtualBox ls

Here, the “system2” is the name of a remote machine on which we want to run the “ls” command by making use of the SSH.

Once you run the previously mentioned command, you will be asked to enter the password for the remote system. After which, the results of the executed command appears on the terminal as shown in the following image:

Step 5: Run the PWD Command Remotely with SSH

Similarly, you can also run the “pwd” command on a remote machine with SSH in the following manner:

$ ssh system2@server-VirtualBox pwd

Again, the “system2” is the name of a remote machine on which we wish to run the “pwd” command by using the SSH.

Upon execution, the result of the “pwd” command appears on the terminal as shown in the following image:

Conclusion

This article discussed all the prerequisite steps of running a remote command with SSH on a Linux system along with actually running a command remotely for demonstration. After going through this method carefully, you will be able to run any command on a remote system of your choice provided that you know the correct username of that system as well as its password with which you will be able to access it remotely.

Ubuntu Server Admin

Recent Posts

Predict, compare, and reduce costs with our S3 cost calculator

Previously I have written about how useful public cloud storage can be when starting a…

23 hours ago

One Thread to Poll Them All: How a Single Pipe Made WaterDrop 50% Faster

This is Part 2 of the "Karafka to Async Journey" series. Part 1 covered WaterDrop's…

1 day ago

A year of documentation-driven development

For many software teams, documentation is written after features are built and design decisions have…

2 days ago

Announcing FIPS 140-3 for Ubuntu Core22

With the release of the FIPS 140-3 certified cryptographic modules for Ubuntu 22.04 LTS, Canonical…

3 days ago

The foundations of software: open source libraries and their maintainers

Open source libraries are repositories of code that developers can use and, depending on the…

6 days ago

From inspiration to impact: design students from Regent’s University London explore open design for their dissertation projects

Last year, we had the opportunity to speak at Regent’s UX Conference (Regent’s University London’s…

7 days ago