Search code examples
linuxcommand

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.


Solution

  • 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/