Linux 基础入门 02

Linux 基础入门 02

一、用户&组管理
1.1 usermod

用来修改用户账号的各项设定

-c<备注>  	#修改用户帐号的备注文字。
-d登入目录>    #修改用户登入时的目录。
-e<有效期限> 	#修改帐号的有效期限。
-f<缓冲天数> 	#修改在密码过期后多少天即关闭该帐号。
-g<群组>  	#修改用户所属的群组。
-G<群组>  	#修改用户所属的附加群组。
-l<帐号名称>  #修改用户帐号名称。
-L  		#锁定用户密码,使密码无效。
-s<shell>  	#修改用户登入后所使用的shell。
-u<uid>  	#修改用户ID。
-U  	    #解除密码锁定。

-d 修改用户登入

[root@wentan ~]# su - admin
[admin@wentan ~]$ pwd
/home/admin
[admin@wentan ~]$ usermod admin -d /dwt/
usermod: user admin is currently used by process 2495
[admin@wentan ~]$ exit
logout
[root@wentan ~]# usermod admin -d /dwt/
[root@wentan ~]# su - admin
[admin@wentan ~]$ pwd
/dwt/

-g 修改属组

[root@wentan ~]# usermod admin -g root
[root@wentan ~]# su - admin
[admin@wentan ~]$ id
uid=1001(admin) gid=0(root) groups=0(root),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

-s 修改用户登录后的shell功能

[root@wentan ~]# usermod admin -s /sbin/nologin
[root@wentan ~]# su - admin
This account is currently not available.
1.2 groupmod

-n 改名为new_group

-g 改名 ID 改为 GID

[root@wentan ~]# groupmod admins -n admin123
[root@wentan ~]# groupmod admin123 -n admins
1.3 userdel&groupdel

用户和组的删除

-f 删除用户家目录及邮箱 目录名还在

[root@wentan ~]# useradd laowang
[root@wentan ~]# userdel laowang
[root@wentan ~]# cd /home/
[root@wentan home]# ls
admin  laowang  wentan
[root@wentan home]# useradd laowang
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists
[root@wentan home]# userdel -f laowang
[root@wentan home]# ls
admin  laowang  wentan
[root@wentan home]# ll
total 4
drwx------.  3 admin  wentan   99 Jan 10 09:42 admin
drwx------.  3   1002   1003   78 Jan 10 10:00 laowang
drwx------. 15 wentan wentan 4096 Jan  8 19:21 wentan
[root@wentan home]# cd laowang/
[root@wentan laowang]# cd /home/
[root@wentan home]# rm -rf laowang
[root@wentan home]# ls
admin  wentan

[root@wentan home]# groupadd laowang
[root@wentan home]# groupdel laowang
1.4 /etc/shadow

用于存储 Linux 系统中用户的密码信息

[root@wentan home]# vi /etc/shadow
root:$6$pL84D17FANiN.6Wn$BWNR.GoMZd9Hgrxy.HwTcqOWnW5zb.RpbyNmfepe4V8W5pS3RrXfJshn9zyZzD7TAMB2m7PRWsgys6qKGVEiu.::0:99999:7:::
bin:*:17784:0:99999:7:::
daemon:*:17784:0:99999:7:::
···
"/etc/shadow" [readonly] 46L, 1336C
#字段信息
root:$6$pL84D17FANiN.6Wn$BWNR.GoMZd9Hgrxy.HwTcqOWnW5zb.RpbyNmfepe4V8W5pS3RrXfJshn9zyZzD7TAMB2m7PRWsgys6qKGVEiu.::0:99999:7:::
字段1:用户账户名称
字段2:加密秘钥字符串信息一般都是sha或md5加密
字段3:最近一次修改密码的时间,表示从1970.1.1到至今的天数
字段4:密码的最短使用天数
字段5:密码最长有效天数默认99999
字段6:密码即将到期警告天数默认7
字段7:在密码过期后账号保持活跃的天数,指天数之后账号被锁定,无效,表示从1970.1.1到至今的天数
字段8:账号失效时间,默认也是为空
1.5 chage

配置密码相关信息

-m 	最小期限
-M 	最大期限
-W 	警告周期
-I 	失效周期
-d 	强制要求在下一次登录时更新密码
-l 	显示秘密信息
-E 	用户将于2020-12-30到期(XXXX-DD-YY)

实例

[root@wentan home]# useradd test
[root@wentan home]# passwd test
Changing password for user test.
New password: 123
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 123
passwd: all authentication tokens updated successfully.
[root@wentan home]# chage -m 0 -M 30 -W 7 -I 14 test
#查看
[root@wentan home]# vi /etc/shadow
test:$6$5zYgz4aIyGWUK8.P$X1ucmgCzqzXLdmr2qkm36TN3WBBTYhjQcG0Bjio59oPhcFZnO2s2Rlsd.fdAzohBvBJYC1/gs/155sO9oG0ER1:19002:0:30:7:14::

修改账号到期时间

[root@wentan home]# chage -E 2022-12-30 test
[root@wentan home]# vi /etc/shadow
test:$6$5zYgz4aIyGWUK8.P$X1ucmgCzqzXLdmr2qkm36TN3WBBTYhjQcG0Bjio59oPhcFZnO2s2Rlsd.fdAzohBvBJYC1/gs/155sO9oG0ER1:19002:0:30:7:14:19356:

下次登录时必须修改密码

[root@wentan home]# chage -d 0 test
1.6 /etc/login.defs

这个文件是用来创建用户是进行一定的限制,但是优先级低于/etc/passwd和/etc/shadow

[root@wentan home]# vi   /etc/login.defs  
MAIL_DIR	/var/spool/mail  # 系统消息(邮件)文件夹
PASS_MAX_DAYS	99999        # 密码有效最大天数 
PASS_MIN_DAYS	0            # 密码有效最小天数
PASS_MIN_LEN	5            # 密码长度
PASS_WARN_AGE	7            # 密码失效警告倒计时
UID_MIN                  1000    # 用户UID最小1000
UID_MAX                 60000    # 用户UID最大60000
SYS_UID_MIN               201    # 系统用户UID最小201
SYS_UID_MAX               999    # 系统用户UID最大999
GID_MIN                  1000    # 用户组GID最小1000
GID_MAX                 60000    # 用户组GID最大60000
SYS_GID_MIN               201
SYS_GID_MAX               999
CREATE_HOME	yes             # 创建家目录
UMASK           077          # 创建文件/目录的权限掩码
USERGROUPS_ENAB yes          # 创建用户时同时生成组是  如果此处是no 创建的用户 会是gid=100(users)groups=100(users) 
ENCRYPT_METHOD SHA512        # 加密  方法  sha 512 这个方法生成的密码在/etc/shadow里面的第二列会以$6$开头
二、sudo 命令执行过程

1.当用户执行sudo,系统会主动寻找/etc/sudoers 文件,判断该用户是否有执行sudo权限

2.确认用户具有可执行sudo权限后,让用户输入自己的密码确认

3.若密码输入成功,才可以执行sudo后续命令

[root@wentan ~]# vi /etc/sudoers
2.1 如何赋予用户sudo操作权限

通过useradd添加的用户,并不具备sudo权限。在ubuntu/centos等系统下,需要将用户加入admin组或者 wheel组或者sudo 组。以root 用户身份执行如下命令,将用户加入wheel/admin/sudo组。

usermod -a -G wheel 用户名

2.2 如何完成整个组一次性提权
sudo yum repolist all  #尝试执行一下只有root才能运行的命令
/etc/sudoers    #是可以让整个组 完成sudo提权  且不许要密码
[root@wentan home]# cd /etc/sudoers.d/
[root@wentan sudoers.d]# pwd
/etc/sudoers.d
[root@wentan sudoers.d]# touch admins
[root@wentan sudoers.d]# vim admins
%admins ALL=(ALL)	NOPASSWD:ALL
#官方文档推荐的做法,不是直接修改/etc/sudoers文件,而是将修改写在/etc/sudoers.d/目录下的文件中。
[root@wentan sudoers.d]# ls -l
total 4
-rw-r--r--. 1 root root 31 Jan 10 11:12 admins
三、加密算法 MD5 sha

MD5 加密算法 密码+随机数+id通过md5 算法最后变成128位字符

1.等长原则不管加密内容有多少字符,最后加密输出都是等长的

2.雪崩效应 只要两个文件有一个字符不一样那整个秘钥输出也是完全不同的

3.MD5 不可逆无法通过加密后字符来得出之前原来的密码
md5加密所谓破解完全不是靠逆推,靠撞库

[root@wentan dwt]# vi 123.txt 
123456789
[root@wentan dwt]# vi 321.txt
fdsjs
gsd
g
f
....
[root@wentan dwt]# vi 1234.txt
l23456789

[root@wentan dwt]# md5sum 123.txt 
b2cfa4183267af678ea06c7407d4d6d8  123.txt
[root@wentan dwt]# md5sum 321.txt
08465df9a4e531e2d7a7000417a75915  321.txt
#雪崩效应
[root@wentan dwt]# md5sum 1234.txt 
8515b5f02b20b947ba893974a0d529f0  1234.txt

#撞库
[root@wentan dwt]# md5sum text.txt 
b2cfa4183267af678ea06c7407d4d6d8  text.txt
四、linux的文件权限
4.1 stat

用于显示文件时间和 inode 内容,inode相关的知识会在后面的磁盘管理章节详细讲解,这边主要来看文件的时间

[root@wentan dwt]# stat 123.txt 
  File: 123.txt
  Size: 9         	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 17977359    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:default_t:s0
Access: 2022-01-10 11:26:04.660852699 +0800	#访问时间
Modify: 2022-01-10 11:25:52.111794052 +0800	#修改(文件内容)时间
Change: 2022-01-10 11:25:52.113794061 +0800	#修改(文件状态)时间
 Birth: -
  • Access:访问时间,也叫atime

    • 当文件被访问的时候,这个时间就会发生改变
    • inux文件运行的时候查看文件又频繁数量又大,如果每次atime发生变化的时候都记入硬盘,或造成很大的压力。RHEL6开始relatime,atime延迟修改,必须满足其中一个条件:
      • 自上次atime修改后,已达到86400秒
      • 发生写操作时
  • Modify:修改时间,也叫mtime

    • 文件内容发生变化的时候,这个时间就会发生改变
  • Change:改变时间,也叫ctime

    • 当文件状态被改变的时候,这个时间就会发生修改
4.2 文件权限设置

可以赋予某个用户或组,能以何种方式,访问某个文件

Linux系统是一种典型的多用户系统,不同的用户除以不同的地位,拥有不同的权限。

为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规定。

-		rw-r--r--. 	  1 	 root	 root  	10 		Jan 10 11:27 		text.txt
文件类型	权限		连接数		属主	 属组		大小		时间					文件名

文件类型: - 普通文件 d 目录

每一个文件都会有UID GID 属主 属组

同时这个文件运行时也会带上UID GID 其实这个就是属主ID 属组ID

访问任何一个文件,我们有权限规定:

每个文件,都有3个对象,对三个对象赋予不同权限:

属主、属组、其他人

每个文件都有三个权限:

r	可读
w	可写
x	可执行
例如,对某脚本123.txt	属主root	属组root
对root而言某脚本A这个文件可读可写可以执行
对root这个组而言某脚本A这个文件只可以看和编写不能执行
对其他人而言某脚本A这个文件只可以看
于是这个文件的权限:	rwxrw-r--
三个对象属主属组其他人
字母表示rwxrw-r–
数字表示764

在linux当中 权限可以用数字表示

r=4 w=2 x=1 每个对象需要相加

所以:rw-r–r-- 用数字表示: 644

4.3 chmod

修改 文件权限 ,可以一次性对三个对象 三个权限全都修改

[root@wentan dwt]# chmod 755 321.txt
//属主可读可写可执行   属组  可读可执行   其他人  可读可执行
[root@wentan dwt]# chmod 666 123.txt

-rw-rw-rw-. 1 root root   9 Jan 10 11:25 123.txt
-rwxr-xr-x. 1 root root 136 Jan 10 11:20 321.txt
[root@wentan dwt]# chmod g-x,o-x 123.txt
-rw-rw-rw-. 1 root root   9 Jan 10 11:25 123.tx

[root@wentan dwt]# chmod u+x,o-w text.txt
[root@wentan dwt]# chmod 764 1234.txt 
-rwxr--r--. 1 root root  10 Jan 10 11:27 text.txt
-rwxrw-r--. 1 root root  10 Jan 10 11:30 1234.txt

八进制语法

chmod命令可以使用八进制数来指定权限。文件或目录的权限位是由9个权限位来控制,每三位为一组,它们分别是文件所有者(User)的读、写、执行,用户组(Group)的读、写、执行以及其它用户(Other)的读、写、执行。历史上,文件权限被放在一个比特掩码中,掩码中指定的比特位设为1,用来说明一个类具有相应的优先级。

#权限rwx二进制
7读 + 写 + 执行rwx111
6读 + 写rw-110
5读 + 执行r-x101
4只读r–100
3写 + 执行-wx011
2只写-w-010
1只执行–x001
0000

例如, 765 将这样解释:

  • 所有者的权限用数字表达:属主的那三个权限位的数字加起来的总和。如 rwx ,也就是 4+2+1 ,应该是 7。
  • 用户组的权限用数字表达:属组的那个权限位数字的相加的总和。如 rw- ,也就是 4+2+0 ,应该是 6。
  • 其它用户的权限数字表达:其它用户权限位的数字相加的总和。如 r-x ,也就是 4+0+1 ,应该是 5。
4.4 chown

设置文件的属主和属组

[root@wentan dwt]# chown wentan:wentan text.txt
//修改属主和数组都是wentan
[root@wentan dwt]# chown admin 123.txt
//修改属主为admin
[root@wentan dwt]# chown :wentan 123.txt 
//修改属组为wentan
[root@wentan dwt]# ll
total 16
-rwxrw-r--. 1 root   root    10 Jan 10 11:30 1234.txt
-rw-rw-rw-. 1 admin  wentan   9 Jan 10 11:25 123.txt
-rwxr-xr-x. 1 root   root   136 Jan 10 11:20 321.txt
drwxr-xr-x. 2 root   root    22 Jan  8 15:21 etc
-rwxr--r--. 1 wentan wentan  10 Jan 10 11:27 text.txt
4.5 chgrp

单独修改属组

[root@wentan dwt]# chgrp wentan 321.txt 
[root@wentan dwt]# ll
-rwxr-xr-x. 1 root   wentan 136 Jan 10 11:20 321.txt
4.6 setfacl

可以单独针对某个用户设置权限

[root@wentan dwt]# setfacl -m u:admin:rwx 321.txt 
[root@wentan dwt]# getfacl 321.txt 
# file: 321.txt
# owner: root
# group: wentan
user::rwx
user:admin:rwx
group::r-x
mask::rwx		#最大权限,只针对属组和其他人
other:

#设置文件最大权限,(对属主不生效的)
[root@wentan dwt]# setfacl -m m::rw 321.txt
[root@wentan dwt]# getfacl 321.txt 
# file: 321.txt
# owner: root
# group: wentan
user::rwx
user:admin:rwx			#effective:rw-
group::r-x				#effective:r--
mask::rw-
other::r-x
4.7 mask有效权限

mask 权限,指的是用户或群组能拥有的最大 ACL 权限,也就是说,给用户或群组设定的 ACL 权限不能超过 mask 规定的权限范围,超出部分做无效处理。

4.8 特殊权限
SUID: chmod u+s  #当一个文件被设置了SUID(会在该文件所有者的执行为直接变成s)这样任何人执行这个文件,会直接获取属主的权限。

SGID: chmod g+s  #当一个目录被设置了SGID后 ,任何人在这个目录中创建的文件,都属于这个目录所有者,不能对文件设置其他属主

SBIT: chmod o+t  #假设这个目录里面对象是其他人 针对其他人一旦被打上T这个值

