文章目录
一、文件管理命令
Linux目录结构
bin 普通用户使用的命令 /bin/ls, /bin/date
sbin 管理员使用的命令 /sbin/service
dev 设备文件 /dev/sda,/dev/sda1
root root用户的HOME
home 存储普通用户家目录
tmp 临时文件(全局可写:进程产生的临时文件)
var 存放的是一些变化文件,比如数据库,日志,邮件....
==设备(主要指存储设备)挂载目录==
media 移动设备默认的挂载点
mnt 手工挂载设备的挂载点
etc 配置文件(系统相关如网络/etc/sysconfig/network
proc 虚拟的文件系统,反映出来的是内核,进程信息或实时状态 ,硬件的状态
usr 系统文件,相当于C:\Windows
/usr/local 软件安装的目录,相当于C:\Program
boot 存放的系统启动相关的文件,例如kernel,grub(引导装载程序)
lib 库文件Glibc
lib64 库文件Glibc
lost+found fsck修复时,存储没有链接的文件或目录
1.1.创建文件
touch file.txt
或 touch /home/test/file.txt
1.2. 创建目录
mkdir /路径/目录名
mkdir /home/test
若上级目录不存在使用 mkdir -p /路径/目录名
1.3. 复制
cp 源文件路径 目标文件路径
cp /tmp/test/file.txt /tmp/test2
1.4.移动
mv 原文件路径 目标文件路径
mv /tmp/test/file.txt /tmp/test1
1.5.删除
rm -rf 文件或目录的路径
rm -rf /tmp/test/file.txt
1.6.查看文件
cat more head tail grep
cat语法-全部文件
cat /tmp/file.txt
more语法-翻页
more /tmp/file.txt
head语法-头部开始
head -2 /tmp/file.txt (文件前两行)
tail语法-尾部开始
tail -2 /tmp/file.txt (文件后两行)
grep语法-过滤不需要的数据
grep 'abc' /tmp/file.txt (有abc会以行列出来)
1.7.修改文件内容
1.vim /路径/文件名/
2.按i
3.输入内容
4按esc
5.:+命令; 命令1:wq(保存退出)命令2:w(保存不退出)命令3:q!(不保存强制退出)
6.扩展命令
查找替换
语法 : :范围 s/原内容/新内容/全局
:1,5 s/nihao/hello/g 解释意思:从1-5行的nihao替换成hello
7.光标定位
光标移动 hjkl
行首行尾 0$
页首页尾 gg G
第三行 3G
查询字符串 /string nN可以查看下一个查询的字符串
8.文本编辑:
复制 yy
粘贴 p
删除 dd
撤销 u undo
9.扩展命令模式
:w file2.txt 另存为file2.txt
设置行号 :set nu
取消设置行号 :set nonu
显示控制字符 :set list
1.8.文件类型
b 设备文件(块设备)存储设备硬盘,U盘 /dev/sda, /dev/sda1
c 设备文件(字符设备)打印机,终端 /dev/tty1
l 链接文件(淡蓝色)
s 套接字文件
p 管道文件
二、用户管理
2.1.查看当前登录的用户信息
[root@VM-12-5-centos ~]# id
uid=0(root) gid=0(root) 组=0(root)
2.2.查看文件的owner
[root@VM-12-5-centos ~]# ll /tmp/
总用量 4
-rw-r--r-- 1 root root 0 6月 9 22:01 file.txt
-rw-r--r-- 1 root root 0 6月 9 21:57 stargate.lock
drwxr-xr-x 2 root root 4096 6月 9 22:00 test
2.3.用户的基本信息
不能用vim的方式改/etc/passwd这个文件,主要是来观察
[root@VM-12-5-centos ~]# head -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
root: x :0:0:root:/root:/bin/bash(冒号分割为7列字段)
用户名: x :uid:gid:描述:HOME:shell
root:用户名:登录系统的名字
X:密码占位符:,具体内容不在这里
0:UID: 用户的身份证号uid: 0 特权用户uid: 1~999 系统用户 uid: 1000+ 普通用户
0:GID:GROUP 组号每创建一个用户,系统会自动创建同名的组
root:描述:比如经理 manager (这里使用的默认名)
/root:家目录:登录系统时,所在目录
/bin/bash:登录shell:命令解释器
2.4.用户密码信息文件
[root@VM-12-5-centos ~]# head -2 /etc/shadow
root:$1$JLMlAfEa$wJHKlUu03yTsjm0LFCk0i0:19152:0:99999:7:::
bin:*:17834:0:99999:7:::
[root@VM-12-5-centos ~]#
解释:
root:$1$MYG2NDG6$a1wtyr5GDM2esAPjug0YP0:15636:0:99999:7:: :
--------------------------
1)“登录名”是与/etc/passwd文件中的登录名相一致的用户账号
2)“口令”字段存放的是加密后的用户口令字,如果为空,则对应用户没有口令,登录时不需要口令;
星号代表帐号被锁定;
双叹号表示这个密码已经过期了。
$6$开头的,表明是用SHA-512加密的,
$1$ 表明是用MD5加密的
$2$ 是用Blowfish加密的
$5$ 是用 SHA-256加密的。
3)“最后一次修改时间”表示的是从某个时刻起,到用户最后一次修改口令时的天数。时间起点对不同的系统可能不一样。例如在SCOLinux中,这个时间起点是1970年1月1日。
4)“最小时间间隔”指的是两次修改口令之间所需的最小天数。
5)“最大时间间隔”指的是口令保持有效的最大天数。
6)“警告时间”字段表示的是从系统开始警告用户到用户密码正式失效之间的天数。
7)“不活动时间”表示的是用户没有登录活动但账号仍能保持有效的最大天数。(软限制。到期后多少天就不能用账号了。)
8)“失效时间”字段给出的是一个绝对的天数,如果使用了这个字段,那么就给出相应账号的生存期。期满后,该账号就不再是一个合法的账号,也就不能再用来登录了。(硬限制。)
9) 保留
2.5.组信息文件
[root@VM-12-5-centos ~]# head -2 /etc/group
root: x : 0 :
解释:组名:组密码:组ID:组成员(组成员默认为空)
bin:x:1:
[root@VM-12-5-centos ~]#
2.6.用户管理
1.创建用户
(1).未指定选项
语法:useradd 用户名
[root@VM-12-5-centos ~]# useradd laozhang
[root@VM-12-5-centos ~]# id laozhang
uid=1001(laozhang) gid=1001(laozhang) 组=1001(laozhang)
用户编号=具体数字(用户名) 组编号=具体数字(组名) 组=组ID(成员名字)
(2).指定选项
创建用户,指定uid
语法: useradd 用户名 -u uid
[root@VM-12-5-centos ~]# useradd laozhang03 -u 1004
[root@VM-12-5-centos ~]# id laozhang03
uid=1004(laozhang03) gid=1004(laozhang03) 组=1004(laozhang03)
创建用户 指定家目录
[root@VM-12-5-centos /]# useradd laozhang02 -d /tmp/laozhang02
[root@VM-12-5-centos /]# ls -d /tmp/laozhang02/
/tmp/laozhang02/
2.删除用户
userdel -r 用户名
[root@VM-12-5-centos /]# id laozhang02
uid=1002(laozhang02) gid=1002(laozhang02) 组=1002(laozhang02)
[root@VM-12-5-centos /]# userdel -r laozhang02
[root@VM-12-5-centos /]# id laozhang02
id: laozhang02: no such user
[root@VM-12-5-centos /]#
3.用户密码
方法一:root修改其他用户的密码
[root@VM-12-5-centos /]# useradd meier
[root@VM-12-5-centos /]# passwd meier
更改用户 meier 的密码 。
新的 密码:
无效的密码: 密码是一个回文
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@VM-12-5-centos /]#
方法二:用户登录,自己修改密码。
[meier@VM-12-5-centos ~]$ passwd
更改用户 meier 的密码 。
为 meier 更改 STRESS 密码。
(当前)UNIX 密码:
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[meier@VM-12-5-centos ~]$
4.其他选项管理
修改登录SHELL,修改以后不可登录
[root@VM-12-5-centos /]# usermod -s /sbin/nologin meier
[root@VM-12-5-centos /]# tail -1 /etc/passwd
meier:x:1001:1001::/home/meier:/sbin/nologin
5.组员管理
查看用户原先信息
[root@VM-12-5-centos /]# id meier
uid=1001(meier) gid=1001(meier) 组=1001(meier)
将用户追加到mv组
[root@VM-12-5-centos /]# usermod -aG mv meier
usermod:“mv”组不存在
[root@VM-12-5-centos /]# groupadd mv
[root@VM-12-5-centos /]# usermod -aG mv meier
[root@VM-12-5-centos /]# id meier
uid=1001(meier) gid=1001(meier) 组=1001(meier),1002(mv)
将用户移除组(扩展)
[root@VM-12-5-centos /]# id meier
uid=1001(meier) gid=1001(meier) 组=1001(meier),1002(mv)
[root@VM-12-5-centos /]# gpasswd -d meier mv
正在将用户“meier”从“mv”组中删除
[root@VM-12-5-centos /]# id meier
uid=1001(meier) gid=1001(meier) 组=1001(meier)
[root@VM-12-5-centos /]#
2.7.用户组管理
1.创建一个mv组
[root@VM-12-5-centos /]# groupadd mv
[root@VM-12-5-centos /]# tail -2 /etc/group
meier:x:1001:
mv:x:1002:
[root@VM-12-5-centos /]#
2.创建一个mv2组,指定组的gid
[root@VM-12-5-centos /]# groupadd mv2 -g 1004
[root@VM-12-5-centos /]# tail -3 /etc/group
meier:x:1001:
mv:x:1002:
mv2:x:1004:
[root@VM-12-5-centos /]#
3.删除组
语法:groupdel 组名
[root@VM-12-5-centos /]# tail -3 /etc/group
meier:x:1001:
mv:x:1002:
mv2:x:1004:
[root@VM-12-5-centos /]# groupdel mv2
[root@VM-12-5-centos /]# groupdel mv
[root@VM-12-5-centos /]# tail -3 /etc/group
rdma:x:993:
lighthouse:x:1000:lighthouse
meier:x:1001:
[root@VM-12-5-centos /]#
4.用户组:
(1).基本组-随用户名的创建而创建,组名与用户名同名
(2).附加组-用户后期加入的其他组
2.9.提权
永久提权Switching users with su
例如只有root才能创建用户,现在给meier提权,使用meier来创建新用户。
[meier@VM-12-5-centos ~]$ id meier
uid=1001(meier) gid=1001(meier) 组=1001(meier)
[meier@VM-12-5-centos ~]$ useradd u1
useradd: Permission denied.
useradd:无法锁定 /etc/passwd,请稍后再试。
[meier@VM-12-5-centos ~]$ su - root
密码:
。。su: 鉴定故障
[meier@VM-12-5-centos ~]$ su - root
密码:
上一次登录:四 6月 9 23:41:26 CST 2022从 171.221.83.106pts/3 上
最后一次失败的登录:四 6月 9 23:59:25 CST 2022pts/2 上
最有一次成功登录后有 1 次失败的登录尝试。
[root@VM-12-5-centos ~]# useradd u1
[root@VM-12-5-centos ~]# id u1
uid=1002(u1) gid=1002(u1) groups=1002(u1)
[root@VM-12-5-centos ~]#
临时提权Running commands as root with sudo
将当前用户切换到超级用户下
然后以超级用户身份执行命令,执行完成后,直接退回到当前用户。
具体工作过程如下:
当用户执行sudo时,系统会主动寻找/etc/sudoers文件,判断该用户是否有执行sudo的权限
-->确认用户具有可执行sudo的权限后,让用户输入用户自己的密码确认
-->若密码输入成功,则开始执行sudo后续的命令
sudo配置文件语法
user MACHINE=COMMANDS
用户 登录的主机=(可以变换的身份) 可以执行的命令
示例:
1.以root身份,授权普通用户user1
[root@VM-12-5-centos ~]# useradd user1
[root@VM-12-5-centos ~]# id user1
uid=1002(user1) gid=1002(user1) 组=1002(user1)
2.观察授权信息
[root@localhost ~]# vim /etc/sudoers
107行
107 %wheel ALL=(ALL) ALL
允许wheel用户组 不输入密码 使用所有命令
3.将用户加入到wheel组
[root@VM-12-5-centos ~]# useradd user1 -G wheel
useradd:用户“user1”已存在
[root@VM-12-5-centos ~]# userdel user1
[root@VM-12-5-centos ~]# useradd user1 -G wheel
useradd:警告:此主目录已经存在。
不从 skel 目录里向其中复制任何文件。
正在创建信箱文件: 文件已存在
[root@VM-12-5-centos ~]# userdel -r user1
[root@VM-12-5-centos ~]# useradd user1 -G wheel
[root@VM-12-5-centos ~]# id user1
uid=1002(user1) gid=1002(user1) 组=1002(user1),10(wheel)
4.切换用户使用sudo提权
[user1@VM-12-5-centos ~]$ useradd user2
useradd: Permission denied.
useradd:无法锁定 /etc/passwd,请稍后再试。
[user1@VM-12-5-centos ~]$ sudo useradd user2
我们信任您已经从系统管理员那里了解了日常注意事项。
总结起来无外乎这三点:
#1) 尊重别人的隐私。
#2) 输入前要先考虑(后果和风险)。
#3) 权力越大,责任越大。
[sudo] user1 的密码:
[user1@VM-12-5-centos ~]$ id user2
uid=1003(user2) gid=1003(user2) 组=1003(user2)
三、用户的权限
3.1、基本权限UGO权限对象:
主要对文件或文件夹设置权限
权限对象:
属主: u
属组: g
其他人: o
所有人:a(u+g+o)
权限类型:
读:r=4
写:w=2
执行: x=1
查看权限
[root@VM-12-5-centos ~]# mkdir /root/test.txt
[root@VM-12-5-centos ~]# ll
总用量 4
drwxr-xr-x 2 root root 4096 6月 12 22:38 test.txt
-文件类型
rwx主人的权限,属主
r-x属组的权限,
r-x其他人的权限
.权限的扩展
2文件链接(第七章文件链接)
root文件的属主
root文件的属组
4096大小
6月 12 12 22:38 文件最后的修改时间
test.txt 文件的名和路径
设置权限
(1)更改权限:
使用符号方式:
语法:
使用符号:u用户 g组 o其他 r读 w写 x执行
语法: chmod 对象(u/g/o/a)赋值符(+/-/=)权限类型(r/w/x) 文件/目录
1.了解普通文件的基本权限
[root@VM-12-5-centos tmp]# touch test
[root@VM-12-5-centos tmp]# ll test
-rw-r--r-- 1 root root 0 6月 12 22:46 test
权限 属主 属组 文件
2.设置所有人都有读写执行的权限
[root@VM-12-5-centos tmp]# chmod a=rwx test
[root@VM-12-5-centos tmp]# ll test
-rwxrwxrwx 1 root root 0 6月 12 22:46 test
[root@VM-12-5-centos tmp]#
3.设置所有人的无任何权限
[root@VM-12-5-centos tmp]# chmod a=- test
[root@VM-12-5-centos tmp]# ll test
---------- 1 root root 0 6月 12 22:46 test
[root@VM-12-5-centos tmp]#
4.设置属主属组有读写 其他只能的读的权限
[root@VM-12-5-centos tmp]# chmod ug=rw,o=r test
[root@VM-12-5-centos tmp]# ll test
-rw-rw-r-- 1 root root 0 6月 12 22:46 test
[root@VM-12-5-centos tmp]#
使用数字方式:
4读 2写 1执行
数字相加,下面主组有读写权限,其他只有读权限
[root@VM-12-5-centos tmp]# chmod 664 test
[root@VM-12-5-centos tmp]# ll test
-rw-rw-r-- 1 root root 0 6月 12 22:46 test
(2)、更改属主、属组
chown 命令
chown:设置一个文件属于谁,属主
语法:chown 用户名.组名 文件
chgrp 命令
//改属主、属组(从root root 改成了 user sb)
[root@VM-12-5-centos tmp]# chown user1.sb test
[root@VM-12-5-centos tmp]# ll test
-rw-rw-r-- 1 user1 sb 0 6月 12 22:46 test
//只改属主(从user1 主人改成了 root)
[root@VM-12-5-centos tmp]# ll test
-rw-rw-r-- 1 user1 sb 0 6月 12 22:46 test
[root@VM-12-5-centos tmp]# chown root test
[root@VM-12-5-centos tmp]# ll test
-rw-rw-r-- 1 root sb 0 6月 12 22:46 test
[root@VM-12-5-centos tmp]#
//只改属组(sb组改成root组)
[root@VM-12-5-centos tmp]# ll test
-rw-rw-r-- 1 root sb 0 6月 12 22:46 test
[root@VM-12-5-centos tmp]# chown .root test
[root@VM-12-5-centos tmp]# ll test
-rw-rw-r-- 1 root root 0 6月 12 22:46 test
注意:
-R 针对目录下所有的文件。
chgrp命令
chgrp:设置一个文件属于哪个组,属组
语法:chgrp 组名 文件 -R是递归的意思
//改文件属组(从root改成了sb)
[root@VM-12-5-centos tmp]# ll test
-rw-rw-r-- 1 root root 0 6月 12 22:46 test
[root@VM-12-5-centos tmp]# chgrp sb test
[root@VM-12-5-centos tmp]# ll test
-rw-rw-r-- 1 root sb 0 6月 12 22:46 test
[root@VM-12-5-centos tmp]#
//改文件属组
[root@VM-12-5-centos tmp]# chgrp -R root test
[root@VM-12-5-centos tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 6月 12 22:47 stargate.lock
-rw-rw-r-- 1 root root 0 6月 12 22:46 test
2、基本权限ACL
UGO设置基本权限:只能一个用户,一个组和其他人
ACL文件权限管理:设置不同用户,不同的基本权限(r、w、x),对象不同。
语法:
setfacl -m u:user1:rw /tmp/test.txt
命令 设置 用户或组:用户名:权限 文件对象
使用:
1.文件准备
[root@VM-12-5-centos tmp]# touch test.txt
[root@VM-12-5-centos tmp]# ll test.txt
-rw-r--r-- 1 root root 0 Jun 13 23:01 test.txt
2.查看文件有哪些ACL权限
[root@VM-12-5-centos tmp]# getfacl test.txt
# file: test.txt
# owner: root 属主:root
# group: root 属组:root
user::rw- 用户:属主:rwx
group::r-- 组:属组:rwx
other::r-- other:其他人:rwx
mask::rwx 掩码::rwx
3.设置jack1 有rw 权限
[root@VM-12-5-centos tmp]# setfacl -m u:jack1:rw test.txt
[root@VM-12-5-centos tmp]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:jack1:rw-
group::r--
mask::rw-
other::r--
4.设置其他用户有读写权限
[root@VM-12-5-centos tmp]# setfacl -m o::rw test.txt
[root@VM-12-5-centos tmp]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:jack1:rw-
group::r--
mask::rw-
other::rw-
5.增加hr组队test.txt文件读取的权限
--------------------------重点命令-----------------------------
[root@VM-12-5-centos tmp]# setfacl -m g:hr:rw test.txt
---------------------------------------------------------------
[root@VM-12-5-centos tmp]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:jack1:rw-
group::r--
group:hr:rw-
mask::rw-
other::rw-
5.删除部分权限 -删除组hr的acl权限
[root@VM-12-5-centos tmp]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:jack1:rw-
group::r--
group:hr:rw-
group:hr2:rw-
mask::rw-
other::rw-
--------------------------重点命令-----------------------------
[root@VM-12-5-centos tmp]# setfacl -x g:hr test.txt
---------------------------------------------------------------
[root@VM-12-5-centos tmp]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:jack1:rw-
group::r--
group:hr2:rw-
mask::rw-
other::rw-
6.删除所有acl权限
[root@VM-12-5-centos tmp]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:jack1:rw-
group::r--
group:hr2:rw-
mask::rw-
other::rw-
--------------------------重点命令-----------------------------
*****[root@VM-12-5-centos tmp]# setfacl -b test.txt*****
---------------------------------------------------------------
[root@VM-12-5-centos tmp]# getfa
getfacl getfattr
[root@VM-12-5-centos tmp]# getfa
getfacl getfattr
[root@VM-12-5-centos tmp]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
group::r--
other::rw-
3、特殊权限(了解)
(1)、特殊位 suid
针对文件/程序时,具备临时获得属主的权限。
设置suid,使普通用户通过suid临时提权,查看超管root用户的文件。
1.设置suid,登录user1用户查看file1.txt效果
[root@VM-12-5-centos ~]# ll
总用量 4
-rw-r--r-- 1 root root 0 6月 13 22:02 file1.txt
drwsrwxrwx 2 root root 4096 6月 13 22:36 test
[root@VM-12-5-centos ~]# su - user1
上一次登录:一 6月 13 22:03:25 CST 2022pts/0 上
[user1@VM-12-5-centos ~]$ cat /root/file1.txt
cat: /root/file1.txt: Permission denied
结果,普通用户,看不到了root的内容。
[user1@VM-12-5-centos ~]$
2.设置suid
[root@VM-12-5-centos ~]# ll /usr/bin/cat
-rwxr-xr-x 1 root root 54080 11月 17 2020 /usr/bin/cat
--------------------------重点命令-----------------------------
[root@VM-12-5-centos ~]# chmod u+s /usr/bin/cat
---------------------------------------------------------------
[root@VM-12-5-centos ~]# ll /usr/bin/cat
-rwsr-xr-x 1 root root 54080 11月 17 2020 /usr/bin/cat
--------------------------登录user1后可查看root的文件----------
[root@VM-12-5-centos ~]# su - user1
上一次登录:一 6月 13 23:45:30 CST 2022pts/0 上
[user1@VM-12-5-centos ~]$ cat /root/file1.txt
111
22
33
[user1@VM-12-5-centos ~]$ exit
logout
-----------------------删除权限--------------------------------
[root@VM-12-5-centos ~]# chmod u-s /usr/bin/cat
--------------------------登录user1后不可查看root的文件--------
[root@VM-12-5-centos ~]# su - user1
上一次登录:一 6月 13 23:50:34 CST 2022pts/0 上
[user1@VM-12-5-centos ~]$ cat /root/file1.txt
cat: /root/file1.txt: Permission denied
(2)、文件属性chattr
用途:常用语锁:定某个文件,拒绝修改。
分类:案例:
1 先创建新文件进行对比。查看默认权限。
[root@localhost ~]# touch file100
[root@localhost ~]# lsattr file100
-------------- file100
2 加上不能删除的属性。
不能更改,重命名,删除
[root@localhost ~]# chattr +i file100
3 查看不同属性
[root@localhost ~]# lsattr file100
----i--------- file100
4 尝试删除
[root@localhost ~]# rm -rf file100
rm: cannot remove `file100': Operation not permitted
5 将属性还原。
[root@localhost ~]# chattr -i file100
注意:
设置文件属性(特别权限),针对所有用户
(3)、进程掩码 umask
概述:新建文件、目录的默认权限会受到umask的影响,umask表示要减掉的权限
示例1:观察系统默认掩码
示例1: 在shell进程中创建文件,先查看当前用户的umask权限
[root@localhost ~]# umask
0022
[root@localhost ~]# touch file800
[root@localhost ~]# mkdir dir800
[root@localhost ~]# ll -d dir800 file800
drwxr-xr-x. 2 root root 4096 3月 11 19:40 dir800
-rw-r--r--. 1 root root 0 3月 11 19:40 file800
示例2:老铁,双击666
修改shell umask值(临时)
示例2:修改shell umask值(临时)
[root@localhost ~]# umask 000
[root@localhost ~]# mkdir dir900
[root@localhost ~]# touch file900
[root@localhost ~]# ll -d dir900 file900
drwxrwxrwx. 2 root root 4096 3月 11 19:44 dir900
-rw-rw-rw-. 1 root root 0 3月 11 19:44 file900