Executing Linux / UNIX commands from web page

A Web interfaces is almost used by routers and many other sophisticated programs such as webmin. However, why go for a web interface or execute commands from web page? For automation purpose, you need to use a web interfaces. Another advantage is you can access your web-based interface from any computer, running any operating system, anytime in the world 😀

In this first part, you will see how to use simple bash (shell) script from web page. In order to execute commands or shell script from a webpage you need:

  1. CGI support with Apache / lighttpd web server.
  2. I’m assuming that you have a properly configured web server.

You need to store program in cgi-bin directory. If you are using Debian Linux default location for cgi-bin directory is /usr/lib/cgi-bin. Under Red Hat / Fedora it is /var/www/cgi-bin. Use text editor such as vi to create a first.cgi program:

$ cd /usr/lib/cgi-bin
$ vi first.cgi

first.cgi code listing:

echo "Content-type: text/html"
echo ""
echo "<html><head><title>Bash as CGI"
echo "</title></head><body>"

echo "<h1>Hello world</h1>"
echo "Today is $(date)"

echo "</body></html>"

Save and close the file. Setup execute permission on the script:

$ chmod +x first.cgi

Fire up your web browser and test the script, for example type url http://localhost/cgi-bin/first.cgi or http://your-ip/cgi-bin/first.cgi

You need to send headers, first three lines are almost same for all your script:

  • #!/bin/bash : First line tell Linux/UNIX how file first.cgi should be run. So it will use /bin/bash interpreter to execute your rest of program.
  • echo “Content-type: text/html” : Send html headers, you must include this line.
  • echo “” : Send a blank line, you must include this line.

Rest is html code. Take a close look at following echo command:

echo "Today is $(date)"

It use shell feature called command substitution. It allows the output of a command to replace the command name:


Your bash shell performs the expansion by executing command and replacing the command substitution. So date command get executed by replacing the its output.

Real life example

Here is simple script that collects system information. Create script in cgi-bin directory:

echo "Content-type: text/html"
echo ""
echo "<html><head><title>Bash as CGI"
echo "</title></head><body>"

echo "<h1>General system information for host $(hostname -s)</h1>"
echo ""

echo "<h1>Memory Info</h1>"
echo "<pre> $(free -m) </pre>"

echo "<h1>Disk Info:</h1>"
echo "<pre> $(df -h) </pre>"

echo "<h1>Logged in user</h1>"
echo "<pre> $(w) </pre>"

echo "<center>Information generated on $(date)</center>"
echo "</body></html>"

Save and close the file. Setup execute permission on script:

$ chmod +x script.cgi