#那么这个目录中其他用户创建的任何文件,只有文件所有者才能删除
2进制十进制
001sbit1
010sgid4
001suid7
#增加特殊权限
[root@wentan dwt]# chmod u+s 123.txt 
[root@wentan dwt]# ll
-rwSrw-rw-. 1 admin  wentan   9 Jan 10 11:25 123.txt

[root@wentan dwt]# chmod g+s etc/
[root@wentan dwt]# ll
drwxr-sr-x. 2 root   root    22 Jan  8 15:21 etc

[root@wentan dwt]# chmod o+t etc/
[root@wentan dwt]# ll
drwxr-sr-t. 2 root   root    22 Jan  8 15:21 etc
#删除特殊权限
[root@wentan dwt]# chmod u-s 123.txt 
[root@wentan dwt]# chmod g-s etc/
[root@wentan dwt]# chmod o-t etc/
[root@wentan dwt]# ll
total 16
-rwxrw-r--. 1 root   root    10 Jan 10 11:30 1234.txt
-rw-rw-rw-. 1 admin  wentan   9 Jan 10 11:25 123.txt
-rwxrw-r-x+ 1 root   wentan 136 Jan 10 11:20 321.txt
drwxr-xr-x. 2 root   root    22 Jan  8 15:21 etc
4.9 umask

指定在建立文件时预设的权限掩码,进程 新建文件、目录的默认权限会收到umask的影响,umask表示要减掉得到权限。

umask可用来设定[权限掩码][权限掩码]是由3个八进制的数字所组成,将现有的存取权限减掉权限掩码后,即可产生建立文件时预设的权限。

[root@wentan dwt]# touch umask.txt
-rw-r--r--. 1 root   root     0 Jan 10 14:23 umask.txt
[root@wentan dwt]# umask
0022		#反掩码

#777-022=755-111=644   拿掉执行权限

#给权限
[root@wentan dwt]# umask 002
[root@wentan dwt]# touch 111.txt
[root@wentan dwt]# ll
total 16
-rw-rw-r--. 1 root   root     0 Jan 10 14:28 111.txt
#当前环境有效,临时修改umask
#永久修改umask
[root@wentan ~]# vim /etc/profile
#umask 下添加代码
if [ $UID -eq 1000 ]; then
        umask 000
fi
[wentan@wentan dwt]$ touch 123.txt
[wentan@wentan dwt]$ ll
total 16
-rw-rw-rw-. 1 admin  wentan   9 Jan 10 14:43 123.txt
4.10 chattr

用于改变文件属性。

这项指令可改变存放在ext2文件系统上的文件或目录属性,这些属性共有以下8种模式:
a:	#让文件或目录仅供附加用途。
b:	#不更新文件或目录的最后存取时间。
c:	#将文件或目录压缩后存放。
d:	#将文件或目录排除在倾倒操作之外。
i:	#不得任意更动文件或目录。
s:	#保密性删除文件或目录。
S:	#即时更新文件或目录。
u:	#预防意外删除。

实例1:用 chattr 防止系统中某个关键文件被修改

[root@wentan dwt]# chattr +i 123.txt 
[root@wentan dwt]# lsattr 123.txt  
----i------------- 123.txt
//查看目前被打的标签 
[root@wentan dwt]# chattr -i 123.txt 

实例2 : 让某个文件只能往里面追加内容,但是不能删除历史内容,适用于企业服务器日志

[root@wentan dwt]# vi /var/log/messages 
[root@wentan dwt]# chattr +a /var/log/messages

#追加
[root@wentan dwt]# echo hello,world >> /var/log/messages

五、文件管理

我们知道Linux的目录结构为树状结构,最顶级的目录为根目录 /。

其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们。

在开始本教程前我们需要先知道什么是绝对路径与相对路径。

绝对路径:是由/开头的 可以使用在任何场景,一定是可以找到该文件的

相对路径:不会以/开头 ./passwd 例如这个就是相对路径

. 当前目录

… 上级目录

