130

I am using this command on Ubuntu but it is starting on port 8080 and I don't have another server running so I'd like it to start on port 80. I saw ways that you could set up a bash script to do something like this, but isn't there a command line flag or something simpler to specify the port?

python -m SimpleHTTPServer
1
  • 4
    Are you running as root? You can't listen on a port <1024 unless you have root privileges - and I would strongly advise against that. Commented Nov 14, 2011 at 19:53

3 Answers 3

228

sudo python -m SimpleHTTPServer 80

for python 3.x version, you may need :

sudo python -m http.server 80

Ports below 1024 require root privileges.

As George added in a comment, running this command as root is not a good idea - it opens up all kinds of security vulnerabilities.

However, it answers the question.

9
  • 8
    I might also add that running this command as root is not a good idea - it opens up all kinds of security vulnerabilities. Commented Nov 14, 2011 at 19:56
  • 2
    @cwd :) The TCP/IP port numbers below 1024 are special in that normal users are not allowed to run servers on them. This is a security feaure, in that if you connect to a service on one of these ports you can be fairly sure that you have the real thing, and not a fake which some hacker has put up for you.
    – jrg
    Commented Nov 14, 2011 at 20:17
  • 4
    @cwd If you want to run the server on a port <1024, use iptables to route incoming connections to a port ≥1024. For testing, do something like sudo nc -l 80 (netcat). And see Why are the first 1024 ports restricted to the root user only? Commented Nov 14, 2011 at 23:42
  • 1
    @jrg A port <1024 only matters if you trust the sysadmin on the server but not other users. It's near-userless outside a few local networks nowadays: most servers aren't multiuser machines. Commented Nov 14, 2011 at 23:43
  • 3
    This is a related question about how to drop privileges once you've opened port 80.
    – jcollado
    Commented Nov 21, 2011 at 22:43
6

Do something like :

python -m SimpleHTTPServer 50505

Where 50505 is the port number.

0

I'm very late for this response: In Linux, I simply forwarded port 80 to another port e.g. with nftables:

table ip nat {
        chain prerouting {
                type nat hook prerouting priority dstnat; policy accept;
                tcp dport 80 dnat to 192.168.1.1:8080
        }
}

the benefit here is the web server practically don't need any root permissions etc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .