Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux execute command remotely

Tags:

linux

command

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.

like image 314
user121196 Avatar asked Mar 02 '11 01:03

user121196


People also ask

How do I run a Linux command remotely?

Syntax for running commands on a remote Linux or Unix hostssh : The ssh (or other SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. USER-NAME : Remote host user name. REMOTE-HOST : Remote host ip-address or host name, such as fbsd.cyberciti.biz.


2 Answers

I guess ssh is the best secured way for this, for example :

ssh -OPTIONS -p SSH_PORT user@remote_server "remote_command1; remote_command2; remote_script.sh"   

where the OPTIONS have to be deployed according to your specific needs (for example, binding to ipv4 only) and your remote command could be starting your tomcat daemon.

Note:
If you do not want to be prompt at every ssh run, please also have a look to ssh-agent, and optionally to keychain if your system allows it. Key is... to understand the ssh keys exchange process. Please take a careful look to ssh_config (i.e. the ssh client config file) and sshd_config (i.e. the ssh server config file). Configuration filenames depend on your system, anyway you'll find them somewhere like /etc/sshd_config. Ideally, pls do not run ssh as root obviously but as a specific user on both sides, servers and client.

Some extra docs over the source project main pages :

ssh and ssh-agent
man ssh

http://www.snailbook.com/index.html
https://help.ubuntu.com/community/SSH/OpenSSH/Configuring

keychain
http://www.gentoo.org/doc/en/keychain-guide.xml
an older tuto in French (by myself :-) but might be useful too :
http://hornetbzz.developpez.com/tutoriels/debian/ssh/keychain/

like image 73
hornetbzz Avatar answered Oct 05 '22 03:10

hornetbzz


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

or you can just

 ssh user@machine "remote command to run"  
like image 34
Brandon Frohbieter Avatar answered Oct 05 '22 05:10

Brandon Frohbieter