在 Debian 上配置 Tomcat 用户(用于管理界面、部署等)主要是通过编辑 tomcat-users.xml 文件完成的。下面以 Tomcat 9 / 10 为例说明(其他版本类似)。
sudo apt update
sudo apt install tomcat9 tomcat9-admin
tomcat9-admin包含管理界面所需的 Web 应用
Tomcat 的用户和角色定义在:
/etc/tomcat9/tomcat-users.xml
使用编辑器打开:
sudo nano /etc/tomcat9/tomcat-users.xml
在 <tomcat-users> 标签内添加用户,例如:
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="strongpassword" roles="manager-gui,admin-gui"/>
<user username="deployer" password="deploy123" roles="manager-script"/>
</tomcat-users>
| 角色 | 说明 |
|---|---|
manager-gui |
使用 Web 管理界面 |
admin-gui |
访问 Server Status / Host Manager |
manager-script |
使用脚本部署(如 Maven) |
manager-jmx |
JMX 代理 |
manager-status |
查看服务器状态 |
默认情况下,Tomcat 禁止远程访问 Manager。
编辑:
sudo nano /usr/share/tomcat9-admin/manager/META-INF/context.xml
注释或删除以下内容:
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
✅ 或改为允许所有 IP(不推荐生产环境):
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow=".*" />
同样处理 host-manager:
sudo nano /usr/share/tomcat9-admin/host-manager/META-INF/context.xml
sudo systemctl restart tomcat9
浏览器访问:
http://服务器IP:8080/manager/html
使用你配置的用户登录,例如:
adminstrongpassword✅ 使用强密码
✅ 不要开放 manager-gui 给公网
✅ 使用防火墙限制访问端口
✅ 生产环境建议禁用管理界面
如果你用的是 Tomcat 10(Jakarta EE) 或 Docker / systemd 自定义安装,可以告诉我,我可以给你对应配置方式。