Running Commands on a Remote Linux / UNIX Host

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]


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


(A) Get disk information from a server called
$ ssh df -h

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

(C) Reboot remote host:
$ ssh reboot

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

(E) Get memory information and store result/output to local file /tmp/memory.status:
$ ssh '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 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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 24 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
24 comments… add one
  • Amitabh May 14, 2008 @ 10:23

    Hi ,

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



  • web designer Jul 22, 2009 @ 9:38

    I have just put this in a script in a cron job to keep the mysql databases in sync. Great.

  • web design Jul 22, 2009 @ 9:42

    Hi Amitabh, Have you tried “ssh htop”.

    Im not sure where the output would go but its worth a try if you havent already.


  • web design and seo Aug 4, 2009 @ 22:07

    Thank you, Ive been trying to do this for weeks

  • Sandy Dec 24, 2009 @ 5:42


    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.

  • Sandy Dec 24, 2009 @ 5:44

    continue to above post.

    Local host – aaa
    RemoteHost – sandy

  • venkat Jun 25, 2010 @ 21:24

    dude…I want to execute the following

    ssh user@server

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

    • Pramod Sep 22, 2010 @ 12:57

      You need to exchange the ssh keys between the remote and your machine from where you are doing this.

  • sarath Jun 27, 2011 @ 8:04

    Is any additional configurations are required to use ssh in Ubuntu

  • Raj Aug 26, 2011 @ 6:04

    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 username@hostname df -h

  • Siddhat Mar 5, 2012 @ 10:17

    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

  • saurabh Jul 6, 2012 @ 12:34

    hi , can any one tell me hw i can enter the password dynamically

  • Thyagaraj Sep 27, 2012 @ 8:56

    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 remote-user-here@remote-ip-here < /path/to/script
    sshpass -p secret ssh -o StrictHostKeyChecking=no root@ "ls -l"

  • rubence Sep 22, 2013 @ 12:44

    hello, how can i execute a script in remote machine keeping the script in local machine

  • rubence Sep 22, 2013 @ 12:51

    got it …

    for refrence

    ssh root@remotemachine sh <

  • maonato Oct 8, 2013 @ 9:58

    hi,, i want to run mu script to a server, the script is in add file
    will this command “ssh -i id_rsa@ ‘bash -s’ > ~.ssh/authorized_keys

  • tigerwhat Jan 20, 2014 @ 2:12

    Anyone have an idea of how to use this in with except/spawn?

  • SS Mar 24, 2015 @ 12:24


    How to run a a shell script in server 2 by passing a parameter from server1.


  • Nag Sep 22, 2015 @ 11:51

    My script requires few other commands to be executed after logging to the remote host.
    How can this be accomplished?

  • Anubhaw Oct 9, 2015 @ 14:44

    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.

  • ShOguN Feb 21, 2017 @ 2:03

    does anybody know how can you run this command??


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

    • 🐧 Vivek Gite Feb 21, 2017 @ 11:25

      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”

  • Mik Mar 10, 2017 @ 9:50

    What my requirement is in a script I execute this “ssh username@serverName “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 !

  • Anthony May 11, 2017 @ 6:24

    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?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum