HowTo: Execute A Script On Remote UNIX / Linux Server

How do I execute a script on remote server called server2.example.com from my PC? How to run a shell/python/perl script on remote Unix-like systems?

You can use the ssh client which is a part of most *BSD/Linux/OS X/Linux distros/Unix-like oses. If you are on MS-Windows operating systems you need to download and use Putty client.[donotprint]
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Time1m
[/donotprint]

ADVERTISEMENTS

ssh command line syntax

The basic syntax is as follows:

ssh user@server-name-here /path/to/script
ssh user@server-name-here /path/to/script arg1 arg2 arg3

OR

ssh user@server-name-here /path/to/command
ssh user@server-name-here /path/to/command arg1 arg2

Examples

Open the Terminal. To run a script called /root/scripts/backup.sh on remote UNIX or Linux server called server1.cyberciti.biz, enter:

ssh root@server1.cyberciti.biz /root/scripts/backup.sh

You can pass command line arguments too:

ssh root@server1.cyberciti.biz /scripts/job.init --job=sync --type=aws --force=true

You can execute the date command as follows on remote server:

ssh user@server2.example.com date

You may need to specify full script path. In this example, run /root/backup.sh script as follows:

ssh user@server2.example.com /root/backup.sh

To avoid password prompt set password less ssh keys or use tool such as keychain – a manager for ssh-agent, typically run from ~/.bash_profile.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
13 comments… add one
  • Yong Cao Jun 21, 2010 @ 16:17

    This is very good and helpful stuff.
    Thanks a lot
    Yong Cao

  • jay P Mar 2, 2012 @ 4:49

    How can I add Password to this “ssh user@server2.example.com date”

    • webskein Mar 11, 2012 @ 16:43

      jay – you could try “passwordless ssh”. There’s stuff on Google about that.

  • Murryy Dec 11, 2012 @ 19:19

    will this work if the script on the remote is interative ? If not can anyone give more suggestions.
    Thank you

  • kavin Jun 10, 2013 @ 6:51

    Thanks working fine

  • M.C. Swart Nov 22, 2013 @ 13:08

    How to start this by a cronjob

  • Pranesh Nov 5, 2014 @ 15:03

    How to run these below at the same time. (Parallelly). Is it possible?
    by anyway is fine like (Perl/Python/shell)

    ssh user@server1 /root/scripts/backup.sh
    ssh user@server2 /root/scripts/backup.sh

    • Mahendra Feb 6, 2015 @ 5:15

      Put & at the end of the command as below. Ampersand will put the command in the background.
      ssh user@server1 /root/scripts/backup.sh &
      ssh user@server2 /root/scripts/backup.sh &

    • Vinoth Oct 11, 2015 @ 20:24

      Hi All,

      I have used following syntax to run a script in my remote system
      “ssh user@server1 /root/scripts/backup.sh ”
      There is a perl script inside my shell script, its failing to run while use the above syntax.
      i’m getting “No such file or directory” error at the line of perl script.

      Suggest the way forward..

      Thanks in advance

      • Anup Nov 17, 2015 @ 16:02

        Did you find a solution for this problem ?

      • Rasika Oct 4, 2016 @ 12:54

        Add the file path in your perl script or add a cd to change to the directory first. It may be trying to run the perl script from /root directory while the file is in /root/scripts directory.

  • Abhishek I Feb 23, 2015 @ 13:44

    Thank you.
    This helped me.

  • Jeff Sep 26, 2015 @ 4:04

    ssh requires remote login privileges accorded to a registered user on the server, as I understand it. Is this approach going to ask for login credentials, and how are those presented? My concern is
    having ssh login privileges along side ftp user login privileges, such as one would find on a remote
    web hosting service server for the web site administrator. Would I use this approach AFTER having logged into my account via ssh?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.