How to start a program using port 80 as a non-root user in Linux


By default, only the root user has permission to occupy the ports below 1024 in Linux. If our tomcat, apache, nginx and other programs want to use ordinary users to occupy port 80, it will throw java.net.BindException: Permission denied :80 exception. 
 There are two solutions:
 
1. Use a non-80 port to start the program, and then use iptables to do a port forwarding.
 
  iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
 
  can be executed directly with the root user!
 
2. Assuming that the program we need to start is nginx, then this can also achieve the purpose.
 
At the beginning, we looked at the permission description of nginx:     
 
-rwxr-xr-x 1 nginx dev 2408122 Sep 5 16:01 nginx
 
must not be able to start normally at this time.
 
First modify the user of the file to root:
 
chown root nginx
 
and then add the s permission:
 
chmod u+s nginx
 
When viewing the permission description again:
 
-rwsr-xr-x 1 root root 2408122 Sep 5 16:01 nginx
 
can be started .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326487027&siteId=291194637