chcon 改变上下文里的上下文怎么写?

小灰狼 2014-10-31 08:58:06
去年曾经用一条命令改变过一个目录的上下文,当时是为了给 mysql 分配一个存储数据的目录:
chcon -R -t mysqld_db_t /home/myusqldata

这条命令别的东西都好理解,就是里面的 mysqld_db_t 是怎么回事啊,为什么要写成这样呢,有什么规则吗
baidu了很多次,引用的都是同一篇文章,没讲到我想知道的东西
...全文
241 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灰狼 2014-11-06
  • 打赏
  • 举报
回复
感谢楼上各位的帮助 但是SELinux 的水好象有点深,等忙过这一阵子再去研究,目前暂时把 selinux 禁用了
  • 打赏
  • 举报
回复
I don't know why you can't get any good reference for this context type: mysqld_db_t This is on RHEL website that exactly addressed the issue you have: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Managing_Confined_Services/sect-Managing_Confined_Services-MySQL-Configuration_Examples.html
Cody2k3 2014-11-05
  • 打赏
  • 举报
回复
引用 4 楼 hemowolf 的回复:
[quote=引用 3 楼 Cody2k3 的回复:] selinux里面的东西啊 写成这样说明你的selinux policy里面有对这个type设定rule,比如只有你的mysql process可以读取这些文件,之类,如果设定不正确,可能mysql不能正常工作 楼主可以把selinux的policy dump出来看看 另外这篇文章可以作为一个参考的起点 http://phe1129.wordpress.com/2012/04/02/change-mysql-data-folder-on-selinux/ 详细还需要自己百度下
感谢回复 首先说几句废话,我是做开发的,但是系统要在linux上运行,安装mysql,部署java web,但是公司里没有专门的linux管理员,所以只能我自己上。之所以遇到上面的问题,是因为我想在 mysql 创建表的时候指定数据文件的目录,但是mysql一直报错,说没有对那个目录的访问权限,而我已经把那个目录的所有者和所在组都给了 mysql,权限为777,并且指定了安全上下文 chcon -R -t mysqld_db_t /home/myusqldata。但是,还是说没有权限。 不晓得如何把 selinux 的 policy dump 出来,您提供的链接地址我这里打不开[/quote] 理解,很多公司都是这样,有弊有利吧 所以楼主的意思是说即使使用chcon也不能解决权限问题?那么可能问题就需要进一步诊断了 我把那篇英文文章拷贝在下面, 供楼主参考 另外,关于selinux,redhat的这篇tutorial写的挺好 https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/SELinux_Guide/index.html 简单的说楼主可以试几个命令 1. sestatus 看是否selinux enable,如果disable,那么楼主可以不用考虑任何和chcon相关的东西了 2. sesearch -a -t mysqld_db_t /etc/selinux/..../...conf (具体位置取决于系统配置) 比如这是一个sample output Found 99 av rules: allow mysqld_t mysqld_db_t: file {ioctl read write create getattr setattr lock append unlink link rename} allow mysqld mysqld_db_t: dir {ioctl read write create getattr setattr lock unlink link rename } ... 上面的rule是 allow src_type target_type class {permission} mysqld 这个executable的src type是mysqld, 所以把某些dir设成target type=mysqldb_db_t 就可以match这些rule从而拥有合适的permission 3. selinux rule导致的失败通常可以在 /var/log/下找到相关记录, 楼主可以试下 以下是引用文章 Change MySQL data folder on SELinux Posted on April 2, 2012 by phe1129 When installing MySQL on SELinux (Security Enhanced Linux), you may get the following errors in mysqld.log if you changed the default data directory for mysql database even you granted all necessary privileges to the user mysql: 111206 1:46:00 [Warning] Can’t create test file /data/mysql/devmysql.lower-test 111206 1:46:00 [Warning] Can’t create test file /data/mysql/devmysql.lower-test /usr/libexec/mysqld: Can’t change dir to ‘/data/mysql/’ (Errcode: 13) 111206 1:46:00 [ERROR] Aborting This is because on SELinux such as CentOS 6, all processes and files are labeled in a way that represents security-relevant information. This information is called the SELinux context. For files, this is viewed using the ls -Z command: $ ls -Z file1 -rw-rw-r– user1 group1 unconfined_u:object_r:user_home_t:s0 file1 In this example, SELinux provides a user (unconfined_u), a role (object_r), a type (user_home_t), and a level (s0). This information is used to make access control decisions. On DAC systems, access is controlled based on Linux user and group IDs. SELinux policy rules are checked after DAC rules. SELinux policy rules are not used if DAC rules deny access first. So when we change the mysql datadir to a different folder, besides granting the access permission for the mysql user, we also need to change the lable of the new folder. First we need to know what it is the correct labeling using -Z command on the default data dir: [root@xxx ~]# ls -lh -Zd /var/lib/mysql drwxr-xr-x. mysql mysql system_u:object_r:mysqld_db_t:s0 /var/lib/mysql Now change the label for the new location: chcon -R -u system_u -r object_r -t mysqld_db_t /data/mysql To verify the lable is changed: ls -lh -Zd /data/mysql drwxr-xr-x. mysql mysql system_u:object_r:mysqld_db_t:s0 /data/mysql Note the mysql config file has a different lable, do not use that one for the data dir. [root@xxx ~]# ls -lh -Z /etc/my.cnf -rw-r–r–. root root system_u:object_r:mysqld_etc_t:s0 /etc/my.cnf If binlog file is put in a different location (e.g. /mysql-log/mysql), we also need to change the label for the root folder (/mysql-log) and the subfolder /mysql-log/mysql. Now we can install the system tables: mysql_install_db –datadir=/data/mysql –user=mysql
小灰狼 2014-11-04
  • 打赏
  • 举报
回复
引用 3 楼 Cody2k3 的回复:
selinux里面的东西啊 写成这样说明你的selinux policy里面有对这个type设定rule,比如只有你的mysql process可以读取这些文件,之类,如果设定不正确,可能mysql不能正常工作 楼主可以把selinux的policy dump出来看看 另外这篇文章可以作为一个参考的起点 http://phe1129.wordpress.com/2012/04/02/change-mysql-data-folder-on-selinux/ 详细还需要自己百度下
感谢回复 首先说几句废话,我是做开发的,但是系统要在linux上运行,安装mysql,部署java web,但是公司里没有专门的linux管理员,所以只能我自己上。之所以遇到上面的问题,是因为我想在 mysql 创建表的时候指定数据文件的目录,但是mysql一直报错,说没有对那个目录的访问权限,而我已经把那个目录的所有者和所在组都给了 mysql,权限为777,并且指定了安全上下文 chcon -R -t mysqld_db_t /home/myusqldata。但是,还是说没有权限。 不晓得如何把 selinux 的 policy dump 出来,您提供的链接地址我这里打不开
Cody2k3 2014-11-03
  • 打赏
  • 举报
回复
selinux里面的东西啊 写成这样说明你的selinux policy里面有对这个type设定rule,比如只有你的mysql process可以读取这些文件,之类,如果设定不正确,可能mysql不能正常工作 楼主可以把selinux的policy dump出来看看 另外这篇文章可以作为一个参考的起点 http://phe1129.wordpress.com/2012/04/02/change-mysql-data-folder-on-selinux/ 详细还需要自己百度下
小灰狼 2014-11-03
  • 打赏
  • 举报
回复
怎么还没有人??
小灰狼 2014-10-31
  • 打赏
  • 举报
回复
消灭0回复帖

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