关于tomcat 中的 tomcat-users.xml 配置不生效原因

本文介绍了解决Tomcat Manager App登录问题的方法,包括确认server.xml文件配置、解除tomcat-users.xml文件中用户配置的注释、避免用户名重复及遵循Tomcat 7.X版本特定的用户配置规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 安装玩tomcat,或者解压玩tomcat后,在tomcat的目录下有个conf文件夹,在这个文件夹下面有一个tomcat-
users.xml的文件,这个文件里面的配置信息是当我们进入http://localhost:8080 页面后,有些功能需要登录才能

进行访问。今天我解压好了一个tomcat,环境变量也配置好了,启动服务也没问题,就是登录不进去Manager App,大

概花了我一个下午的时间才解决这个问题。
  首先tomcat-users.xml这个文件就是tomcat提供给我们进行登录用户的配置,但是一开始我配置了很多次也不成功
,在网上也查了很多方法没用,接下来我将说明一下如何配置这个用户。
  1、首先你确定在conf目录下的server.xml文件有
  <GlobalNamingResources>
    <!– Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    –>
    <Resource auth=”Container” description=”User database that can be updated and saved” 
factory=”org.apache.catalina.users.MemoryUserDatabaseFactory” name=”UserDatabase” 
pathname=”conf/tomcat-users.xml” type=”org.apache.catalina.UserDatabase”/>
  </GlobalNamingResources>
这样一段代码,让tomcat启动服务后能加载到conf/tomcat-users.xml,这个文件。
  2、新解压的tomcat下的tomcat-users.xml,中的user全部都是被注释掉的
<tomcat-users>
<!–
  NOTE:  By default, no user is included in the “manager-gui” role required
  to operate the “/manager/html” web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
–>
<!–
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
–>
<!–
  <role rolename=”tomcat”/>
  <role rolename=”role1”/>
  <user username=”tomcat” password=”<must-be-changed>” roles=”tomcat”/>
  <user username=”both” password=”<must-be-changed>” roles=”tomcat,role1”/>
  <user username=”role1” password=”<must-be-changed>” roles=”role1”/>
–>
</tomcat-users>
我们需要把那个注释去掉才能是的user生效,有很多人在这个地方犯错,注释不去掉,那都是没用的。
  3、注意在配置username的时候,不要取重复的名字,因为这样很可能让你的配置不起效果。
  4、使用tomcat7.X版本的朋友们注意了,这也是今天下午被坑的原因,tomcat7.X的user配置是有一定的规定的,


不能随意更改,你改成其他的很有可能不会生效,本人亲测,原来各种网上百度,找了很多种的配置方法就是没效果,


结果我将user配置改成如下结果终于登录进去了。
<?xml version=’1.0’ encoding=’utf-8’?>
<!–
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the “License”); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at


      http://www.apache.org/licenses/LICENSE-2.0


  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an “AS IS” BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
–>
<tomcat-users>
<!–
  NOTE:  By default, no user is included in the “manager-gui” role required
  to operate the “/manager/html” web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
–>
<!–
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
–>


<role rolename=”tomcat”/>
<role rolename=”role1”/>
<role rolename=”manager-gui”/>
<role rolename=”admin-gui”/>
<user username=”tomcat” password=”tomcat” roles=”tomcat,manager-gui,admin-gui”/>
<user username=”both” password=”tomcat” roles=”tomcat,role1”/>
<user username=”role1” password=”tomcat” roles=”role1”/>


</tomcat-users>
  5、最后需要注意的是,修改了配置文件需要重新启动服务器,好让服务器重新加载修改后的配置文件。
