当前位置: 首页 > news >正文

linux用户及权限管理

一、用户和组管理

1.1、用户的分类

第一类:管理员

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

第二类:系统用户

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

第三类:普通用户:

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

1.2、用户账号文件——passwd

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

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

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

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

1.3、用户密码文件——shadow

[root@centos7 ~]# cat /etc/shadow

1.4、组帐号文件——group

[root@centos7 ~]# cat /etc/group

1.5、groupadd添加组账号

[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删除组

[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:不创建用户家目录

#用户账户创建后,要及时设置密码
[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设置密码

#可以之间设置
[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修改用户账号属性

#用户账号创建完成后,可使用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删除用户账号

[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、用户和组账户查询

[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):无任何权限

[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

# 创建联系目录/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

# 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

[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

# 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

# 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

# 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、修改属主

[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、修改属组

[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、修改属主属组

[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 递归修改子目录、子文件

[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

# 普通用户可以执行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 粘滞位

[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

http://www.dtcms.com/a/577328.html

相关文章:

  • 北京手机网站建设外包WordPress里面自定义功能
  • 网站建设怎么更改图片网站服务器建设合同
  • 快速理解卷积神经网络CNN
  • IPD PDT 核心组成员来源及扩展组配置说明
  • 51项目分享:基于51单片机仓库环境检测系统
  • Vivado 2015在WIN11电脑综合一直卡在Translating synthesized netlist不动。
  • 绘制软件的状态机图
  • 基于python与streamlit构建的内网聊天应用
  • 对于数据结构:堆的超详细保姆级解析—上
  • linux网站建设论文针对网站做搜索引擎做优化
  • 基于超像素和基于图论的图像分割方法
  • 【算法训练营 · 补充】LeetCode Hot100(中)
  • 新能源网站开发网站没有做301定向
  • 【Ubuntu】新服务器配置完全指南
  • 2026年PMI-PBA商业分析师报考时间+条件全解析
  • 计算机图形学·9 几何学
  • 基于MATLAB的梯度下降法实现
  • dw制作简单网站模板下载网站建设工作会议讲话
  • 如何优化多表查询sql?
  • 64QAM信号的数字预失真处理(MATLAB实现)
  • 网站模板下载之后如何修改公司官网怎么设计
  • 崇信县门户网站留言首页杭州做商务网站
  • 只出现一次的数字 II(二)
  • Linux系统编程:(六)深入理解 Linux 软件包管理器——从原理到 yum 实战全攻略
  • NoSql数据库概念
  • OCR 新范式!DeepSeek 以「视觉压缩」替代传统字符识别;Bald Classification数据集助力高精度人像分类
  • jQuery 入门学习教程,从入门到精通,AJAX在jQuery中的应用 —— 详细知识点与实战案例(14)
  • seo优化标签北京seo百度推广
  • joomla 网站模板.net 手机网站源码下载
  • PL27A1旺玖5Gbps USB 3.0主机到主机桥接控制芯片,超高速USB3.0数据对拷线双机跨屏共享文件和数据的USB对拷芯片