Linux 用户与权限

2020-10-11  本文已影响0人  _于曼丽_

切换用户

# 切换用户为 user1 用户
su - user1
# 切换用户为 root 用户
su -
# 以 root 身份执行一条 command 命令
sudo command
# 退出当前用户
exit

添加删除用户

以下命令需要以 root 身份执行

# 添加用户,同时添加家目录
useradd user1
# 删除用户,不删除家目录
userdel user1
# 删除用户,同时删除家目录
userdel -r user1
# 修改用户密码
passwd user1

与用户有关的文件:

添加删除群组

以下命令需要以 root 身份执行

# 添加群组
groupadd grp1
# 删除群组
groupdel grp1

用户与群组

用户:useradd 命令创建一个用户
群组:groupadd 命令创建一个群组
所属组:每个用户有且只有一个所属组

usermod -g 修改用户的所属组
usermod -G 修改用户加入的群组
usermod -aG 追加用户加入的群组
gpasswd -d 将用户从群组中移除

以下命令需要以 root 身份执行

# 查看用户的组信息
id user
# 修改用户的所属组
usermod -g grp1 user1
# 修改用户加入的群组(覆盖原来的群组),不会修改所属组。多个组以逗号分割,不要加空格
usermod -G grp2,grp3 user1
# 将用户加入到群组(不覆盖原来的群组),不会修改所属组
usermod -aG grp2,grp3 user1
# 将用户从群组中移除,不会修改所属组
gpasswd -d user1 grp2

查看 yumanli 的用户信息

[root@punk ~]# id yumanli
uid=1000(yumanli) gid=1000(yumanli) groups=1000(yumanli)

修改 yumanli 的所属组为 gcd

[root@punk ~]# usermod -g gcd yumanli
[root@punk ~]# id yumanli
uid=1000(yumanli) gid=1004(gcd) groups=1004(gcd)

将 yumanli 加入 gcd 群组

[root@punk ~]# usermod -G gcd yumanli
[root@punk ~]# id yumanli
uid=1000(yumanli) gid=1004(gcd) groups=1004(gcd)

将 yumanli 加入 jt 群组

[root@punk ~]# usermod -G jt yumanli
[root@punk ~]# id yumanli
uid=1000(yumanli) gid=1004(gcd) groups=1004(gcd),1002(jt)

将 yumanli 加入 gcd,zt 群组

[root@punk ~]# usermod -aG gcd,zt yumanli
[root@punk ~]# id yumanli
uid=1000(yumanli) gid=1004(gcd) groups=1004(gcd),1002(jt),1003(zt)

将 yumanli 从群组 jt 和 zt 中移除

[root@punk ~]# gpasswd -d yumanli jt
Removing user yumanli from group jt
[root@punk ~]# gpasswd -d yumanli zt
Removing user yumanli from group zt
[root@punk ~]# id yumanli
uid=1000(yumanli) gid=1004(gcd) groups=1004(gcd)

将 yumanli 从群组 gcd 中移除

[root@punk ~]# gpasswd -d yumanli gcd
Removing user yumanli from group gcd
[root@punk ~]# id yumanli
uid=1000(yumanli) gid=1004(gcd) groups=1004(gcd)

文件权限

不管怎么设置,root 用户永远拥有对所有文件的 rwx 权限
Linux 中目录也算是特殊的文件

文件的权限:

目录的权限:

如果想要删除一个文件或者一个目录,则不是看该文件或目录本身是否具有 w 属性,而是看所在父目录是否有 w 属性。只要父目录有 w 属性,不管文件是否 w,都可以删除。只要父目录有 w 属性,子目录如果是空目录,则不管是否 w,都可以删除。子目录如果不是空目录,则删除子目录中的文件的时候,需要确定子目录是否具有 w 属性。

以下命令需要以 root 身份执行

