linux的用户和文件权限

创建用户

# 创建用户
[root@localhost ~]# adduser lee
# 设置用户密码
[root@localhost ~]# passwd lee

Changing password for user lee.
New password: 输入密码
BAD PASSWORD: The password is a palindrome
Retype new password: 再次输入密码
passwd: all authentication tokens updated successfully.

# 查看用户所属的用户组
[root@localhost ~]# groups lee
lee : lee

前面一个对应的是用户,后面一个对应的是用户组

# /home 目录下已经创建用户目录
[root@localhost ~]# ls /home/
lee

新建的用户一般没有sudo权限,将用户加入sudo用户组

[root@localhost ~]# sudo usermod -G sudo lee
usermod: group 'sudo' does not exist

说明:
    centos默认没有sudo组,可以将用户组指向wheel用户组,wheel用户组同样具有sudo权限。

[root@localhost ~]# usermod -G wheel lee
[root@localhost ~]# groups lee
lee : lee wheel

修改后可能需要重新登录用户才能生效。

删除用户

# 删除用户
[root@localhost ~]# userdel lee

# 删除用户目录
[root@localhost ~]# rm -rf /home/lee/

# 直接删除用户和用户目录
[root@localhost ~]# userdel lee -r

新建用户组

# 查看用户组
[root@localhost ~]# cd /etc/
[root@localhost etc]# tail -5 group
postdrop:x:90:
postfix:x:89:
cgred:x:995:
dockerroot:x:994:
lee:x:1000:

# 新建用户组 testgroup
[root@localhost etc]# groupadd testgroup
# 再次查看
[root@localhost etc]# tail -5 group
postfix:x:89:
cgred:x:995:
dockerroot:x:994:
lee:x:1000:
testgroup:x:1001:

# 将创建的用户添加到新的用户组
[lee@localhost etc]$ sudo usermod -G testgroup lee

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for lee: 输入用户的密码

# 查看用户所属用户组
[lee@localhost etc]$ groups lee
lee : lee testgroup

删除用户组

# 删除用户组
[root@localhost etc]# groupdel testgroup

# 查看用户组
[root@localhost etc]# tail -5 group
postdrop:x:90:
postfix:x:89:
cgred:x:995:
dockerroot:x:994:
lee:x:1000:

# 查看用户所属用户组
[root@localhost etc]# groups lee
lee : lee

文件类型和权限

[root@localhost ~]# mkdir demo
[root@localhost ~]# touch file.ext
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1331 Mar  6  2019 anaconda-ks.cfg
drwxr-xr-x  2 root root    6 Nov  6 14:49 demo
-rw-r--r--  1 root root    0 Nov  6 14:50 file.ext

以目录 demo 为例

drwxr-xr-x 2 root root 6 Nov 6 14:49 demo

  • drwxr-xr-x : 文件类型和权限
  • 2 :链接数
  • root :所有者
  • root :所属用户组
  • 6 :文件大小
  • Nov 6 14:49:修改时间
  • demo :文件名

说明:
drwxr-xr-x 中的第一个字母是文件类型, 后面9个字母是权限

文件类型的种类分为:

  • d : 目录
  • l : 软链接(理解为windows的快捷方式)
  • b : 块设备
  • c : 字符设备
  • s : socket
  • p : 管道
  • (-) : 普通文件

权限分为:

  • r : 允许读权限
  • w : 允许写权限
  • x : 允许执行权限

9个字符中每三个字符按顺序对应于: 拥有者权限,所属用户组权限,其他用户权限

变更文件所有者:

# 先查看文件信息
[root@localhost ~]# ll
-rw-r--r--  1 root root    0 Nov  6 14:50 file.txt

# 变更文件所有者为lee
[root@localhost ~]# chown lee file.txt

# 验证
[root@localhost ~]# ll
-rw-r--r--  1 lee  root    0 Nov  6 14:50 file.txt

修改文件权限

方式一: 二进制数字表示

rwx    1*2^2 + 1*2^1 + 1*2^0 = 7   (全部权限)  
r-x    1*2^2 + 0*2^1 + 1*2^0 = 5   (没有写权限)  
r--    1*2^2 + 0*2^1 + 0*2^0 = 4   (只有读权限)


如: 修改文件的所有权限赋给所有的用户
[root@localhost ~]# chmod 777 file.txt

# 验证
[root@localhost ~]# ll
-rwxrwxrwx  1 lee  root    0 Nov  6 14:50 file.txt

方式二:加减赋值操作

# 查看文件
[root@localhost ~]# ll
-rwxrwxrwx  1 lee  root    0 Nov  6 14:50 file.txt

# 修改权限 -- 去除用户组和其他用户的读写权限
[root@localhost ~]# chmod go-rw file.txt

# 验证
[root@localhost ~]# ll
-rwx--x--x  1 lee  root    0 Nov  6 14:50 file.txt

说明:

chmod go-rw file.txt  

命令中的    g:  group  用户组
            o:  other  其他用户
            u:  user   拥有者
            +:  增加权限
            -:  去掉权限

你可能感兴趣的:(linux的用户和文件权限)