Fire up web browser and test it (http://localhost/cgi-bin/script.cgi):

Next time you will see:

  • How to use and place form elements (from POSTs and GETs)
  • Cookies in your environment
  • Use of perl scripting
  • And finally use of special tools

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

🐧 43 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
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
43 comments… add one
  • Randal L. Schwartz Jan 6, 2006 @ 0:25

    You need to HTML-escape (add ampersands) your command output, or you might be accidentally providing a means for cross-site-scripting attacks.

  • 🐧 nixcraft Jan 7, 2006 @ 12:44

    Randal, thanks, I will cover this later in security issues 🙂 I appreciate your post.

  • 007ben Jan 18, 2006 @ 8:42

    I am struggling with creating a empty file a bash based cgi script. the filename should coome from information passed to the cgi from a forum.

  • 🐧 nixcraft Jan 18, 2006 @ 9:37

    007ben, I hope you have write permission to create a empty file.

    If you just want to retrieve value pass to url for example http://mydom.com/cgi-bin/script.cgi?filename

    Then use $1 in script to get filename i.e.
    touch $1

  • 🐧 nixcraft Jan 18, 2006 @ 9:49

    So if you are using url
    then following shell code should find out value of file:

    LINE=`echo $QUERY_STRING | sed ‘s/&/ /g’`
    for i in $LINE
    f1=$(echo $i | cut -d= -f1)
    v1=$(echo $i | cut -d= -f2)
    echo “Var is f1 and value is $v1”

  • 007ben Jan 18, 2006 @ 11:00

    that last bit was the trick!

    the forum was not passing the info with variable name.
    and when it did before the script could not pick it up.


  • Anonymous Feb 21, 2006 @ 19:11

    hello sir.,
    I am doing my project in linux.I want to know how to execute useradd and iptables commands in linux from PHP.How can i use the form data as arguments to the program.Please give the solution.

  • 🐧 nixcraft Feb 23, 2006 @ 16:09

    Use system(), exec() commands. For example to execute ls -l command you can write code:

    $out = system(“ls -l”);
    echo $out;

    Suppose you are using url http://localhost/page.php?user=raj

    Then you can use following code to get value of user:
    $u = $_GET[‘user’];
    $cmd = “adduser “. $u;
    $out = system($cmd);

  • Anonymous Aug 24, 2006 @ 7:03

    I m doing my project in my university network, which means that I m not the root. Hence I dont have any writtin permissions in cgi-bin directory. Is ther any other way out? I wud be greatfull if you can lemme knw the solution.
    thanks a lot

  • Jadu Nov 15, 2007 @ 9:21

    nixcraft is always best 🙂 Thanks, I am used to BASH, and your helps are always good for me. Is there any doc which tells more about cgi and bash together? like accepting user inputs, database access and all, will be a great help for me.

    Jadu, India

  • 🐧 nixCraft Nov 15, 2007 @ 12:48


    Although bash is handy, it is recommended that you use python or perl for cgi and sys admin related task.


  • olivier jolly Jan 9, 2008 @ 11:57

    I know this topic is a bit old, but I think it could be useful to alter the comment of nixcraft to be like this :


    echo "Content-type: text/plain"

    LINE=`echo $QUERY_STRING | sed 's/&/ /g'`
    for i in $LINE
    f1=$(echo $i | cut -d= -f1)
    v1=$(echo $i | cut -d= -f2)
    typeset -r $f1=$v1

    echo "hard coded content of t = '$t'"

    Like this, you are generating shell variables that you can use when you know in advance their name (as shown in the last line). It still lacks post support and decoding of escaped character but for simple script it can useful.

  • Ravi Jun 12, 2008 @ 4:51


    i want to run a “tail -f ” command using cgi to view unix log files progress in real time from browser.

    any ideas? or cgi script can do it?

  • Thor Henrik Jun 13, 2008 @ 12:35


    I have writen a bash script for work that accesses sevral servers and collect key information about them and creates a web page with the info.

    I would like to be able to run the shell script from the web page.
    Just to be able to do ./script.sh $1
    Is this possible

  • kwissem Jun 16, 2008 @ 23:02

    I want to open konsole and exute telnet and the konsole be opened from web page

  • Princess Jul 10, 2008 @ 11:14


    I have a bash script here that will create email accounts for each person that will request for 1.

    I want this script to be web based is this possible?

    Anyone can help me…


  • Ratnesh Bajpai Feb 11, 2009 @ 11:24

    Hello i want to know the way to creating a directory in linux through browser execution using php. thnx in advance

  • mailmado May 2, 2009 @ 5:51

    i want to run a simple mkdir command in cgi script which will take directory name from html page
    pl help.

  • Pex May 14, 2009 @ 9:13

    I want to install web server with php support. what are the steps to be taken??
    This is on Linux!!

    Pliz help!!

  • narf Oct 2, 2009 @ 0:36

    I get this on the /var/log/httpd/error.log
    Syntax error in type map, no ‘:’ in /var/www/cgi-bin/script.cgi for header “”n

  • apctr Nov 30, 2009 @ 8:26

    i want to execute vncviewer in web page.what should I have to do????????

  • berot3 Dec 12, 2009 @ 19:59

    does it work on mac os x?

    • 🐧 nixCraft Dec 13, 2009 @ 8:03

      Yes, it should work under Mac OS X too

  • berot3 Dec 14, 2009 @ 16:35

    yepp works on mac, just the path of the cgi is different: /Library/Webserver/CGI-Executables/
    but thats not what i was looking for 🙂
    maybe u can help me / maybe have heard of someting like this:
    i want to execute commands as if i would be a ssh-console but for http, u know what i mean?

    • 🐧 nixCraft Dec 14, 2009 @ 16:52

      This is going little offtopic. I suggest you post it to our forum along with shell code if you have any and will continue discussion at our forum http://nixcraft.com/

  • Guntur Jan 5, 2010 @ 11:55

    hi, my server is using CentOS.. where to put the cgi?

    I don’t understand anything bout cgi..
    but I want to execute a bash command from web page..
    like mkdir or unzip etc..

    please help me 🙂


  • Ben May 22, 2010 @ 11:45

    I was happy to find your information. I was able to add a line to start a linux program, with the right attributes. For developing a script within a webpage with buttons I moved to a Ubuntu PC because I was developing on an embedded linux board.

    Since I moved to the Ubuntu system your example still works but the line I placed in your example script does not.

    Can you help me out what could be my problem ? If you want to take a look, please send me a message and I will send you my problem.

    The command works from the command line in Ubuntu but not from the script…


  • Kirby Jun 24, 2010 @ 6:10

    Hi nixcraft, thanks for the Post.

    I’m very interested in the next topic in line after this. I notice this is really old, and sorry to dig up such an old topic.

    But, I’ve looked all over and can not find the next topic: How to use and place form elements (from POSTs and GETs)

    Can you please let me know if you ever wrote that one? Thanks!!
    I’d really like to read about that… more in dept how to control unix commands from a web page/url.

  • akshaya Mar 6, 2011 @ 3:31

    hello sir.,
    I am doing my project in linux.I want to know how to execute useradd and iptables commands in linux from PHP.How can i use the form data as arguments to the program.Please give the solution.


  • Chris Aug 31, 2011 @ 19:47

    Is there a way to execute a Linux command in index.html? Say for instance $HOSTNAME?

  • daniel Sep 28, 2011 @ 8:18

    Hi this site is just great.. Thumbs up nixcraft

  • goodslave Oct 16, 2011 @ 14:05

    I am writing an html page to to display the output of a shell script .
    but i need to pass usename & password from html page to shell script

    please help me to resolve this problem

  • Sai Nov 11, 2011 @ 13:25

    I am trying to execute perl script which accepts a parameter,through Perl CGI as
    system(“./remConnect.pl $var”);
    print “$? &nbsp”;

    $? returns 512. Can you please give a solution to make it run?


  • Venky$ Nov 18, 2011 @ 7:24

    wat r the websites to run unix commands

  • savy Dec 16, 2011 @ 4:28

    I Need to download Executing Linux / UNIX commands from web page

  • Aravind Dec 31, 2011 @ 12:26

    Whats a CGI ? i am new to this.. Please reply

  • prabhat Sep 29, 2012 @ 11:00

    Dear everyone
    plz help
    I have configured postfix with (mysql+ vmail+dovecot ) server.I have two server on different location.server A and B.
    primary(A) or secondary(B). I have some user on secondary(B) and some on primary(A).

    “admin@kvit.in” on primary server(A) But not Secondary server(B).When I send mail from server B. I get some errore USER UKNOWN.
    I have done in Sendmail through Luser relay.But i am unable to do in postfix

  • Nishant Sep 29, 2012 @ 11:16

    Dear sir

    i want to run “adduser” from web page how to make “useradd” or “change password” program i am newbie to linux.I need you help.

  • vibin Mar 8, 2013 @ 14:32

    My linux command is having a UI output.can i get that into html..

  • leejackee May 11, 2013 @ 13:47

    Thank you !!! very much my friend it’s a wonderful article.

  • boiboi Jul 17, 2013 @ 18:47


    my browser keeps telling me “error 404: file not found” from http://my-ip/cgi-bin/first.cgi

    i am using lighttpd in a debain(actually i am using a RaspberryPI)

    and i have configured my lighttpd.conf by adding this:

    fastcgi.server = ( “.php” => ((
    “bin-path” => “/usr/bin/php-cgi”,
    “socket” => “/tmp/php.socket”

    please help

    • ernest Aug 14, 2015 @ 9:00

      ensure that when you navigate to /usr/lib/cgi-bin you execute touch first.cgi (spelling is the issue and ensure you use root )

  • sunil Jul 11, 2016 @ 7:09

    I want to show up and their RAM ,CPU,HARDDISK on browser using Python program.please tell.

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