5.1 几个常见的处理目录的命令
 ls(英文全拼:list files): 列出目录及文件名
 cd(英文全拼:change directory):切换目录
 pwd(英文全拼:print work directory):显示目前的目录
 mkdir(英文全拼:make directory):创建一个新的目录
 rmdir(英文全拼:remove directory):删除一个空的目录  可以用 rm -rf代替  
 cp(英文全拼:copy file): 复制文件或目录
 rm(英文全拼:remove): 删除文件或目录
 mv(英文全拼:move file): 移动文件与目录,或修改文件与目录的名称    
5.2 文本文件查看工具
  • cat 内容抓取 可以支持同时多内容抓取

  • more 空格下一页,回车下一行

    [root@wentan dwt]# more 321.txt
    
  • less 首先是可以使用命令 /查找内容 如果输入v 就是进入vi模式 可以直接编辑 然后wq保存 保存完会退出到less的界面 需要q才能退出

    [root@wentan dwt]# less 321.txt
    
  • head 用来查看文件头部内容来判断文件

    [root@wentan dwt]# head -n 20 /etc/passwd
    
  • tail 通常用来做日志监控

    [root@wentan dwt]# tail -n 30 /var/log/messages
    
5.3 文本筛选工具

用于查找文件里符合条件的字符串。

[root@wentan dwt]# grep -n hello,world /var/log/messages
20499:hello,world
#grep 	linux 三剑客之一
-i 或 --ignore-case : 忽略字符大小写的差别。
-n 或 --line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。
-o 或 --only-matching : 只显示匹配PATTERN 部分。
-v 或 --invert-match : 显示不包含匹配文本的所有行。
5.4 文本处理工具

用于显示每行从开头算起 num1 到 num2 的文字

[root@wentan dwt]# cut -d : -f 1 /etc/passwd
root
bin
daemon
adm
lp
...
-b :以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志。
-c :以字符为单位进行分割。
-d :自定义分隔符,默认为制表符。
-f :与-d一起使用,指定显示哪个区域。
-n :取消分割多字节字符。仅和 -b 标志一起使用。如果字符的最后一个字节落在由 -b 标志的 List 参数指示的范围之内,该字符将被写出;否则,该字符将被排除
5.5 文本区分工具

用于比较文件的差异

[root@wentan dwt]# cat 123.txt 1234.txt 
123456789
l23456789
[root@wentan dwt]# diff 123.txt 1234.txt 
1c1
< 123456789
---
> l23456789
5.6 文本排序工具

用于将文本文件内容加以排序。

-f 排序时,将小写字母视为大写字母。
-n 依照数值的大小排序。
-u 意味着是唯一的(unique),输出的结果是去完重了的。
-r 以相反的顺序来排序。
[root@wentan dwt]# sort -urf 321.txt
5.7 管道符 |

可以把管道符之前的内容 作为管道符之后内容的输入

[root@wentan dwt]# cat /etc/passwd | grep /bin/bash
root:x:0:0:root:/root:/bin/bash
wentan:x:1000:1000:wentan:/home/wentan:/bin/bash
test:x:1002:1003::/home/test:/bin/bash
[root@wentan dwt]# cat /etc/passwd | grep /bin/bash | cut -d: -f1
root
wentan
test
[root@wentan dwt]# tail -n 100 /var/log/messages | grep system
5.8 正则表达式
^    #代表一个行的开头       ^ro      表示以ro开头   
$    #代表一个行的结尾       login\$     表示以login结尾    
.    #匹配一个任意字符  ro.t     字符串为ro.t   可以匹配   root  rokt  ro0t
*    #标识匹配之前内容的0次或无数次   roo\*t
  • ^ 开头
[root@wentan dwt]# grep ^ro /etc/passwd
root:x:0:0:root:/root:/bin/bash
  • $ 结尾
[root@wentan dwt]# grep login\$ /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
...
  • . 匹配任意字符
[root@wentan dwt]# grep ro.t /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
  • *匹配0次&多次
[root@wentan dwt]# grep ro.t /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@wentan dwt]# grep ro\*t /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
  • \ 转义字符

一般在linux系统当中 \ 会用来转义 $ * 让其变成本身字符意思 而不是shell当中的意思

5.9 文件查找工具

