How to use the command rexec (with examples)
- Linux
- December 25, 2023
rexec is a command-line tool used to execute commands on a remote host. It allows users to run commands on a remote host without having to manually log in to that host. However, it is important to note that rexec transmits data in plain text, making it less secure compared to alternative methods like ssh, which provides encrypted communication.
Use case 1: Execute a command on a remote [h]ost
Code:
rexec -h=remote_host ls -l
Motivation:
The motivation behind using this example is to run the ls -l
command on a remote host without having to manually log in to that host. This can be useful when you want to quickly view the detailed information of files and directories on a remote system.
Explanation:
rexec
: The command-line tool that is used to execute commands on a remote host.-h=remote_host
: Specifies the hostname or IP address of the remote host.ls -l
: The command that will be executed on the remote host. In this example, it lists the files and directories in long format.
Example output:
-rw-r--r-- 1 user group 1234 Jan 1 2022 file.txt
drwxr-xr-x 2 user group 4096 Jan 2 2022 directory
Use case 2: Specify the remote [u]sername on a remote [h]ost
Code:
rexec -username=username -h=remote_host ps aux
Motivation:
The motivation behind using this example is to specify a specific username when executing a command on a remote host. This can be useful when you want to run commands as a different user on the remote system.
Explanation:
rexec
: The command-line tool that is used to execute commands on a remote host.-username=username
: Specifies the username to be used when connecting to the remote host.-h=remote_host
: Specifies the hostname or IP address of the remote host.ps aux
: The command that will be executed on the remote host. In this example, it lists all running processes on the remote system.
Example output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 4428 1988 ? Ss Jan02 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S Jan02 0:00 [kthreadd]
...
Use case 3: Redirect stdin
from /dev/null
on a remote [h]ost
Code:
rexec --no-err -h=remote_host ls -l
Motivation:
The motivation behind using this example is to redirect the standard input (stdin
) from /dev/null
when executing a command on a remote host. This can be useful when you want to suppress any error messages that might be produced during the execution of the command.
Explanation:
rexec
: The command-line tool that is used to execute commands on a remote host.--no-err
: Specifies that any error messages produced during the execution of the command should be suppressed.-h=remote_host
: Specifies the hostname or IP address of the remote host.ls -l
: The command that will be executed on the remote host. In this example, it lists the files and directories in long format.
Example output:
-rw-r--r-- 1 user group 1234 Jan 1 2022 file.txt
drwxr-xr-x 2 user group 4096 Jan 2 2022 directory
Use case 4: Specify the remote [P]ort on a remote [h]ost
Code:
rexec -P=1234 -h=remote_host ls -l
Motivation:
The motivation behind using this example is to specify a specific port when connecting to a remote host. This can be useful when the remote host is listening on a non-default port.
Explanation:
rexec
: The command-line tool that is used to execute commands on a remote host.-P=1234
: Specifies the port number to be used when connecting to the remote host.-h=remote_host
: Specifies the hostname or IP address of the remote host.ls -l
: The command that will be executed on the remote host. In this example, it lists the files and directories in long format.
Example output:
-rw-r--r-- 1 user group 1234 Jan 1 2022 file.txt
drwxr-xr-x 2 user group 4096 Jan 2 2022 directory
Conclusion:
The rexec command is a useful tool for executing commands on a remote host without having to manually log in. However, it is important to consider security implications and use encrypted communication methods like ssh when transmitting sensitive data. By providing various options and arguments, rexec allows users to customize the execution of commands on remote hosts for different use cases.