How to use the command 'rexec' (with examples)
- Linux
- December 17, 2024
The rexec
command allows users to execute commands on a remote host. By using rexec
, users can run specified commands on another machine without physically accessing it. However, it’s crucial to note that rexec
transmits data in plain text, making it insecure for sensitive operations. Secure alternatives, such as SSH, are recommended when encryption is necessary.
Execute a command on a remote host:
Code:
rexec -h=remote_host ls -l
Motivation:
This basic use case of rexec
is useful for system administrators or users who need to check the contents of a directory on a remote server. By executing ls -l
, the user can quickly access a detailed list, including permissions, owner, size, and modification date of each file within the directory.
Explanation:
rexec
: Invokes the remote execution command.-h=remote_host
: Specifies the hostname or IP address of the remote server where the command will be executed.ls -l
: The command to be executed remotely, which lists directory contents in long format.
Example output:
-rw-r--r-- 1 user group 1024 Oct 21 12:34 file1.txt
drwxr-xr-x 3 user group 4096 Oct 21 12:00 DirA
Specify the remote username on a remote host:
Code:
rexec -username=username -h=remote_host ps aux
Motivation:
In scenarios where multiple users have access to a remote system, specifying a username is vital for executing commands under the correct user context. This is especially important for monitoring processes, as ps aux
provides detailed information about running processes, including process IDs and memory usage.
Explanation:
rexec
: Executes a command on a remote server.-username=username
: Indicates the username for authentication on the remote host.-h=remote_host
: Points to the specific remote server.ps aux
: A command to display all currently running processes in a detailed view.
Example output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 15844 3128 ? Ss Oct21 0:03 init
username 784 0.1 0.5 101564 10520 ? Ssl Oct21 1:34 task-manager
Redirect stdin
from /dev/null
on a remote host:
Code:
rexec --no-err -h=remote_host ls -l
Motivation:
Redirecting stdin
from /dev/null
can prevent interactive prompts or unintended keyboard interrupts when executing commands remotely. This approach is beneficial when automation and scripting require uninterrupted execution without human intervention.
Explanation:
rexec
: The command for remote execution.--no-err
: An argument to suppress error messages by redirectingstdin
from/dev/null
. This is helpful when the command should run non-interactively.-h=remote_host
: Identifies the target remote machine.ls -l
: The command being executed on the remote server.
Example output:
-rw-r--r-- 1 user group 1024 Oct 21 12:34 file1.txt
drwxr-xr-x 3 user group 4096 Oct 21 12:00 DirA
Specify the remote port on a remote host:
Code:
rexec -P=1234 -h=remote_host ls -l
Motivation:
In network configurations where services are bound to specific ports, specifying the port ensures that commands are directed to the correct service endpoint. This is particularly important for servers running multiple services on different ports, ensuring precise communication.
Explanation:
rexec
: Utilized for command execution on a remote system.-P=1234
: Specifies the port number on which the remote service is listening.-h=remote_host
: The destination remote host for executing the command.ls -l
: The actual command performed on the remote server.
Example output:
-rw-r--r-- 1 user group 1024 Oct 21 12:34 file1.txt
drwxr-xr-x 3 user group 4096 Oct 21 12:00 DirA
Conclusion:
The rexec
command, albeit outdated and insecure for transmitting sensitive information, offers a straightforward way to execute commands on remote systems. Its various use cases, highlighted above, demonstrate flexibility in using remote hosts, specifying user credentials, handling input redirection, and managing port configurations. For secure remote operations, consider using alternatives like SSH to ensure encrypted communication and safeguard data integrity.