The remote shell (rsh) Linux command allows the execution of a single command on a specified remote machine. Rsh remotely connects to the specified host, and you can only execute a selected one command per given time.
Using the rsh is beneficial when you are not required to stay logged in to the remote machine. If you know which command to specify and the path to it, then rsh is perfect for the job. We will discuss the use of rsh to connect to your remote machine using different examples. Let’s get started!
How to Execute Command Remotely With Rsh
The rsh command is easy to use. You only need the machine name or its IP and the command.
1 | $ rsh machinename/ip command |
The rsh command works similar to the rlogin and rcp commands. It accesses the remote machines configured in the etc/hosts file. However, if the remote machine is not specified in the file, you can directly invoke it using its hostname and IP.
Connecting the Remote Machine Using the Hostname
To connect to a remote machine, you need to know its hostname. In our case, our remote machine is kyle. So, to connect to it using the hostname, the command is as follows:
1 | $ rsh kyle ls ~/Desktop |
The previous command lists the contents of the Desktop directory.
If we open the remote machine, we can see the available files on the Desktop/.
Let’s execute the remote command, and we will get the same files.
If you get the error reflected in the previous image, it implies that the hostname is not defined in your /etc/hosts file. So, go ahead and open the file, add the IP address of the remote machine and the name. In our case, the file is as follows:
1 | $ sudo nano /etc/hosts |
Save the configuration and run the command again. You should now see the connection established. We can see our Desktop files in the following output:
Note that you still need the password to the remote machine. Once the command is executed, you have to re-run it again if you need to use another command.
Connecting the Remote Machine Using the Hostname and IP
In the previous method, you need to add the remote machine’s name to the list of trusted devices under the /etc/hosts file. However, there is another way of connecting to the same remote machine and executing one command without saving its details. You will need the exact IP of your remote device and its hostname.
Our remote machine is in Virtual Box, so we have the same local IP that we used in the previous method.
Use the command below to connect to the remote machine and still list the files in the Desktop/:
1 | $ rsh -l <hostname> <ip> ls ~/Desktop |
We see that we still access the same files in the output.
Even with this method, you still need the password for the remote machine.
Saving the Output of the Remote Machine to the Local Machine
The main point of connecting to the remote machine is to execute a command. If the command returns an output, you can pipe it to the local machine.
For instance, if we need to save the contents of the Desktop/ instead of listing them as we did in the previous methods, the new command is as follows:
1 | $ rsh kyle ~/Desktop > new1.txt |
A new file, new1.txt, is created. If we list its contents, we see the files on the Desktop of the remote machine.
You can use the same concept to execute any command on the remote machine.
Executing Commands to the Remote Machine Using Rsh
So far, we’ve seen how we can run a command that displays the output to the local machine. What if you want to manipulate the files or text to the remote device instead? That’s also possible.
For instance, let’s move the contents on the Desktop/ file of the remote machine to a new folder.
1 2 3 | $ rsh kyle mkdir ~/Desktop/testfolder $ rsh kyle mv -v ~/Desktop/*.txt ~/Desktop/tesffolder/ |
Everything worked as expected.
Conclusion
We’ve seen how to use the rsh command to access and run one command to a remote machine using its hostname and IP. As discussed in the article, the rsh command is helpful when you need to execute commands without creating a permanent connection to your remote device.