Execute 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. 

Here at LinuxAPT, we shall look into how to run a command remotely with SSH.

Steps to Run a Command Remotely with SSH

1. Generate the SSH key pairs

To begin, you need to generate the SSH key pairs by running the below commands:

$ ssh-keygen –t rsa

The generated key's image appears on your terminal after executing this command.

2. Modify the SSH Configuration File

Now, you need to do some modifications within the configuration file of SSH by running the following command:

$ sudo nano /etc/ssh/sshd_config

This will enable you to access the SSH configuration file.

Next, 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.

3. Restart the SSH Service

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

$ sudo service ssh restart

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:

$ 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 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

5. Run the PWD Command Remotely with SSH

Next, you can also run the "pwd" command on a remote machine with SSH:

$ ssh system2@server-VirtualBox pwd

Here, 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.

[Need help in fixing Linux system issues ? We can help you. ]

This article covers the process of running a remote command with SSH on a Linux system along with actually running a command remotely. In fact, 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.

Related Posts