linux文件属性share,文件的SELinux属性

本文介绍如何使用SELinux的安全上下文管理命令临时和永久修改文件及目录的类型属性,包括使用chcon进行临时修改,以及使用semanage fcontext和restorecon进行永久修改。

文件的SELinux属性

1.临时修改文件的类型属性

文件的类型属性不正确是常见的SELinux拒绝访问的主要原因

1)修改文件的SELinux属性:

[root@localhost ~]# touch test.file   ##新建文件

[root@localhost ~]# ls -Z test.file   ##查看文件的SELinux属性

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 test.file

[root@localhost ~]# chcon -t samba_share_t test.file  ##修改文件的默认SELinux属性

[root@localhost ~]# ls -Z test.file

-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 test.file

[root@localhost ~]# restorecon -F -v test.file   ##恢复修改过的SELinux属性为默认属性

restorecon reset /root/test.file context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:admin_home_t:s0

[root@localhost ~]# ls -Z test.file

-rw-r--r--. root root system_u:object_r:admin_home_t:s0 test.file

2)修改目录的SELinux属性(所有的操作与文件对比多了一个“-R”代表递归):

[root@localhost ~]# mkdir /web

[root@localhost ~]# touch /web/file{1,2}

[root@localhost ~]# ls -dZ /web  ##查看目录的SELinux属性

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /web

[root@localhost ~]# ls -lZ /web/   ##查看目录下文件的SELinux属性

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file2

[root@localhost ~]# chcon -R -t httpd_sys_content_t /web/    ##临时修改目录的SELinux属性为httpd_sys_content_t

[root@localhost ~]# ls -dZ /web/

drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/

[root@localhost ~]# ls -lZ /web/

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file2

[root@localhost ~]# restorecon -R -v /web/   ##恢复为默认SELinux属性

restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:default_t:s0

restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:default_t:s0

restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:default_t:s0

[root@localhost ~]# ls -lZ /web/

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file2

[root@localhost ~]# ls -dZ /web/

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /web/

2.永久修改文件的类型属性

永久修改文件及目录的类型属性使用下列命令:

semanage fcontext -{a|d|l|m} [-frst] filespec  ##-a增加、-d删除、-l显示、-m修改

restorecon -v filespec    ##由于“semanage fcontext”命令只是将属性定义项加载到

“/etc/selinux/targeted/contexts/files/file_contexts.local”文件中,

使用该命令才是将文件的selinux属性永久地修改.

1)文件的selinux属性修改

[root@localhost tmp]# touch test.file

[root@localhost tmp]# ls -Z test.file

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.file

[root@localhost tmp]# yum install policycoreutils-python  ##安装semanage管理工具提供软件

[root@localhost tmp]# semanage fcontext -a -t samba_share_t /tmp/test.file  ##将测试文件的selinux属性设置为“samba_share_t”

[root@localhost tmp]# ls -Z /tmp/test.file   ##测试文件的selinux属性未发生变化

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/test.file

[root@localhost tmp]# restorecon -v /tmp/test.file    ##使semanage设置的selinux属性永久的生效

restorecon reset /tmp/test.file context unconfined_u:object_r:user_tmp_t:s0->unconfined_u:object_r:samba_share_t:s0

[root@localhost tmp]# ls -Z /tmp/test.file   ##测试文件的selinux属性已改

-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 /tmp/test.file

2)目录的selinux属性修改:(完成后目录下新建的文件自动继承selinux属性)

[root@localhost ~]# mkdir /html

[root@localhost ~]# touch /html/file{1,2}

[root@localhost ~]# ls -dZ /html

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /html

[root@localhost ~]# ls -lZ /html/

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file2

[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t "/html(/.*)?"  ##正则表达式“/html(/.*)?”表示/html目录及其中的任何文件或子目录

[root@localhost ~]# restorecon -R -v /html/  ##使semanage设置的selinux属性永久的生效

restorecon reset /html context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

restorecon reset /html/file2 context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

restorecon reset /html/file1 context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

[root@localhost ~]# ls -lZ /html/   ##验证修改结果

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file2

[root@localhost ~]# ls -dZ /html/

drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /html/

3)如果要恢复/html的文件属性可使用下列命令:

[root@localhost ~]# semanage fcontext -d "/html(/.*)?"   ##删除自定义的selinux属性

[root@localhost ~]# restorecon -F -R -v /html/  ##永久生效

restorecon reset /html context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0

restorecon reset /html/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0

restorecon reset /html/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0

[root@localhost ~]# ls -lZ /html  ##验证文件的selinux属性已经从“httpd_sys_content_t”改为默认的"default_t"

-rw-r--r--. root root system_u:object_r:default_t:s0   file1

-rw-r--r--. root root system_u:object_r:default_t:s0   file2

[root@localhost ~]# ls -dZ /html

drwxr-xr-x. root root system_u:object_r:default_t:s0   /html

4)file_t与default_t

file_t:文件没有selinux属性

default_t:文件或目录的selinux属性与file-context配置文件定义的模式不匹配

两种的类型的文件或目录,受限制的域的程序均不能访问

### 如何使用 cwRsync 在 Linux 系统中同步文件 cwRsync 是一种可以在 Windows 平台上使用的工具,它基于 Cygwin 提供了 rsync 功能。虽然 cwRsync 主要用于 Windows 环境下的文件同步,但在某些情况下也可以通过跨平台的方式与 Linux 系统交互完成文件同步。 #### 安装和配置 cwRsync 在 Windows 上安装 cwRsync 后,可以通过命令行调用 `rsync` 工具来实现与远程 Linux 服务器之间的文件同步功能。以下是具体的操作方法: 1. **基本语法** 使用 cwRsync 的核心在于掌握其基础命令结构: ```bash rsync.exe -avz --progress source destination ``` 参数解释如下: - `-a`: 表示归档模式,保持文件权限、时间戳等属性不变[^3]。 - `-v`: 输出详细的执行过程信息。 - `-z`: 对数据流进行压缩处理后再传输,减少带宽占用。 - `--progress`: 显示每个文件的实时传输进度。 2. **从本地向远端同步** 如果需要将本地目录中的内容上传至 Linux 服务器,则可采用以下形式: ```bash rsync.exe -avz C:\local\src\ user@remote_host:/path/to/destination/ ``` 3. **从远端下载到本地** 若要把位于 Linux 服务上的某个路径复制回本机硬盘里,则应这样写指令: ```bash rsync.exe -avz user@remote_host:/path/to/source/ D:\target\ ``` 4. **删除多余项选项** 添加 `--delete` 可让目标位置自动移除那些源地址已不存在的数据条目: ```bash rsync.exe -avz --delete /source/directory/ remote_user@host:/dest/dir/ ``` 5. **排除特定类型的文件** 当不想同步某类特殊格式或者大体积媒体资料时,利用 `--exclude` 条件非常有用: ```bash rsync.exe -avz --exclude='*.mp4' src_folder/ dest_folder/ ``` 6. **设置密码文件** 为了避免每次都需要手动输入 SSH 密码,在批量化作业场景下显得尤为不便;因此创建一个纯文本格式的小型档案存储登录凭证,并指定给程序读取即可简化流程: 创建名为 `password.txt` 文件保存密码后赋予适当权限防止泄露风险; 修改上述任一条语句加入新参数指向该密钥所在处所: ```bash rsync.exe -avz --password-file=C:\secrets\password.txt module_name::share_path target_directory/ ``` #### 注意事项 - 确认防火墙规则允许必要的端口通信 (默认为873)[^3]。 - SELinux 设置可能干扰正常运作需调整策略或暂时关闭测试效果[^3]。 - 日常维护期间记得查阅错误日志排查隐患问题。 ```python import subprocess def run_rsync(source, destination): command = f"rsync.exe -avz {source} {destination}" result = subprocess.run(command, shell=True, capture_output=True, text=True) if result.returncode != 0: raise Exception(f"Error occurred: {result.stderr}") return result.stdout ``` 以上脚本展示了如何借助 Python 调用系统级命令完成自动化任务管理。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值