We can run commands using the SSH command to manage the other machine remotely. The SSH is the secured shell used to transfer the data and manage the machine connected to the SSH. Different commands are run in the remote machine to manage administrative tasks, saving the user’s time.
This blog will demonstrate how to connect with another machine using SSH, and also to run commands and then exit the remote machine using SSH; moreover, the contents of this blog are:
- Prerequisite: Install and Configure SSH
- Navigate to Remote and Then Run SSH Commands
- Run Commands in Remote Machine From the Host Machine
Let’s start the post from prerequisites section:
Prerequisite: Install and Configure SSH
To understand running the commands and exiting from the computer using SSH, ensure SSH is installed on both computers. If using the Ubuntu Machine, follow the link to install and enable SSH.
Method 1: Navigate to Remote Machine, and Then Run SSH Commands
The first method is to establish the connection with the Remote machine and then navigate to the remote machine. Run the commands in the machine and then exit the remote machine.
How to Establish the SSH Connection in Linux?
To establish the SSH connection between the host and the client machine, run the ssh command with the ip address of the client machine. The IP address can be found on the client machine by running the command:
$ ip a
Now, in the host machine, execute the command to establish the connection with the client machine:
$ ssh [email protected]
Type the “yes” to continue connecting with the machine:
Enter the Password of the connecting machine:
The connection has been established successfully.
How to Run Commands Using the SSH?
After accessing the remotely connected machine using SSH, we can run the commands. For example, we will list the contents of the connected machine:
$ ls
The contents of the remote machine have been listed down.
How to Exit the Machine Using the SSH?
To close the remote connection and exit from the remote machine, run the command:
$ exit
The connection has been closed.
Method 2: Run Commands in Remote Machine From the Host Machine
Another method of running the commands in the remote machine is running the commands in the host machine without navigating to the bash shell of the client machine. We will explain this in detail with the help of some examples.
To run the commands, we will follow the general syntax of the ssh command:
$ ssh [username@ip address of the client machine] [command]
All the examples in the next section will follow the above general syntax.
How to Run Single Command Using the SSH?
We will simply follow the mentioned above general syntax to run the single command. For example, to list down the contents of the remote machine:
$ ssh [email protected] ls
All the contents are being listed down on the remote machine.
How to Run Multiple Commands Using the SSH?
To run multiple commands using SSH, run all the commands between the double quotation marks “” separated by the semi-colon; For example, we will list the contents and display the time with the command:
$ ssh [email protected] “ls; time”
Both the commands have been executed successfully.
That’s all about running the commands in the remote machine using the SSH command and exit.
Conclusion
To run the SSH command for executing different commands in the remote machine, enter the remote machine’s shell with “ssh user@ip_address”. In this blog, two different approaches to accessing the host machine remotely using the SSH command have been explained. Also, different examples have been used to explain the method of running SSH commands and exiting from the remote machine.