Running Commands on a Remote Linux / UNIX Host

Posted on in Categories CentOS, Debian Linux, FreeBSD, Gentoo Linux, Howto, HP-UX, Linux, Monitoring, Networking, OpenBSD, OS X, RedHat/Fedora Linux, Security, Shell scripting, Solaris, Tips, Ubuntu Linux, UNIX last updated July 25, 2005

You would like to execute a command on a remote Linux/FreeBSD/Solaris/UNIX host and have the result displayed locally. Once result obtained it can be used by local script or program. A few examples:
=> File system and disk information

=> Get user information

=> Find out all running process

=> Find out if particular service is running or not etc

You can use rsh or ssh for this purpose. However, for security reason you should always use the ssh and NOT rsh. Please note that remote system must run the OpenSSH server.

Syntax for running command on a remote host:
ssh [USER-NAME]@[REMOTE-HOST] [command or script]

Where,

  • ssh: ssh (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.
  • command or script: Command or shell script is executed on the remote host instead of a login shell.

Examples

(A) Get disk information from a server called www1.cyberciti.biz:
$ ssh [email protected] df -h

(B) List what ports are open on remote host
$ ssh [email protected] netstat -vatn

(C) Reboot remote host:
$ ssh [email protected] reboot

(D) Restart mysql server (please note enclosed multiple command line arguments using a single or double quotes)
$ ssh [email protected] '/etc/init.d/mysql restart'

(E) Get memory information and store result/output to local file /tmp/memory.status:
$ ssh [email protected] 'free -m' > /tmp/memory.status

(G) You can also run multiple command or use the pipes, following command displays memory in format of “available memory = used + free memory” :
$ ssh [email protected] free -m | grep "Mem:" | awk '{ print "Total memory (used+free): " $3 " + " $4 " = " $2 }'

See how to configure ssh for password less login using public key based authentication.

=> Related: shell script to get uptime, disk usage, cpu usage, RAM usage,system load,etc. from multiple Linux servers and output the information on a single server in a html format.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

24 comment

  1. Hi ,

    Can you please help to show “how to run top command in a remote host” ?

    Thanks,

    Amitabh

  2. Hi,

    I am trying to execute the script from my local to remote host.
    ssh sandy “/home/user/san/print_my_lines”

    The scripts works all right but not able to come out of remote host.

    Could you please help me out.

  3. dude…I want to execute the following

    ssh [email protected]

    it is asking for password….how to give password dynamically?

  4. How can we run a script on a remote machine without passwords?

    When i issue the following command, it is prompting for passoword. Could any one advise on this please ?
    ssh [email protected] df -h

  5. Can you please suggest how to check the system uptime for last 1year in Unix envirnment is there any parameter to check with uptime cammand which can show the logs of approximately 1 year

    Warm Regards ,

    Siddhant K

  6. For all who need password automation:

    There is a package called “sshpass” for password automation, you can search if it’s available in the Ubuntu repository by typing ‘apt-cache search sshpass’.

    You can install ‘sshpass’ by ‘apt-get install sshpass’.

    Using ‘sshpass’:
    sshpass -p remote-password-here ssh -o StrictHostKeyChecking=no [email protected] < /path/to/script
    (or)
    sshpass -p secret ssh -o StrictHostKeyChecking=no [email protected].1.2 "ls -l"

  7. got it …

    for refrence

    ssh [email protected] sh < local_script.sh

  8. how can i run multiple command on shell and then get the result on local in any file.for eq:-

    ssh -n $HOST dpkg -l | grep warehouse | awk ‘{print $3}’ >> d.txt; ps -ef | grep nginx | wc -l >> d.txt; ps -ef | grep unicorn | wc -l >> d.txt

    what is wrong here i am doing.

  9. does anybody know how can you run this command??

    /opt/xbt/tracker/.xbt_tracker

    i need to know the exact form of running it coz no matter what i try i get directory not found error in putty…!

    1. May be file does not exits. Login to your system and run:
      ls -l /opt/xbt/tracker/.xbt_tracker
      Do you get file name? If not try to find actual file path:
      find / -iname “.xbt_tracker”

  10. What my requirement is in a script I execute this “ssh [email protected] “unix command”
    If authorization key is set properly command will run but if it’s not set then my script will get stuck it will ask for password, My requirement is not to get script stuck, I don’t want to pass any password I simply want my script to continue if authorization key is not set.
    Thanks !

  11. In our organisation we use AIX, Solaris, Red Hat and SUSE. On AIX and Solaris we use KSH. On our Linux based systems we use BASH. Is there a way to figure out the OS without issuing a command so that we know which shell to use?

Comments are closed.