linux用户及权限管理

一、用户和组管理

1.1、用户的分类

第一类:管理员

  • uid=0用户
  • 默认root的uid是0,所以root是管理员。
  • 系统默认创建的
  • 权限非常高,可以执行任意操作

第二类:系统用户

  • uid是1-500
  • 通常无法登录系统,而且也没有用户家目录
  • 系统用户的作用是用来运行一个特定的程序

第三类:普通用户:

  • uid>500
  • 普通用户都是在系统创建完成后,新创建的用户。
  • 普通用户权限非常低,通常只能在自己的家目录下进行操作

1.2、用户账号文件------passwd

所有用户都可以访问passwd文件中的内容,但只有root用户才能更改。

普通用户默认shell:/bin/bash

程序用户使用:/sbin/nologin,不能登录操作系统。

bash 复制代码
[root@centos7 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash

#这个文件中,每行是一个用户,每行信息都是用冒号分割的七段
#第一段:用户名
#第二段:密码或者密码占位符
#第三段:是用户的uid
#第四段:是用户的gid,gid就是用户所在的组的id
#第五段:是用户的描述信息,可以没有
#第六段:用户的家目录
#第七段:用户的shell类型,shell类型决定用户是否可以登录系统

1.3、用户密码文件------shadow

bash 复制代码
[root@centos7 ~]# cat /etc/shadow

1.4、组帐号文件------group

bash 复制代码
[root@centos7 ~]# cat /etc/group

1.5、groupadd添加组账号

bash 复制代码
[root@centos7 ~]# groupadd sales
[root@centos7 ~]# groupadd market
[root@centos7 ~]# tail -5 /etc/group
ntp:x:38:
tcpdump:x:72:
ob:x:1000:ob
sales:x:1001:
market:x:1002:
[root@centos7 ~]# groupadd -g 2000 it
[root@centos7 ~]# groupadd dba
[root@centos7 ~]# tail -5 /etc/group
ob:x:1000:ob
sales:x:1001:
market:x:1002:
it:x:2000:
dba:x:2001:

1.6、groupdel删除组

bash 复制代码
[root@centos7 ~]# groupdel market
[root@centos7 ~]# tail -5 /etc/group
tcpdump:x:72:
ob:x:1000:ob
sales:x:1001:
it:x:2000:
dba:x:2001:

1.7、useradd创建用户账号

useradd [选项] 用户名

-o:通常和-u一块使用,实现uid的复用

-u X:指定用户的uid

-g X:指定用户的gid

-s /bin/bash:指定用户的shell类型

-d /home/xxxx:指定用户的家目录

-r:创建一个系统用户

-M:不创建用户家目录

bash 复制代码
#用户账户创建后,要及时设置密码
[root@centos7 ~]# useradd tom
[root@centos7 ~]# tail -2 /etc/passwd
ob:x:1000:1000:ob:/home/ob:/bin/bash
tom:x:1001:2002::/home/tom:/bin/bash

#创建用户bob,指定uid600,主组sales,附加组it,dba
[root@centos7 ~]# useradd -u 600 -g sales -G it,dba bob
[root@centos7 ~]# tail -2 /etc/passwd
tom:x:1001:2002::/home/tom:/bin/bash
bob:x:600:1001::/home/bob:/bin/bash
[root@centos7 ~]# tail -5 /etc/group
ob:x:1000:ob
sales:x:1001:
it:x:2000:bob
dba:x:2001:bob
tom:x:2002:

1.8、passwd设置密码

bash 复制代码
#可以之间设置
[root@centos7 ~]# passwd tom
Changing password for user tom.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

#也可以通过stdin选项进行明文设置
[root@centos7 ~]# echo '123456' | passwd --stdin bob
Changing password for user bob.
passwd: all authentication tokens updated successfully.

1.9、usermod修改用户账号属性

bash 复制代码
#用户账号创建完成后,可使用usermod -G修改用户附加组
#使用usermod命令修改tom用户的附加组为dba
[root@centos7 ~]# id tom
uid=1001(tom) gid=2002(tom) groups=2002(tom)
[root@centos7 ~]# usermod -G dba tom
[root@centos7 ~]# id tom
uid=1001(tom) gid=2002(tom) groups=2002(tom),2001(dba)

1.10、userdel删除用户账号

bash 复制代码
[root@centos7 ~]# userdel -r tom
[root@centos7 ~]# tail -2 /etc/passwd
ob:x:1000:1000:ob:/home/ob:/bin/bash
bob:x:600:1001::/home/bob:/bin/bash

1.11、用户和组账户查询

bash 复制代码
[root@centos7 ~]# id
uid=0(root) gid=0(root) groups=0(root)
[root@centos7 ~]# id bob
uid=600(bob) gid=1001(sales) groups=1001(sales),2000(it),2001(dba)
[root@centos7 ~]# id ob
uid=1000(ob) gid=1000(ob) groups=1000(ob)

二、Linux权限管理

2.1、查看文件权限

r(4):读权限

w(2):写权限

x(1):执行权限

-(0):无任何权限

bash 复制代码
[root@centos7 ~]# ll
total 8
-rw-------. 1 root root 2123 Oct 30 15:48 anaconda-ks.cfg
-rw-r--r--. 1 root root 2154 Oct 30 15:49 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 30 16:07 下载
drwxr-xr-x. 2 root root    6 Oct 30 16:07 公共
drwxr-xr-x. 2 root root    6 Oct 30 16:07 图片
drwxr-xr-x. 2 root root    6 Oct 30 16:07 文档
drwxr-xr-x. 2 root root    6 Oct 30 16:07 桌面
drwxr-xr-x. 2 root root    6 Oct 30 16:07 模板
drwxr-xr-x. 2 root root    6 Oct 30 16:07 视频
drwxr-xr-x. 2 root root    6 Oct 30 16:07 音乐

-rw-r--r--

第一位:表示的是文件的类型

-:表示这个文件是一般文件

d:表示这个文件是目录文件

第二位-第四位:表示的是文件的属主具有的权限

第五位-第七位:表示的是文件的属组用户具有权限

第八位-第十位:表示的是文件的其他用户具有权限

2.2、设置文件权限chmod

2.2.1、文件的读权限 r

bash 复制代码
# 创建联系目录/practice
[root@centos7 ~]# mkdir /practice
[root@centos7 ~]# ls -ld practice
ls: cannot access practice: No such file or directory
[root@centos7 ~]# ls -ld /practice
drwxr-xr-x 2 root root 6 Nov  6 02:03 /practice
[root@centos7 ~]# cd /practice/
[root@centos7 practice]# echo 'hello root' > rootfile
[root@centos7 practice]# ll
total 4
-rw-r--r-- 1 root root 11 Nov  6 02:04 rootfile
[root@centos7 practice]# cat rootfile
hello root

# tom用户登录查看,tom用户不是此文件的拥有者root,也不属于文件的属组root组,身份是其他人
# 对应的权限是后三位r--
[root@centos7 practice]# su tom
[tom@centos7 practice]$ ls -l
total 4
-rw-r--r-- 1 root root 11 Nov  6 02:04 rootfile
[tom@centos7 practice]$ id 
uid=1001(tom) gid=2002(tom) groups=2002(tom),2001(dba)
[tom@centos7 practice]$ cat rootfile 
hello root
[tom@centos7 practice]$ echo 'hello tom' >> rootfile 
bash: rootfile: Permission denied

# tom用户的权限是r--,可以查看文件内容,但没有写权限W,故不可以修改文件内容,也不可以vim编辑
# root用户修改文件权限,取消其他人身份的读权限r
[root@centos7 practice]# ll
total 4
-rw-r--r-- 1 root root 11 Nov  6 02:04 rootfile
[root@centos7 practice]# chmod o-r rootfile 
[root@centos7 practice]# ll
total 4
-rw-r----- 1 root root 11 Nov  6 02:04 rootfile

# tom用户尝试查看文件内容没有权限
[tom@centos7 practice]$ ll
total 4
-rw-r----- 1 root root 11 Nov  6 02:04 rootfile
[tom@centos7 practice]$ cat rootfile 
cat: rootfile: Permission denied

# root用户恢复权限
[root@centos7 practice]# chmod o+r rootfile 
[root@centos7 practice]# ll
total 4
-rw-r--r-- 1 root root 11 Nov  6 02:04 rootfile

2.2.2、文件的写权限 w

bash 复制代码
# root用户修改文件权限,增加其他人身份的写权限w
[root@centos7 practice]# ll
total 4
-rw-r--r-- 1 root root 11 Nov  6 02:04 rootfile
[root@centos7 practice]# chmod o+w rootfile 
[root@centos7 practice]# ll
total 4
-rw-r--rw- 1 root root 11 Nov  6 02:04 rootfile

# tom用户可以正常修改文件内容
[tom@centos7 practice]$ ll
total 4
-rw-r--rw- 1 root root 11 Nov  6 02:04 rootfile
[tom@centos7 practice]$ echo 'hello tom' >> rootfile 
[tom@centos7 practice]$ cat rootfile 
hello root
hello tom

2.2.3、文件的执行权限 x

bash 复制代码
[root@centos7 practice]# echo 'echo hello,dba' > file1
[root@centos7 practice]# cat file1 
echo hello,dba
[root@centos7 practice]# ll file1 
-rw-r--r-- 1 root root 15 Nov  6 02:42 file1
[root@centos7 practice]# ./file1
bash: ./file1: Permission denied
[root@centos7 practice]# chmod 755 file1
[root@centos7 practice]# ll file1 
-rwxr-xr-x 1 root root 15 Nov  6 02:42 file1
[root@centos7 practice]# ./file1
hello,dba

2.2.4、 目录的读权限 r

bash 复制代码
# root用户创建目录rootdir并修改目录其他人权限,取消所有权限
[root@centos7 practice]# mkdir rootdir
[root@centos7 practice]# ls -ld rootdir/
drwxr-xr-x 2 root root 6 Nov  6 02:45 rootdir/
[root@centos7 practice]# chmod o= rootdir/
[root@centos7 practice]# ls -ld rootdir/
drwxr-x--- 2 root root 6 Nov  6 02:45 rootdir/
[root@centos7 practice]# echo 'rootfile' > /practice/rootdir/rootfile
[root@centos7 practice]# ls rootdir/
rootfile


# tom用户查看文件内容,没有权限
[tom@centos7 practice]$ ls -ld rootdir/
drwxr-x--- 2 root root 22 Nov  6 02:46 rootdir/
[tom@centos7 practice]$ ls rootdir/
ls: cannot open directory rootdir/: Permission denied


#root用户增加可读权限 r
[root@centos7 practice]# chmod o+r rootdir/
[root@centos7 practice]# ls -ld rootdir/
drwxr-xr-- 2 root root 22 Nov  6 02:46 rootdir/

[tom@centos7 practice]$ ls -ld rootdir/
drwxr-xr-- 2 root root 22 Nov  6 02:46 rootdir/
[tom@centos7 practice]$ ls rootdir/
rootfile

2.2.5、目录的执行权限 x

bash 复制代码
# tom用户尝试进入rootdir目录下,没有权限
[tom@centos7 practice]$ ls -ld rootdir/
drwxr-xr-- 2 root root 22 Nov  6 02:46 rootdir/
[tom@centos7 practice]$ cd rootdir/
bash: cd: rootdir/: Permission denied

# root用户增加可执行权限 x
[root@centos7 practice]# ls -ld rootdir/
drwxr-xr-- 2 root root 22 Nov  6 02:46 rootdir/
[root@centos7 practice]# chmod o+x rootdir/
[root@centos7 practice]# ls -ld rootdir/
drwxr-xr-x 2 root root 22 Nov  6 02:46 rootdir/

# tom用户查看
[tom@centos7 practice]$ ls -ld rootdir/
drwxr-xr-x 2 root root 22 Nov  6 02:46 rootdir/
[tom@centos7 practice]$ cd rootdir/
[tom@centos7 rootdir]$ pwd
/practice/rootdir

2.2.6、目录的写权限 w

bash 复制代码
# tom用户新建一个文件,没有权限
[tom@centos7 rootdir]$ ls -ld
drwxr-xr-x 2 root root 22 Nov  6 02:46 .
[tom@centos7 rootdir]$ touch tomfile
touch: cannot touch 'tomfile': Permission denied

# root用户增加写权限
[root@centos7 practice]# chmod o+w rootdir/
[root@centos7 practice]# ls -ld rootdir/
drwxr-xrwx 2 root root 22 Nov  6 02:46 rootdir/

# tom用户查看
[tom@centos7 rootdir]$ ls -ld
drwxr-xrwx 2 root root 22 Nov  6 02:46 .
[tom@centos7 rootdir]$ touch tomfile
[tom@centos7 rootdir]$ ll
total 4
-rw-r--r-- 1 root root 9 Nov  6 02:46 rootfile
-rw-rw-r-- 1 tom  tom  0 Nov  6 03:00 tomfile

2.3、设置文件归属chown

2.3.1、修改属主

bash 复制代码
[root@centos7 practice]# cd rootdir/
[root@centos7 rootdir]# ll
total 0
-rw-rw-r-- 1 tom tom 0 Nov  6 03:00 tomfile
[root@centos7 rootdir]# chown root tomfile 
[root@centos7 rootdir]# ll
total 0
-rw-rw-r-- 1 root tom 0 Nov  6 03:00 tomfile

2.3.2、修改属组

bash 复制代码
[root@centos7 rootdir]# ll
total 0
-rw-rw-r-- 1 root tom 0 Nov  6 03:00 tomfile
[root@centos7 rootdir]# chown :root tomfile 
[root@centos7 rootdir]# ll
total 0
-rw-rw-r-- 1 root root 0 Nov  6 03:00 tomfile

2.3.3、修改属主属组

bash 复制代码
[root@centos7 rootdir]# ll
total 0
-rw-rw-r-- 1 root root 0 Nov  6 03:00 tomfile
[root@centos7 rootdir]# chown tom:tom tomfile 
[root@centos7 rootdir]# ll
total 0
-rw-rw-r-- 1 tom tom 0 Nov  6 03:00 tomfile

2.3.4、-R 递归修改子目录、子文件

bash 复制代码
[root@centos7 practice]# chown -R root:tom rootdir/
[root@centos7 practice]# chmod -R 777 rootdir/
[root@centos7 practice]# ls -ld rootdir/
drwxrwxrwx 2 root tom 21 Nov  6 03:01 rootdir/
[root@centos7 practice]# ls -l rootdir/
total 0
-rwxrwxrwx 1 root tom 0 Nov  6 03:00 tomfile

2.4、附加权限 set

bash 复制代码
# 普通用户可以执行passwd命令修改密码,但密码要足够复杂
[tom@centos7 rootdir]$ passwd

# 修改密码相当于修改/etc/shadow文件,查看文件权限
[tom@centos7 rootdir]$ ls -ld /etc/shadow
---------- 1 root root 1389 Nov  6 01:56 /etc/shadow

# passwd命令文件有suid,其他人执行此命令时,使用命令的属主root身份来操作
[root@centos7 practice]# which passwd
/usr/bin/passwd
[root@centos7 practice]# ls -ld /usr/bin/passwd
-rwsr-xr-x. 1 root root 27856 Mar 31  2020 /usr/bin/passwd

2.5、附加权限 sticky 粘滞位

bash 复制代码
[root@centos7 ~]# chmod 777 /practice/
[root@centos7 ~]# ls -ld /practice/
drwxrwxrwx 3 root root 50 Nov  6 02:45 /practice/
[root@centos7 ~]# cd /practice/
[root@centos7 practice]# touch rootfile
[root@centos7 practice]# ll
total 8
-rwxr-xr-x 1 root root 15 Nov  6 02:42 file1
drwxrwxrwx 2 root tom  21 Nov  6 03:01 rootdir
-rw-r--rw- 1 root root 21 Nov  6 03:31 rootfile


# tom用户修改文件内容
[tom@centos7 practice]$ ll
total 8
-rwxr-xr-x 1 root root 15 Nov  6 02:42 file1
drwxrwxrwx 2 root tom  21 Nov  6 03:01 rootdir
-rw-r--rw- 1 root root 21 Nov  6 03:31 rootfile
[tom@centos7 practice]$ echo 'hello root' > rootfile 


# tom用户成功删除共享目录中的其他用户文件,因为对此目录有写权限
[tom@centos7 practice]$ ll
total 8
-rwxr-xr-x 1 root root 15 Nov  6 02:42 file1
drwxrwxrwx 2 root tom  21 Nov  6 03:01 rootdir
-rw-r--rw- 1 root root 11 Nov  6 03:32 rootfile
[tom@centos7 practice]$ rm rootfile 
[tom@centos7 practice]$ ll
total 4
-rwxr-xr-x 1 root root 15 Nov  6 02:42 file1
drwxrwxrwx 2 root tom  21 Nov  6 03:01 rootdir


# 为共享目录增加sticky粘滞位权限
[root@centos7 practice]# chmod o+t /practice/
[root@centos7 practice]# ls -ld /practice/
drwxrwxrwt 3 root root 34 Nov  6 03:34 /practice/
[root@centos7 practice]# touch rootfile
[root@centos7 practice]# ll
total 4
-rwxr-xr-x 1 root root 15 Nov  6 02:42 file1
drwxrwxrwx 2 root tom  21 Nov  6 03:01 rootdir
-rw-r--r-- 1 root root  0 Nov  6 03:35 rootfile

[tom@centos7 practice]$ ll
total 4
-rwxr-xr-x 1 root root 15 Nov  6 02:42 file1
drwxrwxrwx 2 root tom  21 Nov  6 03:01 rootdir
-rw-r--r-- 1 root root  0 Nov  6 03:35 rootfile
[tom@centos7 practice]$ rm rootfile 
rm: remove write-protected regular empty file 'rootfile'? y
rm: cannot remove 'rootfile': Operation not permitted
[tom@centos7 practice]$ ll
total 4
-rwxr-xr-x 1 root root 15 Nov  6 02:42 file1
drwxrwxrwx 2 root tom  21 Nov  6 03:01 rootdir
-rw-r--r-- 1 root root  0 Nov  6 03:35 rootfile
相关推荐
塔能物联运维3 小时前
物联网运维中的自适应DNS解析优化与动态负载均衡技术
运维·物联网·负载均衡
wheeldown3 小时前
【Linux】Linux内存管理与线程控制核心解析
linux·运维·服务器
努力努力再努力wz4 小时前
【Linux进阶系列】:线程(下)
linux·运维·服务器·c语言·数据结构·c++·算法
LCG元4 小时前
Linux 防火墙双雄:iptables 与 firewalld 配置案例详解
linux
KV_T4 小时前
centos运维常用命令
linux·运维·centos
todoitbo4 小时前
使用n8n搭建服务器监控系统:从Webhook到Telegram告警的完整实现
运维·服务器·数据库·ai·向量数据库·流处理·n8n
dessler4 小时前
MYSQL-主键(Primary Key)
linux·运维·mysql
LCG元4 小时前
Nginx 配置入门到实战:从静态网站到负载均衡
linux
全栈小54 小时前
【C#】从一次异步锁逐渐展开浅谈服务器架构解决重复编码问题,我与AI的一次深度讨论得出的一些解决方案
服务器·架构·c#