# 修改文件的所有者
chown user1 file 
# 修改文件的所有者和所属组
chown user1:grp1 file
# 递归修改目录以及子目录中所有目录和文件的所有者和所属组
chown -R user1:grp1 web

以下命令不需要以 root 身份执行,只要是此文件的所有者就可以执行

# 修改文件的权限
chmod 644 file
# 修改目录的权限
chmod 755 web
# 递归修改目录以及子目录中所有目录和文件的的权限,root 用户不受限制
chmod -R 700 /home/oscar
# 以字母方式修改文件的权限
chmod u+x file
chmod g-wx file
chmod o=rwx file
chmod +x file

setfacl

chmod 命令可以把文件权限分为u,g,o三个组,setfacl 命令可以把文件权限细分到具体用户和具体组。

chmod 命令和 setfacl 命令设置的所属组的权限可以相互覆盖,当二者设置的权限不一致时,以使用 getfacl 命令看到的 “#effective:” 后的权限为准。

getfacl test 查看某个文件/目录的 acl 权限
setfacl -m u:user1:rwx test 添加 acl 权限
setfacl -m g:grp1:rw test
setfacl -x u:user1 test 删除 acl 权限
setfacl -x g:grp1 test
setfacl -b test 删除某个文件/目录的所有 acl 权限
setfacl -R 递归
setfacl -m d:u:sixijie:rw,d:g:hadoop:r dir1 设置默认的 acl 权限,一般只针对目录
setfacl -k dir1 删除默认的 acl 权限

getfacl 查看某个文件/目录的 acl 权限

[root@punk mnt]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
group::r-x
other::r-x

setfacl -m 设置某个文件/目录的 acl 权限

[root@punk mnt]# setfacl -m u:yumanli:r-- test
[root@punk mnt]# setfacl -m u:lvhanzhi:rw- test
[root@punk mnt]# setfacl -m u:songyi:rwx test
[root@punk mnt]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:yumanli:r--
user:lvhanzhi:rw-
user:songyi:rwx
group::r-x
mask::rwx
other::r-x

setfacl -R -m 递归设置某个目录的子目录和子文件的 acl 权限

[root@punk mnt]# setfacl -R -m u:songyi:rwx test
[root@punk mnt]# getfacl test/foo/hello.php 
# file: test/foo/hello.php
# owner: root
# group: root
user::rw-
user:songyi:rwx
group::r--
mask::rwx
other::r--

以上设置,在 test 目录以及子目录中新创建的文件或目录不会继承 test 目录的 acl 权限,如果想要新创建的文件或目录继承 test 目录的权限,需要设置 test 目录的默认权限:

[root@punk mnt]# setfacl -m d:u:yumanli:rwx test
[root@punk mnt]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:yumanli:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@punk mnt]# cd test
[root@punk test]# mkdir foo
[root@punk test]# getfacl foo
# file: foo
# owner: root
# group: root
user::rwx
user:yumanli:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:yumanli:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

[root@punk test]# touch file
[root@punk test]# ls
file  foo
[root@punk test]# getfacl file
# file: file
# owner: root
# group: root
user::rw-
user:yumanli:rwx        #effective:rw-
group::r-x          #effective:r--
mask::rw-
other::r--

setfacl -x 删除某个用户的 acl 权限

# 删除 test 目录的针对 songyi 的 acl 权限
setfacl -x u:songyi test

setfacl -b 删除所有 acl 权限

# 删除 test 目录的所有 acl 权限
setfacl -b test
# 递归删除 test 的所有子目录以及子文件的 acl 权限
setfacl -b -R test

setfacl -k 删除某个目录的默认权限

# 删除 test 目录的默认权限
setfacl -k test

常用

设置某个目录的 acl 权限:

setfacl -R -m u:apache:rwx www
setfacl -m d:u:apache:rwx www

删除某个目录的 acl 权限:

setfacl -R -b www
上一篇 下一篇

猜你喜欢

热点阅读