在CentOS中,管理文件系统权限主要涉及到以下几个方面:
# 查看当前用户
whoami
# 查看所有用户
cat /etc/passwd
# 查看所有组
cat /etc/group
# 添加用户
useradd username
# 添加组
groupadd groupname
# 将用户添加到组
usermod -aG groupname username
文件和目录的权限分为三类:所有者(owner)、组(group)和其他人(others)。每类权限又分为读(r)、写(w)和执行(x)。
ls -l /path/to/file_or_directory
输出示例:
-rwxr-xr-- 1 owner group 4096 Jan 1 12:34 file_or_directory
-rwxr-xr-- 表示权限:
- 表示普通文件,d 表示目录)。rwx)。r-x)。r--)。使用 chmod 命令修改权限。
chmod 755 /path/to/file_or_directory
755 表示:
rwx(读、写、执行)r-x(读、执行)r-x(读、执行)chmod u+x /path/to/file_or_directory # 给所有者添加执行权限
chmod g-w /path/to/file_or_directory # 从组中移除写权限
chmod o=r /path/to/file_or_directory # 将其他人的权限设置为只读
使用 chown 和 chgrp 命令修改文件和目录的所有者和组。
chown newowner /path/to/file_or_directory
chgrp newgroup /path/to/file_or_directory
除了基本的读、写和执行权限外,还有特殊权限:SetUID(suid)、SetGID(sgid)和Sticky Bit。
chmod u+s /path/to/executable_file
这会使执行该文件的用户获得文件所有者的权限。
chmod g+s /path/to/directory
这会使在该目录中创建的文件继承目录的组。
chmod +t /path/to/directory
这会防止用户删除或重命名目录中的其他用户的文件。
umask 命令用于设置默认的文件创建权限掩码。
umask 022
这会使新创建的文件默认权限为 644(rw-r--r--),目录默认权限为 755(rwxr-xr-x)。
通过以上方法,你可以有效地管理CentOS系统中的文件系统权限,确保系统的安全性和数据的完整性。