[linux] linux execute command remotely

how do I execute command/script on a remote linux box? say I want to do service tomcat start on box b from box a.

This question is related to linux command

The answer is


If you don't want to deal with security and want to make it as exposed (aka "convenient") as possible for short term, and|or don't have ssh/telnet or key generation on all your hosts, you can can hack a one-liner together with netcat. Write a command to your target computer's port over the network and it will run it. Then you can block access to that port to a few "trusted" users or wrap it in a script that only allows certain commands to run. And use a low privilege user.

on the server

mkfifo /tmp/netfifo; nc -lk 4201 0</tmp/netfifo | bash -e &>/tmp/netfifo

This one liner reads whatever string you send into that port and pipes it into bash to be executed. stderr & stdout are dumped back into netfifo and sent back to the connecting host via nc.

on the client

To run a command remotely: echo "ls" | nc HOST 4201


I think this article explains well:

Running Commands on a Remote Linux / UNIX Host

Google is your best friend ;-)


 ssh user@machine 'bash -s' < local_script.sh

or you can just

 ssh user@machine "remote command to run"