Linux用户和权限

Linux 的root用户

root用户(超级管理员)

无论是Windows、MacOS、Linux均采用多用户管理模式进行权限管理

  • 在Linux系统中,拥有最大权限的账户名称为:root(超级管理员)
  • 而在前期我们一直使用的账户是普通用户

root用户拥有最大的系统操作权限,而普通用户在许多地方的权限是受限的。

#演示:
btc@btc-VMware20-1:~$ mkdir /test
mkdir: 无法创建目录 "/test": 权限不够

btc@btc-VMware20-1:~$ su -
密码: 
root@btc-VMware20-1:~# mkdir /test
root@btc-VMware20-1:~# 

  • 普通用户的权限一般是在HOME目录内不受限的
  • 一旦出了HOME目录,大多数地方,普通用户仅有只读和执行权限,无修改权限

su和exit命令

在前面,我们接触过su命令切换到root用户。

su命令就是用于账户切换的系统指令,来源于英文单词:Switch User

语法:su [-] [用户名]

  • -符号是可选的,表示是否在切换用户后加载环境变量,建议带上

  • 参数:用户名,表示要切换的用户,也可以省略,表示切换到root

  • 切换用户后,可以通过exit退回到上一个用户,也可以使用快捷键CTRL + d

  • 使用普通用户,切换到其他用户需要输入密码,如切换到root用户

  • 使用root用户切换到其他用户,无需密码,可以直接切换

root@btc-VMware20-1:~# exit
注销
btc@btc-VMware20-1:~$ su -
密码: 
root@btc-VMware20-1:~# su - btc
btc@btc-VMware20-1:~$ 
注销
root@btc-VMware20-1:~# ^C

sudo命令

在我们得知root密码的时候,可以通过su命令切换到root得到最大权限。

但是我们不建议长期使用root用户,避免带来系统损坏

我们可以使用sudo命令,给普通的命令授权,临时以root身份执行

语法:sudo 其他命令

  • 在其他命令之前,带上sudo,为普通命令授权root权限

  • 但是并不是所有的用户,都有权利使用sudo,我们需要为普通用户配置sudo认证


为普通用户配置sudo认证

image

root@btc-VMware20-1:~# visudo
root@btc-VMware20-1:~# 
注销
btc@btc-VMware20-1:~$ sudo mldir /test1
sudo: mldir:找不到命令
btc@btc-VMware20-1:~$ sudo mkdir /test1
btc@btc-VMware20-1:~$ mkdir /test1
mkdir: 无法创建目录 "/test1": 文件已存在
btc@btc-VMware20-1:~$ mkdir /test13
mkdir: 无法创建目录 "/test13": 权限不够


小结

image


用户、用户组

image

用户组管理

image


用户管理

image

root@btc-VMware20-1:/home# useradd -m -s /bin/bash ttaadd
root@btc-VMware20-1:/home# ls
btc  ttaadd
root@btc-VMware20-1:/home# userdel -r ttaadd
userdel:ttaadd 信件池 (/var/mail/ttaadd) 未找到
root@btc-VMware20-1:/home#  ls
btc
root@btc-VMware20-1:/home# id test4
uid=1001(test4) gid=1002(hahaha) 组=1002(hahaha)
root@btc-VMware20-1:/home# groupadd hello
root@btc-VMware20-1:/home# usermod -aG hello test4
root@btc-VMware20-1:/home# id test4
uid=1001(test4) gid=1002(hahaha) 组=1002(hahaha),1003(hello)

getent

image

root@btc-VMware20-1:/home# getent passwd
root:x:0:0:root:/root:/bin/bash
......
nm-openvpn:x:121:122:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin
btc:x:1000:1000:btc:/home/btc:/bin/bash
sshd:x:122:65534::/run/sshd:/usr/sbin/nologin
test4:x:1001:1002::/home/test4:/bin/bash

image

root@btc-VMware20-1:/home# getent group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,btc
tty:x:5:
disk:x:6:
lp:x:7:
......
nm-openvpn:x:122:
lxd:x:123:
gamemode:x:986:
gnome-initial-setup:x:985:
btc:x:1000:
yuzi:x:1001:
hahaha:x:1002:
hello:x:1003:test4


小结:

image


查看权限控制

认知权限信息

image

image

btc@btc-VMware20-1:~$ ls -l
总计 36
drwxr-xr-x 3 btc btc 4096 10月 10 12:37 公共
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 模板
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 视频
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 图片
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 文档
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 下载
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 音乐
drwxr-xr-x 5 btc btc 4096  1月 19 16:36 桌面
drwx------ 7 btc btc 4096 10月  9 09:07 snap
# 全是文件夹
btc@btc-VMware20-1:~$ touch 1.txt
btc@btc-VMware20-1:~$ ls -l
总计 36
-rw-rw-r-- 1 btc btc    0  1月 20 13:06 1.txt #创建了一个文件,以-开头
drwxr-xr-x 3 btc btc 4096 10月 10 12:37 公共
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 模板
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 视频
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 图片
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 文档
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 下载
drwxr-xr-x 2 btc btc 4096  9月 10 17:44 音乐
drwxr-xr-x 5 btc btc 4096  1月 19 16:36 桌面
drwx------ 7 btc btc 4096 10月  9 09:07 snap

rwx

image


小结:

image


修改权限控制 —— chmod

chmod命令

image

