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


一、问题

网站绑定域名后直接通过域名访问使用的是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进行端口转发

  1. tomcat监听8080(其他非熟知端口皆可)端口,直接执行 sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 将对80端口的请求转发到8080端口。
  2. iptables规则设置后都是即时生效的,但在机器重启后iptables中的配置信息会被清空。因此可以将这些配置保存下来,让iptables在interface启动时自动被加载:

(1)保存防火墙规则: sudo iptables-save > /etc/zsmiptables.rules 

(2)编辑/etc/network/interfaces,在末尾加一行:pre-up iptables-restore < /etc/zsmiptables.rules

参考资料:

1、Linux配置本地Tomcat应用80端口转发   

iptables规则设置后都是即时生效的,在机器重启后,iptables中的配置信息会被清空.
您可以将这些配置保存下来,让iptables在启动时自动加载,省得每次都得重新输入.
iptables-save和iptables-restore就是用来保存和恢复设置的.
先将防火墙规则保存到/etc/iptables.up.rules文件中:
iptables-save > /etc/iptables.up.rules
然后修改脚本/etc/network/interfaces,在末尾添加一行,在网络启动时应用防火墙规则:
pre-up iptables-restore < /etc/iptables.up.rules

Ubuntu使用iptables配置防火墙简化总结:
sudo ufw disable && sudo ufw default allow 确保INPUT/FORWARD/OUTPUT几条链都是ACCEPT状态,否则网络访问中断,包括ssh.
sudo iptables -F && sudo iptables -X && sudo iptables -Z && sudo iptables -L
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -p tcp -i eth0 --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT
sudo iptables -A INPUT -j DROP
iptables-save > /etc/iptables.up.rules 切换到root用户执行,sudo会提示无权限
sudo nano /etc/network/interfaces 在末尾添加一行,在网络启动时应用防火墙规则:
pre-up iptables-restore < /etc/iptables.up.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做成了服务,能够开机自己启动。

更多Tomcat相关教程见以下内容

CentOS 6.6下安装配置Tomcat环境 

RedHat Linux 5.5安装JDK+Tomcat并部署Java项目   

Tomcat权威指南(第二版)(中英高清PDF版+带书签)   

Tomcat 安全配置与性能优化  

Linux下使用Xshell查看Tomcat实时日志中文乱码解决方案  

CentOS 64-bit下安装JDK和Tomcat并设置Tomcat开机启动操作步骤  

CentOS 6.5下安装Tomcat   

Tomcat 的详细介绍:请点这里
Tomcat 的下载地址:请点这里

本文永久更新链接地址

相关内容

    暂无相关文章