10

I believe the port 80 on my remote instance is blocked, and I am trying to run a node.js app using port 80. I have experimented with ports 3000 and 3002, and both ports are working fine, but I get an error when running on port 80. I suspect port 80 is blocked from my output of netstat -an below, but how can I find the process id's of the addresses that are blocking port 80 below?

[lucas@ecoinstance]~/node/nodetest1$ netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:3002            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:27017         127.0.0.1:51108         ESTABLISHED
tcp        0      0 127.0.0.1:51106         127.0.0.1:27017         ESTABLISHED
tcp        0      0 127.0.0.1:27017         127.0.0.1:51106         ESTABLISHED
tcp        0      0 127.0.0.1:51107         127.0.0.1:27017         ESTABLISHED
tcp        0      0 10.240.241.116:3002     174.61.171.61:36583     TIME_WAIT  
tcp        0      0 127.0.0.1:27017         127.0.0.1:51109         ESTABLISHED
tcp        0      0 10.240.241.116:42423    169.254.169.254:80      ESTABLISHED
tcp        0      0 127.0.0.1:51108         127.0.0.1:27017         ESTABLISHED
tcp        0    532 10.240.241.116:22       174.61.171.61:56824     ESTABLISHED
tcp        0      0 127.0.0.1:27017         127.0.0.1:51107         ESTABLISHED
tcp        0      0 10.240.241.116:42412    169.254.169.254:80      ESTABLISHED
tcp        0      0 127.0.0.1:51109         127.0.0.1:27017         ESTABLISHED
tcp        0      0 127.0.0.1:51105         127.0.0.1:27017         ESTABLISHED
tcp        0      0 10.240.241.116:42422    169.254.169.254:80      TIME_WAIT  
tcp        0      0 127.0.0.1:27017         127.0.0.1:51105         ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
udp        0      0 0.0.0.0:49948           0.0.0.0:*                          
udp        0      0 0.0.0.0:68              0.0.0.0:*                          
udp        0      0 10.240.241.116:123      0.0.0.0:*                          
udp        0      0 127.0.0.1:123           0.0.0.0:*                          
udp        0      0 0.0.0.0:123             0.0.0.0:*                          
udp6       0      0 :::12151                :::*                               
udp6       0      0 :::123                  :::*                               
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     405680   /tmp/ssh-KdkxJfFLpKTC/agent.22
813
unix  2      [ ACC ]     STREAM     LISTENING     408230   /tmp/ssh-ofUeNNEwAqtP/agent.22
243
unix  2      [ ACC ]     STREAM     LISTENING     416227   /tmp/mongodb-27017.sock
unix  2      [ ACC ]     SEQPACKET  LISTENING     3692     /run/udev/control
unix  7      [ ]         DGRAM                    5286     /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     5318     /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     16170    /tmp//tmux-1000/default
unix  2      [ ACC ]     STREAM     LISTENING     414450   /var/run/dbus/system_bus_socke

And here is the log when trying to run on port 80 with node.js:

[lucas@ecoinstance]~/node/nodetest1$ npm start

> [email protected] start /home/lucas/node/nodetest1
> node ./bin/www


events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EACCES
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1023:19)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Function.app.listen (/home/lucas/node/nodetest1/node_modules/express/lib/applicati
on.js:532:24)                                                                           
    at Object.<anonymous> (/home/lucas/node/nodetest1/bin/www:7:18)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 8
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is most likely a problem with the nodetest1 package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get their info via:
npm ERR!     npm owner ls nodetest1
npm ERR! There is likely additional logging output above.
npm ERR! System Linux 3.13-0.bpo.1-amd64
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! cwd /home/lucas/node/nodetest1
npm ERR! node -v v0.10.28
npm ERR! npm -v 1.4.9
npm ERR! code ELIFECYCLE
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /home/lucas/node/nodetest1/npm-debug.log
npm ERR! not ok code 0

And sudo netstat -lnp does not return any matching port 80's:

[lucas@ecoinstance]~/node/nodetest1$ sudo netstat -lnp                           [48/648]
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Progr
am name
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      29160/mon
god    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1976/sshd

tcp6       0      0 :::22                   :::*                    LISTEN      1976/sshd

udp        0      0 0.0.0.0:49948           0.0.0.0:*                           1604/dhcl
ient   
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1604/dhcl
ient   
udp        0      0 10.240.241.116:123      0.0.0.0:*                           2076/ntpd

udp        0      0 127.0.0.1:123           0.0.0.0:*                           2076/ntpd

udp        0      0 0.0.0.0:123             0.0.0.0:*                           2076/ntpd

udp6       0      0 :::12151                :::*                                1604/dhcl
ient   
udp6       0      0 :::123                  :::*                                2076/ntpd

Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     405680   22814/ssh-agent     /tmp/ssh-K
dkxJfFLpKTC/agent.22813
unix  2      [ ACC ]     STREAM     LISTENING     408230   24049/ssh-agent     /tmp/ssh-o
fUeNNEwAqtP/agent.22243
unix  2      [ ACC ]     STREAM     LISTENING     416227   29160/mongod        /tmp/mongo
db-27017.sock
unix  2      [ ACC ]     SEQPACKET  LISTENING     3692     284/udevd           /run/udev/
control
unix  2      [ ACC ]     STREAM     LISTENING     5318     1798/acpid          /var/run/a
cpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     16170    5177/tmux           /tmp//tmux
-1000/default
unix  2      [ ACC ]     STREAM     LISTENING     414450   28213/dbus-daemon   /var/run/d
bus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     404225   22324/1             /tmp/ssh-9
TlDmu4bjl/agent.22324

4 Answers 4

18

You are trying to start node as a non-root user. Linux by default only allows root to bind to ports 1024 or below.

If you want to run it as this user, you may want to look at https://stackoverflow.com/questions/413807/is-there-a-way-for-non-root-processes-to-bind-to-privileged-ports-1024-on-l

1
  • 1
    According to the bind manpage: EACCES The address is protected, and the user is not the superuser. And you see EACCES in your error report. Jun 6, 2014 at 7:32
3

The simplest, best answer imho:

  • sudo apt-get install libcap2-bin
  • sudo setcap cap_net_bind_service=+ep /usr/local/bin/node
  • Ta da! This allows your node app to run on port 80 without complaining.

Why do I like it? Because:

  • You don't have to use apache or nginx
  • You don't have to run your application as root
  • You won't have to forward ports (and handle that each time your machine boots)

Reference Link: https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps (A great article on how to set up your node app on cloud hosting).

4
  • what about platforms other than ubuntu?
    – Michael
    Feb 28, 2017 at 16:50
  • Should work on anything *nix, I believe Mar 1, 2017 at 20:43
  • 1
    How can we revert these changes if it don't work..??
    – Ritesh
    Mar 24, 2017 at 6:16
  • Thanks, it's helped a lot! Jul 23, 2020 at 11:36
2

Try netstat -lnp as root or lsof -n -i :80

1

I was trying to run server using node server.js. It didn't show up any error. Also,

lsof -t -i:80

not listed any processes.

The problem was I tried to bind as a non-privileged user. It got worked when I tried:

sudo node server.js

So simple, but, sometimes you may forgot it and you will start running behind other server processes when you have multiple servers like apache, nginx etc are running on your machine. (Just like I did). Hope this helps somebody!!!

You must log in to answer this question.

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