Linux组管理和权限管理

Linux组管理和权限管理

Linux 系统中每个用户都必须属于一个组。不能独立于组外,在Linux中每个文件有所有组、所在组、其他组的概念

  • 所有者
  • 所在组
  • 其他组

文件/目录-所有者

一般为文件的创建者,谁创建了该文件,谁就自然成为该文件的所有者

查看文件所有者 ls-ahl
[ylq@rootylq ~]$ ls -ahl
total 52K
drwx------  3 ylq  ylq  4.0K Jan  5 16:11 .
drwxr-xr-x. 4 root root 4.0K Jan  5 13:43 ..
-rw-------  1 ylq  ylq  1.4K Jan  6 00:12 .bash_history
-rw-r--r--  1 ylq  ylq    18 Nov 25  2021 .bash_logout
-rw-r--r--  1 ylq  ylq   193 Nov 25  2021 .bash_profile
-rw-r--r--  1 ylq  ylq   231 Nov 25  2021 .bashrc
-rw-r--r--  1 ylq  ylq    97 Jan  5 13:24 hello.cpp
-rw-------  1 ylq  ylq    49 Jan  5 13:10 .lesshst
-rw-rw-r--  1 ylq  ylq  5.6K Jan  5 16:12 myhome.tar.gz
-rw-rw-r--  1 ylq  ylq  2.7K Jan  5 15:56 myhome.zip
-rw-rw-r--  1 ylq  ylq   237 Jan  5 16:10 pc.tar.gz
-rw-rw-r--  1 ylq  ylq     0 Jan  5 16:08 pig.txt
drwxrwxr-x  3 ylq  ylq  4.0K Jan  5 16:17 proj
[ylq@rootylq ~]$ 

修改文件所有者 chown
[root@rootylq ylq]# chown tom apple.txt 
[root@rootylq ylq]# ls
apple.txt  hello.cpp
[root@rootylq ylq]# ls -ahl
total 32K
drwx------  2 ylq  ylq  4.0K Jan  6 09:46 .
drwxr-xr-x. 5 root root 4.0K Jan  6 09:48 ..
-rw-rw-r--  1 tom  ylq     0 Jan  6 09:46 apple.txt
-rw-------  1 ylq  ylq  1.4K Jan  6 00:12 .bash_history
-rw-r--r--  1 ylq  ylq    18 Nov 25  2021 .bash_logout
-rw-r--r--  1 ylq  ylq   193 Nov 25  2021 .bash_profile
-rw-r--r--  1 ylq  ylq   231 Nov 25  2021 .bashrc
-rw-r--r--  1 ylq  ylq    97 Jan  5 13:24 hello.cpp
-rw-------  1 ylq  ylq    49 Jan  5 13:10 .lesshst
[root@rootylq ylq]# 

[root@rootylq ylq]# chown -R tom /home/ylq/
[root@rootylq ylq]# ll
total 4
-rw-rw-r-- 1 tom ylq    0 Jan  6 09:46 apple.txt
-rw-r--r-- 1 tom ylq   97 Jan  5 13:24 hello.cpp
-rwx-wx-wx 1 tom fruit  0 Jan  6 10:00 orange.txt



创建组 groupadd

[root@rootylq ylq]# groupadd monster
[root@rootylq ylq]# useradd -g monster newuser
[root@rootylq ylq]# who is newuser
root     pts/0        2023-01-06 09:33 (115.236.56.78)
[root@rootylq ylq]# id newuser
uid=1006(newuser) gid=1007(monster) groups=1007(monster)
[root@rootylq ylq]# 

所在组

当某个用户创建了一个文件后,这个文件的所在组就是该用户的所在组

[newuser@rootylq ~]$ touch ok.txt
[newuser@rootylq ~]$ ls
ok.txt
[newuser@rootylq ~]$ ll
total 0
-rw-r--r-- 1 newuser monster 0 Jan  6 09:57 ok.txt
[newuser@rootylq ~]$ 
修改文件所在组 chgrp
[root@rootylq ylq]# groupadd fruit
[root@rootylq ylq]# touch orange.txt
[root@rootylq ylq]# ll
total 4
-rw-rw-r-- 1 tom  ylq   0 Jan  6 09:46 apple.txt
-rw-r--r-- 1 ylq  ylq  97 Jan  5 13:24 hello.cpp
-rw-r--r-- 1 root root  0 Jan  6 10:00 orange.txt
[root@rootylq ylq]# chgrp fruit orange.txt 
[root@rootylq ylq]# ll
total 4
-rw-rw-r-- 1 tom  ylq    0 Jan  6 09:46 apple.txt
-rw-r--r-- 1 ylq  ylq   97 Jan  5 13:24 hello.cpp
-rw-r--r-- 1 root fruit  0 Jan  6 10:00 orange.txt
[root@rootylq ylq]# 