用于查找符合条件的文件,他会去保存文件和目录名称的数据库内,查找合乎范本样式条件的文件或目录

  • locate

    在使用locate之前,需要更新一下数据库,因为locate只会在数据库中查找文件所在的位置,所以locate查找速度极快,缺点就是数据库更新并不是实时的,更新数据库有两种方式:

    (1) 手动更新 updatedb

    (2) 自动更新 默认情况下,updatedb每天执行一次

    [root@wentan ~]# updatedb
    [root@wentan ~]# locate passwd
    /etc/passwd
    /etc/passwd-
    /etc/pam.d/passwd
    /etc/security/opasswd
    /usr/bin/gpasswd
    /usr/bin/grub2-mkpasswd-pbkdf2
    ...
    
    locate
    -c:只输出找到的数量
    -n:至多显示 n个输出
    -i:忽略大小写
    -r:使用基本正则表达式
    -d DBPATH:使用 DBPATH 指定的数据库,而不是默认数据库 /var/lib/mlocate/mlocate.db
    
    #用正则表达式来查找网卡位置
    [root@wentan ~]# locate -r ens160\$
    /etc/sysconfig/network-scripts/ifcfg-ens160
    
  • find

    实时查找工具,通过遍历指定路径下的文件系统完成文件查找

    特点 :

    1.查找速度略慢

    2.精确查找

    3.实时查找

    4.满足多条件匹配

    find [选项] [路径] [查找条件 + 处理动作]
    

    查找路径:指定具体目录路径,默认是当前文件夹

    查找条件:指定的查找标准(文件名/大小/类型/权限等),默认是找出所有文件

    处理动作:对符合条件的文件做什么操作,默认输出屏幕

    #条件组合  
    -a     #多个条件and并列
    -o     #多个条件or并列
    -not    #条件相反
    
    • 按文件名查找

      [root@wentan ~]# find /etc -name "ifcfg-ens160"
      [root@wentan ~]# find /etc -name "ifCfg-ens160"  //忽略大小写
      [root@wentan ~]# find /etc -name "ifcfg*"	//通配符
      /etc/sysconfig/network-scripts/ifcfg-ens160
      
    • 按大小查找

      [root@wentan ~]# find /etc/ -size +5M  //大于5M
      /etc/selinux/targeted/policy/policy.31
      /etc/udev/hwdb.bin
      [root@wentan ~]# find /etc/ -size -5M  //小于5M
      [root@wentan ~]# find /etc/ -size  5M  //等于5M
      ....
      
      [root@wentan ~]# find /etc/ -size +5M -a -name "ifcfg-ens160"   //-a 与
      none
      [root@wentan ~]# find /etc/ -size +5M -o -name "ifcfg-ens160"	//-o 或
      /etc/selinux/targeted/policy/policy.31
      /etc/sysconfig/network-scripts/ifcfg-ens160
      /etc/udev/hwdb.bin
      ...
      
      
    • 按目录深度查找

      #指定查找深度为4  且名字是ifcfg-ens160的
      [root@wentan ~]# find / -maxdepth 4 -a -name "ifcfg-ens160*"
      /etc/sysconfig/network-scripts/ifcfg-ens160
      /var/log/anaconda/ifcfg.log
      /usr/sbin/ifcfg
      /tmp/ifcfg.log
      
    • 按时间查找

      [root@wentan ~]# find /etc/ -mtime +5
      [root@wentan ~]# find /etc/ -mtime  5
      [root@wentan ~]# find /etc/ -mtime -5
      
    • 按文件属主和属组查找

      [root@wentan ~]# find /dwt/ -group root
      /dwt/
      /dwt/etc
      /dwt/etc/password
      /dwt/1234.txt
      /dwt/umask.txt
      /dwt/111.txt
      [root@wentan ~]# find /home -user wentan//属主是xwz的文件
      [root@wentan ~]# find /home -nouser		//没有属主的文件
      [root@wentan ~]# find /home -nogroup	//没有属组的文件
      
      
    • 按文件权限查找

      #查找权限为644的文件
      [root@wentan ~]# find /dwt/ -perm 644 -ls 
       34778829      4 -rw-r--r--   1  root     root         2487 Jan  8 15:21 /dwt/etc/password
       17383188      0 -rw-r--r--   1  root     root            0 Jan 10 14:23 /dwt/umask.txt
      
      #查找权限高于644的文件
      [root@wentan ~]# find /dwt/ -perm -644 -ls  
       17383197      0 drwxr-xr-x   3  root     root          111 Jan 10 15:43 /dwt/
       34778829      4 -rw-r--r--   1  root     root         2487 Jan  8 15:21 /dwt/etc/password
       17383191      4 -rwxrw-r-x   1  root     wentan        211 Jan 10 15:43 /dwt/321.txt
      
      #查找特殊权限SUID  且权限刚好是000  基本查不出东西
      [root@wentan ~]# find / -perm 4000 -ls
      none
      #查找特殊权限SUID   且权限大于000
      [root@wentan ~]# find / -perm -4000 -ls
      all
      
    • 按正则表达式查找

      [root@wentan ~]# find /etc -regex '.*ifcfg-ens[0-9][0-9][0-9]'
      # .*    	任意多个字符
      # [0-9]     任意一个数字
      /etc/sysconfig/network-scripts/ifcfg-ens160
      
    • 处理动作

      #处理动作   
      -print  	#默认  显示至屏幕
      -ls     	#对于查找到的文件 执行  ls -l  命令
      -delete  	#删除查找到的文件   
      - exec     command     #执行其他复合命令
      
      #复制配置文件到/dwt/back/ 下
      [root@wentan dwt]#mkdir back
      [root@wentan dwt]#find /etc/ -maxdepth 4 -name "*.conf" -exec cp -r {} /dwt/back/ \;
      find: missing argument to `-exec'
      [root@wentan back]# ll
      total 1536
      -rw-r--r--. 1 root root   346 Jan 11 11:02 00-keyboard.conf
      -rw-r--r--. 1 root root   690 Jan 11 11:02 05-redhat.conf
      lrwxrwxrwx. 1 root root    55 Jan 11 11:02 10-hinting-slight.conf -> /usr/share/fontconfig/conf.avail/10-hinting-slight.conf
      lrwxrwxrwx. 1 root root    59 Jan 11 11:02 10-scale-bitmap-fonts.conf -> /usr/share/fontconfig/conf.avail/10-scale-bitmap-fonts.conf
      ...
      
      #删除刚刚这些文件
      [root@wentan dwt]# find /dwt/back/ -name "*.conf" -delete
      [root@wentan back]# ll
      total 0
      
      #在目录中查找权限为777的文件,将其他人的x权限删除
      [root@wentan dwt]# find   /dwt/  -perm  777   -exec  chmod  o-x   {}   \;
      [root@wentan dwt]# ll
      total 16
      -rwxrwxrw-. 1 root   root     0 Jan 10 14:28 111.txt
      -rwxrwxrw-. 1 root   root    10 Jan 10 11:30 1234.txt
      
      #清理tmp目录下 修改时间大于7天的  且属主是root的文件
      [root@wentan /]# find /tmp/* -mtime +7 -user root -delete
      

      实例

      #查找/var目录下属主为root,且属组为mail的所有文件或目录
      [root@wentan ~]# find /var -user root -group mail
      /var/spool/mail
      
      #查找/etc目录下最近一周内容曾被修改过的文件或目录
      [root@wentan ~]# find /etc/ ‐mtime ‐7
      
      #查找当前系统上没有属主或属组,且最近一周内曾被访问过的文件或目录
      [root@wentan ~]# find / \(‐nouser ‐o ‐nogroup\) ‐a ‐atime ‐7
      
      #查找/etc目录下至少一类用户没有执行权限的文件
      [root@wentan ~]#  find /etc ‐not ‐perm ‐111
      
      #查找/etc/init.d目录下,所有用户都执行权限,且其它用户写权限的文件
      [root@wentan ~]# find /etc/init.d ‐perm ‐113
      /etc/init.d
      

      欢迎关注微信公众号:问渠清源

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tan-1210

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值