The rsh command is one of the older commands in UNIX/Linux. It allows you to execute commands on the remote machine from the local machine. You do not have to log in or switch users in the remote system to run commands. rsh connects to the remote host and executes the specified commands.
In this article, we will explain how to use rsh command and execute commands remotely in Linux.
Install rsh package
The rsh-redone-client and rsh-redone-server are the newest versions of rsh packages. They are the reimplementation of the older versions: rsh-client and rsh-server.
Run the following command to install rsh command on your Linux system.
For Debian/Ubuntu
sudo apt install rsh-redone-client rsh-redone-server
For CentOS/RHEL/Fedora
sudo dnf install rsh-redone-client rsh-redone-server
Syntax to use rsh command
The basic syntax for rsh command is as follows:
rsh [option] host [command]
Prerequisites
To follow along with this tutorial, you will need an Ubuntu system with a non-root user having sudo privileges. The commands described in this tutorial have been executed on the terminal of Ubuntu 20.04 LTS. The same commands can be used for other Linux distributions.
Before you start using rsh command, you will have to make the below changes in your local and remote systems.
Add hostname in /etc/hosts
You have to include the name of remote host in the /etc/hosts
file of the local computer and local hostname in the /etc/hosts
file of the remote computer.
For example, if the hostname of your remote computer is kali
and IP address is 192.168.56.102
then your local computer’s /etc/hosts
file must contain 192.168.56.102 kali
.
You can use the nano command to edit the file content. You will need the sudo privilege to edit the /etc/hosts
file.
$ sudo nano /etc/hosts
Similarly, if the hostname of your local computer is ubuntu-PC
and the IP address is 192.168.56.106
then your remote computer’s /etc/hosts
file must have a line 192.168.56.106 ubuntu-PC
.
Add users in $HOME/.rhosts
In the local user’s home directory, you have to create a .rhosts
file and add the login name of remote users which will be used to log in to the remote computer.
Suppose you have two users linuxhint
and kali
in the remote computer having an IP address 192.168.56.102
. Then you have to add the below lines to a .rhosts
file in the local user’s home directory.
192.168.56.102 linuxhint 192.168.56.102 kali
Similarly, you have to add the login name of the local users in the remote user’s home directory.
If you have a user linuxhint
in the local machine, you have to add login names to .rhosts
file in both remote user’s home directories.
192.168.56.106 linuxhint
Login to a remote shell
If no command is specified to execute in the remote host, you will be logged in on the remote shell with rlogin
.
The following command will launch the terminal session on the remote host kali
as the same user on the local machine.
rsh 192.168.56.102
It will prompt you to enter the password of user linuxhint
on the remote host.
To exit the remote session, execute the exit
command.
exit
Run commands on the remote computer
The hostname
command displays the computer’s hostname in which the command is executed.
The following command executes the command hostname
on the remote computer.
rsh 192.168.18.57 hostname
As a result, it displays the hostname of the remote computer. Likewise, you can run other many commands on a remote host. For example, you can run ls
command to see files and directories on the remote computer.
The rsh command exits after the remote command is terminated.
Run commands as a different user
The above examples use the same username as the local machine to connect to the remote host, i.e, linuxhint
.
You can specify a username with the -l
option to log in as a different user.
The following example executes the whoami
command on a remote host as a user kali
.
rsh -l kali 192.168.56.102 whoami
The ls
command lists files and directories of the current working directory, which is the user’s home directory by default.
Run multiple commands in the remote computer
You can run multiple commands on the remote system by separating them with semicolons ;
or &&
symbols. But you have to put commands in single ''
or double quotes ""
.
If the multiple commands are not enclosed in single or double quotes, then the first command will execute on the remote host and others on the local machine.
For instance, to run pwd
and ls
command on a remote host, you can type these commands on your local system.
rsh -l kali 192.168.56.102 "pwd ; ls"
OR
rsh -l kali 192.168.56.102 "pwd && ls"
As you can see, it displays the working directory and lists folders inside it.
When using the &&
separator, the second command will only work if the first command is run successfully.
In this case, when using &&
separator, the cat
command is not executed successfully, so the second command ls
did not run on a remote host. But with ;
separator, the second command is executed even after the first command did not run successfully.
Run commands with sudo privileges on the remote system
Some commands in Linux require sudo privileges to execute, like adding or removing users, installing or removing packages, viewing the root directory, etc. By default, rsh command does not ask for the sudo password and shows the error saying the password is required.
You have to use the flag -S
with sudo command to prompt for the password and read a password from the standard input.
Run local scripts on the remote system
This command runs the remote script file seq.sh
from the local system.
rsh -l kali 192.168.56.102 bash /home/kali/Documents/seq.sh
For running local scripts on the remote system, you have to use the following syntax.
rsh -l user remotehost 'bash -s' < script.sh
Suppose you have host.sh
file in the local computer which contains the below lines.
#!/bin/bash hostname echo $HOME ls
Then you can execute a local script on the remote system by issuing the following command.
rsh -l kali 192.168.56.102 'bash -s' < host.sh
Save the remote system’s output to the local system
You can also run commands on the remote system and save the output to the local system. For example, this command will execute ps
command on the remote system and save its output to a file process.txt
in the local machine’s directory /home/linuxhint/record
.
rsh -l kali 192.168.56.102 ps > ~/record/process.txt
Save content of the remote file to the local file
Using the above method, you can also save the content of the remote file to the local file. The following example executes cat hello.txt
on the remote system and save its output to a file hello.txt
on the local system.
rsh -l kali 192.168.56.102 cat hello.txt > hello.txt
Append content of the remote file to the local file
Sometimes, you may need to add content of the remote file to the local file without replacing the previous versions. The >>
operator allows you to append the remote file’s content to the local file.
The following command runs cat new.txt
on a remote host and appends its output to the previously created local file hello.txt
.
rsh -l kali 192.168.56.102 cat new.txt >> hello.txt
Run interactive commands on the remote host
The rsh command cannot run interactive commands like top, vi, nano, etc. For running interactive commands on a remote host, you have to log in to the remote shell.
You can use the rlogin
command or rsh without no commands to log in to the remote host.
rlogin -l kali 192.168.56.102
OR
rsh -l kali 192.168.56.102
On the remote terminal session, you will be able to execute interactive commands.
Conclusion
By now, you should have understood how to use rsh commands in Linux. rsh is intended to be replaced by the ssh command. rsh is not secure as it does not use any encryption for data transfer. ssh is highly recommended for a secure connection. The command-line use of the ssh command is similar to the rsh command.
Discussion about this post