#将目录下的所有文件的所在组进行修改
[root@rootylq ylq]# chgrp -R ylq /home/ylq 
[root@rootylq ylq]# ll
total 4
-rw-rw-r-- 1 ylq ylq  0 Jan  6 09:46 apple.txt
-rw-r--r-- 1 ylq ylq 97 Jan  5 13:24 hello.cpp
-rwx-wx-wx 1 ylq ylq  0 Jan  6 10:00 orange.txt

改变用户所在的组 usermod
[root@rootylq ylq]# cat /etc/group | grep fruit
fruit:x:1008:
[root@rootylq ylq]# id newuser
uid=1006(newuser) gid=1007(monster) groups=1007(monster)
[root@rootylq ylq]# usermod -g fruit newuser
[root@rootylq ylq]# id newuser
uid=1006(newuser) gid=1008(fruit) groups=1008(fruit)

改变用户登录的初始目录

usermod -g 目录名 用户名

用户需要有进入新目录的权限

其他组

除文件的所有者和所在组的用户外,系统的其他用户都是文件的其他组

Linux系统的权限管理

-rw-r--r-- 1 root fruit  0 Jan  6 10:00 orange.txt

0-9 位 (-rw-r–r–)

  • 第0位确定文件的类型(d,-,l,c,b)
    • l:链接
    • d:目录
    • c:字符设备
    • b:块设备
    • -:普通文件
  • 第1-3位确定所有者(该文件的所有者)拥有该文件的权限 --User
  • 第4-6位确定所属组(同用户组的)拥有该文件的权限 --Group
  • 第7-9位确定其他用户对该文件的权限 --Other

rwx可用数字表示

  • r:4
  • w:2
  • x:1
  • rwx=4+2+1=7

其他说明

  • 1 :文件:硬链接数或目录 目录:子目录
  • root : 用户(所有者)
  • fruit:组(所在组)
  • 0 :文件大小(字节)如果是文件,显示4096
  • Jan 6 10:00 :最后修改文件时间
  • orange.txt :文件名

rwx权限

rwx作用在文件上
  • 【r】代表可读,可以被读取,查看
  • 【w】代表可以写,可以被修改,但不代表可以被删除文件,删除文件需要对文件所在的目录拥有写权限,仅对文件有写权限不可以
  • 【x】代表可执行,可以被执行
rwx作用在目录上
  • 【r】代表可读,可以被读取,ls查看目录内容
  • 【w】代表可以写,可以被修改,对目录内允许创建+删除+重命名其中的文件
  • 【x】代表可执行,可以进入该目录

修改权限 chmod

chmod指令,可以修改文件或则目录的权限

方式一:+、-、=变更权限

u :所有者 g : 所在组 o:其他人 a:所有人(u、g、o的总和)

[root@rootylq ylq]# chmod u=rwx,g=rx,o=x orange.txt 
[root@rootylq ylq]# ll
total 4
-rw-rw-r-- 1 tom  ylq    0 Jan  6 09:46 apple.txt
-rw-r--r-- 1 ylq  ylq   97 Jan  5 13:24 hello.cpp
-rwxr-x--x 1 root fruit  0 Jan  6 10:00 orange.txt
[root@rootylq ylq]# chmod u-x,g+w orange.txt 
[root@rootylq ylq]# ll
total 4
-rw-rw-r-- 1 tom  ylq    0 Jan  6 09:46 apple.txt
-rw-r--r-- 1 ylq  ylq   97 Jan  5 13:24 hello.cpp
-rw-rwx--x 1 root fruit  0 Jan  6 10:00 orange.txt
[root@rootylq ylq]# chmod a+r orange.txt 
[root@rootylq ylq]# ll
total 4
-rw-rw-r-- 1 tom  ylq    0 Jan  6 09:46 apple.txt
-rw-r--r-- 1 ylq  ylq   97 Jan  5 13:24 hello.cpp
-rw-rwxr-x 1 root fruit  0 Jan  6 10:00 orange.txt

方式二:通过数字变更权限

rwx可用数字表示

  • r:4
  • w:2
  • x:1
  • rwx=4+2+1=7
[root@rootylq ylq]# chmod 733 orange.txt 
[root@rootylq ylq]# ls
apple.txt  hello.cpp  orange.txt
[root@rootylq ylq]# ll
total 4
-rw-rw-r-- 1 tom  ylq    0 Jan  6 09:46 apple.txt
-rw-r--r-- 1 ylq  ylq   97 Jan  5 13:24 hello.cpp
-rwx-wx-wx 1 root fruit  0 Jan  6 10:00 orange.txt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值