亲测,用这里面的tomcat-user.xml内容,可以登录成功!
<think>好的,用户遇到了在修Tomcat配置文件时出现的权限问题。他运行了sudo vi命令,但仍然无法保存文件,提示“E212: Can't open file for writing”。我需要分析可能的原因,并提供解决方案。 首先,用户使用了sudo来执行vi,这应该以root权限运行,理论上可以写入文件。但错误提示说明文件无法打开写入。可能的原因有几个: 1. **文件权限问题**:虽然用户用sudo,但目标文件或目录的权限可能不允许root用户写入。例如,/opt/tomcat/conf目录或tomcat-users.xml文件的权限设置可能过于严格,比如目录没有写权限,或者文件被设置为只读。 2. **文件不存在**:用户指定的路径可能不正确,导致vi尝试创建新文件,但父目录没有写权限。例如,如果/opt/tomcat/conf目录不存在,或者路径拼写错误,vi无法创建文件。 3. **SELinux或AppArmor限制**:在某些系统上,安全模块如SELinux可能会阻止即使有root权限的进程写入特定文件或目录。 4. **文件系统挂载为只读**:如果所在的文件系统被挂载为只读模式,即使有权限也无法写入。 接下来,我需要分步骤引导用户检查这些问题。首先确认文件路径是否正确,然后检查文件和目录的权限。如果权限没问题,再考虑安全模块或文件系统的问题。另外,用户可能需要使用sudoedit或者检查sudoers配置是否限制了某些操作。同时,提供替代方法,比如使用其他编辑器或者修权限后再编辑,确保用户有多种解决途径。</think>--- ### 问题分析:`E212: Can't open file for writing` 错误原因 当你使用 `sudo vi` 编辑文件时出现此错误,表明 **权限不足或文件路径异常**。即使使用 `sudo`,仍需满足以下条件: 1. **父目录可写**:文件所在目录(如 `/opt/tomcat/conf`)必须允许 `root` 用户写入。 2. **文件权限可覆盖**:文件本身权限允许修(例如不被设置为 `chattr +i` 的不可修状态)。 3. **路径正确性**:文件路径必须存在且无拼写错误。 --- ### 分步解决方案 #### 1. **检查路径和文件是否存在** ```bash # 确认文件路径正确性 ls -l /opt/tomcat/conf/tomcat-users.xml ``` - 若文件不存在,需先创建父目录并设置权限: ```bash sudo mkdir -p /opt/tomcat/conf sudo chown -R root:root /opt/tomcat ``` #### 2. **验证父目录的写权限** ```bash # 查看目录权限(重点检查 conf 目录) ls -ld /opt/tomcat /opt/tomcat/conf ``` - **期望输出**:目录权限应包含 `rwx`(如 `drwxr-xr-x`)。 - **修复权限**(如果需要): ```bash sudo chmod 755 /opt/tomcat/conf # 允许 root 写入 ``` #### 3. **检查文件本身的权限和属性** ```bash # 查看文件权限和属性 ls -l /opt/tomcat/conf/tomcat-users.xml sudo lsattr /opt/tomcat/conf/tomcat-users.xml ``` - **权限修复**: ```bash sudo chmod 644 /opt/tomcat/conf/tomcat-users.xml # 允许 root 读写 sudo chattr -i /opt/tomcat/conf/tomcat-users.xml # 解除不可修标记(如果存在) ``` #### 4. **使用 `sudo -i` 提权到完整 root 环境** ```bash sudo -i # 进入 root 会话 vi /opt/tomcat/conf/tomcat-users.xml exit # 退出 root ``` #### 5. **用 `sudo tee` 写入(适用于 Vim 失效时)** ```bash echo ' <role rolename="manager-gui"/> <user username="admin" password="secret" roles="manager-gui"/> ' | sudo tee -a /opt/tomcat/conf/tomcat-users.xml ``` --- ### 特殊情况处理 - **SELinux/AppArmor 拦截**: 临时禁用 SELinux 测试(生产环境慎用): ```bash sudo setenforce 0 # 临时关闭 sudo vi /opt/tomcat/conf/tomcat-users.xml sudo setenforce 1 # 重新启用 ``` - **文件系统只读挂载**: 检查挂载状态并重新挂载为读写: ```bash mount | grep /opt/tomcat sudo mount -o remount,rw /opt/tomcat ``` --- ### 验证配置生效 ```bash # 查看文件修时间和权限 ls -l /opt/tomcat/conf/tomcat-users.xml # 重启 Tomcat 并测试登录 sudo systemctl restart tomcat curl -u admin:secret http://localhost:8080/manager/html ``` --- ### 总结 此错误的核心是 **权限链断裂**。即使使用 `sudo`,仍需确保: 1. 目录树权限允许 `root` 写入 2. 文件未被锁定或设置为不可修 3. 路径真实存在 通过分步检查权限、路径和系统安全策略,可快速定位并解决问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值