红联Linux门户
Linux帮助

非root用户无法启动监听80端口的Tomcat

发布时间:2016-07-11 15:08:53来源:cnblogs.com/z-sm/作者:March On
一、问题
网站绑定域名后直接通过域名访问使用的是80端口,因此tomcat须监听80端口,而为了安全起见tomcat一般不用root身份运行,综上,需要以普通用户来运行监听80端口的tomcat。此时就会启动失败,报没有权限,因为只有root身份才能监听1024以下的熟知端口。
 
二、解决
(以下未经验证)
There are a few different solutions to work around this:
1.Install and configure Apache or nginx as a reverse proxy server, which can be started as root to open the port, and then downgrade its privileges back to a normal user.
2.Set up a firewall on the server using iptables or an alternative, so that the lower port number is forwarded internally to a higher port number listened by Confluence.
3.Use jsvc, which is able to open ports as root, and then downgrade privileges.
4.Use authbind to grant privileges for a non-root user to open a privileged port.
 
1、通过iptables进行端口转发
一,tomcat监听8080(其他非熟知端口皆可)端口,直接执行 sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 将对80端口的请求转发到8080端口。
二,iptables规则设置后都是即时生效的,但在机器重启后iptables中的配置信息会被清空。因此可以将这些配置保存下来,让iptables在interface启动时自动被加载:
(1)保存防火墙规则: sudo iptables-save > /etc/zsmiptables.rules 
(2)编辑/etc/network/interfaces,在末尾加一行:pre-up iptables-restore < /etc/zsmiptables.rules
(前者言将iptables-restore < /etc/zsmiptables.rules放到一脚本里置于/etc/network/if-pre-up.d/下,但一直不成功;改用后者所言将iptables-restore < /etc/zsmiptables.rules加到/etc/network/interfaces末尾成功了)
 
2、通过isvc
jsvc能以root角色使用端口,因此借助之即可。另外,这种方式也把tomcat做成了服务,能够开机自己启动。
 
本文永久更新地址:http://www.linuxdiyf.com/linux/22280.html