root@btc-VMware20-1:/# ls -l
总计 4194404
lrwxrwxrwx   1 root root          7  4月 22  2024 bin -> usr/bin
drwxr-xr-x   2 root root       4096  2月 26  2024 bin.usr-is-merged
drwxr-xr-x   4 root root       4096  1月 19 13:11 boot
dr-xr-xr-x   2 root root       4096  8月  6 02:17 cdrom
...
drwxr-xr-x   2 root root       4096  1月 20 02:39 test1
-rw-r--r--   1 root root          0  1月 20 14:30 test.txt #注意这行
drwxrwxrwt  21 root root       4096  1月 20 10:24 tmp
drwxr-xr-x  12 root root       4096  8月  6 00:48 usr
drwxr-xr-x  14 root root       4096  9月 10 17:40 var
root@btc-VMware20-1:/# chmod o=w test.txt
root@btc-VMware20-1:/# ls -l
总计 4194404
lrwxrwxrwx   1 root root          7  4月 22  2024 bin -> usr/bin
drwxr-xr-x   2 root root       4096  2月 26  2024 bin.usr-is-merged
drwxr-xr-x   4 root root       4096  1月 19 13:11 boot
dr-xr-xr-x   2 root root       4096  8月  6 02:17 cdrom
...
drwxr-xr-x   2 root root       4096  1月 20 02:39 test1
-rw-r---w-   1 root root          0  1月 20 14:30 test.txt #权限改变
drwxrwxrwt  21 root root       4096  1月 20 10:24 tmp
drwxr-xr-x  12 root root       4096  8月  6 00:48 usr
drwxr-xr-x  14 root root       4096  9月 10 17:40 var
root@btc-VMware20-1:/# chmod o=rwx test.txt
root@btc-VMware20-1:/# ls -l
总计 4194404
lrwxrwxrwx   1 root root          7  4月 22  2024 bin -> usr/bin
drwxr-xr-x   2 root root       4096  2月 26  2024 bin.usr-is-merged
drwxr-xr-x   4 root root       4096  1月 19 13:11 boot
dr-xr-xr-x   2 root root       4096  8月  6 02:17 cdrom
...
drwxr-xr-x   2 root root       4096  1月 20 02:39 test1
-rw-r--rwx   1 root root          0  1月 20 14:30 test.txt #再次改变
drwxrwxrwt  21 root root       4096  1月 20 10:24 tmp
drwxr-xr-x  12 root root       4096  8月  6 00:48 usr
drwxr-xr-x  14 root root       4096  9月 10 17:40 var
root@btc-VMware20-1:/# 
#注意,在改变权限时,倘若不写相应的权限,则变更为无该权限 
root@btc-VMware20-1:/# chmod -R o=rwx nihao
root@btc-VMware20-1:/# ls
bin                etc                lost+found  proc                snap      test1
bin.usr-is-merged  home               media       root                srv       test.txt
boot               lib                mnt         run                 swap.img  tmp
cdrom              lib64              nihao       sbin                sys       usr
dev                lib.usr-is-merged  opt         sbin.usr-is-merged  test      var
root@btc-VMware20-1:/# ls -l
总计 4194408
lrwxrwxrwx   1 root root          7  4月 22  2024 bin -> usr/bin
drwxr-xr-x   2 root root       4096  2月 26  2024 bin.usr-is-merged
drwxr-xr-x   4 root root       4096  1月 19 13:11 boot
...
drwxr-xr-x   2 root root       4096  8月  6 00:48 mnt
drwxr-xrwx   3 root root       4096  1月 20 14:44 nihao #注意这里
drwxr-xr-x   2 root root       4096  8月  6 00:48 opt
dr-xr-xr-x 384 root root          0  1月 19 00:00 proc
drwx------   6 root root       4096  1月 20 02:59 root
...
drwxrwxrwt  21 root root       4096  1月 20 10:24 tmp
drwxr-xr-x  12 root root       4096  8月  6 00:48 usr
drwxr-xr-x  14 root root       4096  9月 10 17:40 var
root@btc-VMware20-1:/# cd nihao
root@btc-VMware20-1:/nihao# ls -l
总计 4
-rw-r--rwx 1 root root    0  1月 20 14:44 1.txt
drwxr-xrwx 2 root root 4096  1月 20 14:44 zaijian
root@btc-VMware20-1:/nihao# cd zaijian
root@btc-VMware20-1:/nihao/zaijian# ls -l
总计 0
-rw-r--rwx 1 root root 0  1月 20 14:44 2.txt

除此之外,还有快捷方法:chmod 751 hello.txt

将hello.txt的权限修改为751

那么问题来了,751表示什么?


权限的数字序号

image

所以7 5 1 就是 u=7 g=5 o=1

速记法:r=4,w=2,x=1 组合相加即可


小结:

image


修改权限控制 —— chown

chown命令

image

root@btc-VMware20-1:/nihao/zaijian# id test4
uid=1001(test4) gid=1002(hahaha) 组=1002(hahaha),1004(TEST)
root@btc-VMware20-1:/nihao/zaijian# ls -l
总计 0
-rw-r--rwx 1 root root 0  1月 20 14:44 2.txt
root@btc-VMware20-1:/nihao/zaijian# chmod 751 2.txt
root@btc-VMware20-1:/nihao/zaijian# ls -l
总计 0
-rwxr-x--x 1 root root 0  1月 20 14:44 2.txt
root@btc-VMware20-1:/nihao/zaijian# chown test4 : TEST 2.txt
chown: 无法访问 ':': 没有那个文件或目录
chown: 无法访问 'TEST': 没有那个文件或目录
root@btc-VMware20-1:/nihao/zaijian# chown test4:TEST 2.txt
root@btc-VMware20-1:/nihao/zaijian# ls -l
总计 0
-rwxr-x--x 1 test4 TEST 0  1月 20 14:44 2.txt
  • 普通用户没有权限把文件(夹)的所有者修改到其他的用户(组)身上

小结:

image

posted @ 2026-01-20 16:52  yorkchain  阅读(0)  评论(0)    收藏  举报