阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

130次阅读
没有评论

共计 2768 个字符,预计需要花费 7 分钟才能阅读完成。

一、问题

网站绑定域名后直接通过域名访问使用的是 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 端口转发  http://www.linuxidc.com/Linux/2016-07/133078.htm 

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 环境  http://www.linuxidc.com/Linux/2015-08/122234.htm

RedHat Linux 5.5 安装 JDK+Tomcat 并部署 Java 项目  http://www.linuxidc.com/Linux/2015-02/113528.htm 

Tomcat 权威指南(第二版)(中英高清 PDF 版 + 带书签)  http://www.linuxidc.com/Linux/2015-02/113062.htm 

Tomcat 安全配置与性能优化 http://www.linuxidc.com/Linux/2015-02/113060.htm 

Linux 下使用 Xshell 查看 Tomcat 实时日志中文乱码解决方案 http://www.linuxidc.com/Linux/2015-01/112395.htm 

CentOS 64-bit 下安装 JDK 和 Tomcat 并设置 Tomcat 开机启动操作步骤 http://www.linuxidc.com/Linux/2015-01/111485.htm 

CentOS 6.5 下安装 Tomcat  http://www.linuxidc.com/Linux/2015-01/111415.htm 

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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133079.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计2768字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中