文本处理工具、用户及权限管理

##### 命令tr

“`
选项:
-c 取字符补集
-d 删除字符
-s 把连续重复的字符以单独一个字符表示
-t 将第一个字符集对应字符转化为第二字符集对应的字符
例:
[root@centos6 ~]#tr ‘a-f’ ‘A-Z’
abcdefgh
ABCDEFgh
tr 与tr -t 的区别
[root@centos7 21:34:27 ~]#echo abcdef | tr ‘abcd’ ‘123’
1233ef
[root@centos7 21:35:33 ~]#echo abcdef | tr -t ‘abcd’ ‘123’
123def

“`

“`
例:-d
[root@centos6 ~]#tr -d ‘12345’
13432rdf345tudfihg4t5e5
rdftudfihgte
“`

“`
例:-t
root@centos6 ~]#tr -s ‘abc’
aaaaacccccbbbbbbdddddabcff
acbdddddabcff
“`

“`
例:
Linux文本文件与window文本文件区别
[root@centos7 21:26:24 data]#cat -A win.txt
a^M$
b^M$
c
“^M”表示回车即”\r”
[root@centos7 19:50:48 data]#ll
total 8
-rw-r–r–. 1 root root 7 Apr 2 19:50 linux.txt
-rw-r–r–. 1 root root 7 Apr 2 19:50 win.txt
[root@centos7 19:50:50 data]#hexdump -C linux.txt
00000000 61 0a 62 0a 63 0a 0a |a.b.c..|
00000007
[root@centos7 19:55:12 data]#hexdump -C win.txt
00000000 61 0d 0a 62 0d 0a 63 |a..b..c|
00000007
0a 是十六进制,转换十进制为10,代表换新行
0d 转换十进制为13,代表回车
0d转换为8进制数为15,删除0d。则两个文件相同
[root@centos7 20:05:55 data]#tr -d ‘\15’ < win.txt > win2.txt
[root@centos7 20:06:39 data]#hexdump -C win2.txt
00000000 61 0a 62 0a 63 |a.b.c|
00000005
“`

“`
例:-s压缩并替换
[root@centos7 20:15:11 data]#df > df.txt
[root@centos7 20:15:25 data]#cat df.txt
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 52403200 3211956 49191244 7% /
devtmpfs 608152 0 608152 0% /dev
tmpfs 622872 0 622872 0% /dev/shm
tmpfs 622872 8768 614104 2% /run
tmpfs 622872 0 622872 0% /sys/fs/cgroup
[root@centos7 20:16:35 data]#tr -s ” ” “:” <df.txt
Filesystem:1K-blocks:Used:Available:Use%:Mounted:on
/dev/sda2:52403200:3211956:49191244:7%:/
devtmpfs:608152:0:608152:0%:/dev
tmpfs:622872:0:622872:0%:/dev/shm
tmpfs:622872:8768:614104:2%:/run
“`
##### 多行重定向

“`
使用“<<终止符” ,直到 终止词 位置结束
例:
[root@centos7 20:25:31 data]#cat >file<<EOF
> AB
> AC
> BB
> EOF
“`

“`
例:
[root@centos7 20:26:33 data]#tr ‘a-z’ ‘A-Z’ <<EOF
> dsfergsges
> dersferg
> fhegh
> EOF
DSFERGSGES
DERSFERG
FHEGH
“`
##### 标准输出和错误输出重定向到管道

“`
方法一
[root@centos7 20:36:03 data]#ls /boot /erro 2>&1 | tr ‘a-z’ ‘A-Z’
LS: CANNOT ACCESS /ERRO: NO SUCH FILE OR DIRECTORY
/BOOT:
CONFIG-3.10.0-693.EL7.X86_64
EFI
GRUB
GRUB2
INITRAMFS-0-RESCUE-C5449074FC20419A859118352BA75C58.IMG
INITRAMFS-3.10.0-693.EL7.X86_64.IMG
INITRD-PLYMOUTH.IMG
方法二
[root@centos7 20:37:57 data]#ls /boot /erro |& tr ‘a-z’ ‘A-Z’
LS: CANNOT ACCESS /ERRO: NO SUCH FILE OR DIRECTORY
/BOOT:
CONFIG-3.10.0-693.EL7.X86_64
EFI
GRUB
GRUB2
INITRAMFS-0-RESCUE-C5449074FC20419A859118352BA75C58.IMG
INITRAMFS-3.10.0-693.EL7.X86_64.IMG
“`
##### 求和1+2+3+…+99+100

“`
方法一
[root@centos7 21:07:21 data]#echo {1..100} |tr ” ” “+” |bc
5050
方法二
[root@centos7 21:07:14 data]#seq -s + 1 100 |bc
5050
“`
##### 命令tee

“`
例:
[root@centos6 21:14:11 data]#ls | tee ls.log
linux.txt
lost+found
ls.log
win.txt
“`

“`
例:-a不覆盖原文件内容
[root@centos6 21:16:24 data]#who | tee -a ls.log
root tty1 2018-04-02 19:31 (:0)
root pts/0 2018-04-02 19:31 (:0.0)
root pts/2 2018-04-02 21:13 (192.168.30.1:S.0)
[root@centos6 21:18:19 data]#cat ls.log
linux.txt
lost+found
ls.log
win.txt
root tty1 2018-04-02 19:31 (:0)
root pts/0 2018-04-02 19:31 (:0.0)
root pts/2 2018-04-02 21:13 (192.168.30.1:S.0)
“`
##### 命令lsb_release(查看操作系统版本信息)

“`
例:
[root@centos6 data]#lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description: CentOS release 6.9 (Final)
Release: 6.9
Codename: Final
“`
##### 创建由随机小写字母和数字组成的文件

“`
[root@centos7 ~]#touch `openssl rand -base64 12 | tr -dc ‘[:alnum:]’ | tr ‘A-Z’ ‘a-z’
`.log
“`
##### 命令pwunconv和pwconv 老版本与旧版本系统转化,
例:

“`
[root@centos7 ~]#pwunconv
[root@centos7 ~]#cat /etc/passwd
root:$6$.1JNtrB7XUhLXav2$fpsnJcPdZliFSwieeFEEy7een26wSipFobdIsjwqWeCg8k7aqcKJYO33ugeWWMHCQMQrCi7S5T.3nZA7WSmac1:0:0:root:/root:/bin/bash
bin:*:1:1:bin:/bin:/sbin/nologin
daemon:*:2:2:daemon:/sbin:/sbin/nologin
“`
##### 命令chfn 修改用户描述信息

“`
例:
[root@centos7 ~]#chfn li
Changing finger information for li.
Name []: li
Office []: it
Office Phone []: 1234
Home Phone []: 3453254
“`
##### 命令chsh 修改用户shell类型

“`
例: -s指定shell类型
[root@centos7 ~]#chsh -s /bin/csh li
Changing shell for li.
Shell changed.
“`
##### 文件/etc/shadow

“`
li:$6$yyPMNAJI$x84VZGYuU8aczAtZDVUQKfxLWGpBklDKJ8VXtavfN2ELk0Gg3WrlnXnAP4d.OyPP3IJpTA4BGlu9N3wZ4Uv/v1:17624:0:99999:7:::

li:用户名

$6$yyPMNAJI$x84VZGYuU8aczAtZDVUQKfxLWGpBklDKJ8VXtavfN2ELk0Gg3WrlnXnAP4d.OyPP3IJpTA4BGlu9N3wZ4Uv/v1:随机密码,$6代表算法,$yyPMNAJI$代表salt

17624:最近一次修改密码时间,如果此处为0,则表示,下次登录需要修改密码

3: 最短口令有效期,此处代表至少需要等三天后才能修改口令,如果为零,用户可以随时更改口令

99999:密码最长使用期限

7:提前7天提醒

::密码使用宽限期 ,一旦过了宽限期,账号直接锁定

::账户有效期,与密码无关

::保留字段
“`
##### 更改加密算法

“`
authconfig –passalgo=sha256 –updata
例:
[root@centos7 ~]#authconfig –passalgo=sha256 –update
[root@centos7 ~]#getent shadow haha
haha:$5$xmDRRl5Z$DVAOyTLjq9VvP1TcwA9Zl36upW2.Srjn1EOBZKi8zfB:17624:0:99999:7:::
###也可以在配置文件中直接修改/etc/login.defs
“`
##### 命令passwd和chage 让密码立即失效

“`
passwd -e username
chage -d 0 username

chage -l username 查看用户口令情况
chage username 交互式更改用户/etc/shadow 相关信息
“`
##### 配置文件/etc/default/useradd

“`
[root@centos7 mail]#cat /etc/default/useradd
GROUP=100 如果用户不创建主组,则自动放入到users组
HOME=/home
INACTIVE=-1 账户口令宽限期
EXPIRE= 账户的有效期
SHELL=/bin/bash
SKEL=/etc/skel 用户模板文件
CREATE_MAIL_SPOOL=yes 自动创建邮箱
“`
##### 用户、组

“`
在Linux中,普通用户也可以把自己加入到组中,前提是需要知道组的密码,但最好不要设置密码,普通用户加入到该组能获得相应的权限,造成安全隐患
查看用户所在组id和groups
[root@centos7 ~]#groups haha
haha : haha wang
[root@centos7 ~]#id haha
uid=1002(haha) gid=1002(haha) groups=1002(haha),1003(wang)

在root用户下:执行id和id -root
id -root直接搜索/etc/passwd,shadow,group,gshadow 等几个文件,而id看到的未必是最新信息,有些用户更改信息,需要重新登录获取新的口令
“`
##### /etc/group和/etc/gshadow

“`
cat /etc/gshadow
hehe:!!::hehe
第三字段:表示管理员,能调整组成员关系,
第四字段: 表示组成员,里面成员必须和/etc/group配置中同步,否则可能造成系统问题,所有最好用命令修改,而不进入文件中编辑。
“`
##### 命令gpasswd 给组设置密码

“`
[root@centos7 ~]#gpasswd haha
Changing the password for group haha
New Password:
Re-enter new password:
[root@centos7 ~]#cat /etc/gshadow
haha:$6$hkb.1/GbYpuDSDoI$g/WH/Gwh2E4DeN8iGmIOsIl2URkYewcbk2gko1ra0FBxqE838FPW4Z/3jGfJgUZtYmVeAU8EoloaXVZ8
“`
##### 命令newgrp临时切换主组

“`
[hehe@centos7 ~]$newgrp wang #把当前用户加入到wang组
Password:
[hehe@centos7 ~]$id
uid=1000(hehe) gid=1003(wang) groups=1003(wang),1000(hehe) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
“`
##### 命令vipw和vigr pwck和grck
“`
vipw=vi /etc/passwd #vipw和vigr有语法检查功能
pwck 对/etc/passwd 进行格式检查
grck 对/etc/group 进行格式检查
“`
##### 命令useradd

“`
useradd [options] LOGIN
-u UID
-o 配合-u 选项,不检查UID的唯一性
-g GID:指明用户所属基本组,可为组名,也可以GID
-c “COMMENT”:用户的注释信息
-d HOME_DIR: 以指定的路径(不存在)为家目录
-s SHELL: 指明用户的默认shell程序
可用列表在/etc/shells文件中
-G GROUP1[,GROUP2,…]:为用户指明附加组,组须事先存在
-N 不创建私用组做主组,使用users组做主组
-r: 创建系统用户 CentOS 6: ID<500,CentOS 7: ID<1000
-m 创建家目录,用于系统用户
-M 不创建家目录,用于非系统用户
例:创建系统用户
useradd -s /sbin/nologin -r nginx #-r默认不创建家目录和邮箱,加上-m则可创建

默认值设定:/etc/default/useradd文件中
显示或更改默认设置
useradd -D
useradd –D -s SHELL
useradd –D –b BASE_DIR
useradd –D –g GROUP
“`
##### 批量创建用户、批量改口令

“`
newusers user.txt #user.txt文件格式和/etc/passwd格式一样
cat pass.txt | chpasswd #pass.txt密码文件格式为:
user1:12345
user2:12345
user3:12345
“`
##### 命令usermod

“`
-u UID: 新UID
-g GID: 新主组
-G GROUP1[,GROUP2,…[,GROUPN]]]:新附加组,原来的附加组将会被覆盖;
若保留原有,则要同时使用-a选项
-s SHELL:新的默认SHELL
-c ‘COMMENT’:新的注释信息
-d HOME: 新家目录不会自动创建;若要创建新家目录并移动原家数据,同时使
用-m选项
-l login_name: 新的名字;
-L: lock指定用户,在/etc/shadow 密码栏的增加 !
-U: unlock指定用户,将 /etc/shadow 密码栏的 ! 拿掉
-e YYYY-MM-DD: 指明用户账号过期日期
-f INACTIVE: 设定非活动期限
“`

“`
例:-G 修改附加组
[root@centos7 home]#usermod -G li haha
[root@centos7 home]#id haha
uid=1002(haha) gid=1002(haha) groups=1002(haha),1001(li)
[root@centos7 home]#usermod -G xi haha
[root@centos7 home]#id haha
uid=1002(haha) gid=1002(haha) groups=1002(haha),2001(xi)
指定haha的附加组分别为li和xi,但xi把li覆盖了,需要在-a选项,
[root@centos7 home]#usermod -aG li haha
[root@centos7 home]#id haha
uid=1002(haha) gid=1002(haha) groups=1002(haha),1001(li),2001(xi)

如果使haha不加入任何附加组,则:
[root@centos7 home]#usermod -G “” haha
[root@centos7 home]#id haha
uid=1002(haha) gid=1002(haha) groups=1002(haha)
或者[root@centos7 home]#usermod -G haha haha
“`
##### 命令id

“`
id [OPTION]… [USER]
-u: 显示UID
-g: 显示GID
-G: 显示用户所属的组的ID
-n: 显示名称,需配合ugG使用
“`
##### 命令passwd

“`
passwd [OPTIONS] UserName: 修改指定用户的密码
常用选项:
-d:删除指定用户密码
-l:锁定指定用户
-u:解锁指定用户
-e:强制用户下次登录修改密码
-f: 强制操作
-n mindays: 指定最短使用期限
-x maxdays:最大使用期限
-w warndays:提前多少天开始警告
-i inactivedays:非活动期限
–stdin:从标准输入接收用户密码
echo “PASSWORD” | passwd –stdin USERNAME
“`
##### 命令gpasswd

“`
更改组密码
组密码:gpasswd
gpasswd [OPTION] GROUP
-a user 将user添加至指定组中
-d user 从指定组中移除用户user
-A user1,user2,… 设置有管理权限的用户列表
例:-a
[root@centos7 data]#gpasswd -a xi haha
Adding user xi to group haha
[root@centos7 data]#gpasswd -a li haha
Adding user li to group haha

例:-d
root@centos7 data]#gpasswd -d xi haha
Removing user xi from group haha
[root@centos7 data]#gpasswd -d li haha
Removing user li from group haha
例:-A
[root@centos7 data]#gpasswd -A li haha
[root@centos7 data]#cat /etc/gshadow
haha:$6$hkb.1/GbYpuDSDoI$g/WH/Gwh2E4DeN8iGmIOsIl2URkYewcbk2gko1ra0FBxqE838FPW4Z/3jGfJgUZtYmVeAU8EoloaXVZ8VDLod0:li:
“`
##### 命令groupmems 更改查看组成员

“`
options:
-g, –group groupname 更改为指定组 (只有root)
Actions:
-a, –add username 指定用户加入组
-d, –delete username 从组中删除用户
-p, –purge 从组中清除所有成员
-l, –list 显示组成员列表
“`
“`
例:-a
[root@centos7 data]#groupmems -a xi -g haha
[root@centos7 data]#groupmems -a li -g haha
“`
“`
例:-d
root@centos7 data]#groupmems -d xi -g haha
[root@centos7 data]#groupmems -d li -g haha
“`
“`
例:-p
[root@centos7 data]#groupmems -g haha -p
“`
“`
例:-l
[root@centos7 data]#groupmems -g haha -l
“`
##### 修改和删除组

“`
组属性修改:groupmod
groupmod [OPTION]… group
-n group_name: 新名字
-g GID: 新的GID

组删除:groupdel
groupdel GROUP
“`
##### 创建组
“`
roupadd [OPTION]… group_name
-g GID: 指明GID号;[GID_MIN, GID_MAX]
-r: 创建系统组
CentOS 6: ID<500
CentOS 7: ID<1000
“`
##### 修改用户密码策略
ch
“`
age [OPTION]… LOGIN
-d LAST_DAY
-E –expiredate EXPIRE_DATE
-I –inactive INACTIVE
-m –mindays MIN_DAYS
-M –maxdays MAX_DAYS
-W –warndays WARN_DAYS
–l 显示密码策略
示例:
chage -d 0 tom 下一次登录强制重设密码
chage -m 0 –M 42 –W 14 –I 7 tom
chage -E 2016-09-10 tom
“`
#### 权限
##### 普通用户更改自己文件的所有组

“`
[root@centos6 ~]#groupadd g1
[root@centos6 ~]#groupadd g2
[root@centos6 ~]#usermod -aG g1 wang
[root@centos6 ~]#usermod -aG g2 wang
[root@centos6 ~]#groups wang
wang : wang g1 g2
root@centos6 data]#touch file
[root@centos6 data]#ll
total 32
-rw-r–r–. 1 root root 0 Apr 4 19:27 file
[root@centos6 data]#chown wang.li file
[root@centos6 data]#ll
total 32
-rw-r–r–. 1 wang li 0 Apr 4 19:27 file
[root@centos6 ~]#su – wang
[wang@centos6 data]$chown li file
chown: changing ownership of `file’: Operation not permitted
[wang@centos6 data]$chgrp bin file
chgrp: changing group of `file’: Operation not permitted
[wang@centos6 data]$chgrp g1 file
[wang@centos6 data]$chgrp g2 file
wang@centos6 data]$ll
total 32
drwxr-sr-x. 2 root webs 4096 Apr 3 16:34 dir
-rw-r–r–. 1 wang g2 0 Apr 4 19:27 file
普通用户想要更改文件的所属组为g1,g2,前提是这普通用户是g1,g2的组成员。
“`
##### 普通用户能通过chmod更改自己文件的权限

“`
wang@centos6 data]$chmod o= file
[wang@centos6 data]$ll
total 32
drwxr-sr-x. 2 root webs 4096 Apr 3 16:34 dir
-rw-r—–. 1 wang g2 0 Apr 4 19:27 file
“`
##### 读、写权限对root起不到约束作用,执行权限则能暂时约束

“`
[root@centos6 data]#ll /etc/shadow
———-. 1 root root 1150 Apr 4 19:53 /etc/shadow
[root@centos6 data]#cat /etc/shadow 可读
root@centos6 data]#echo aaa>>/etc/shadow 可写
[root@centos6 data]#ll /bin/cat
-rwxr-xr-x. 1 root root 48568 Mar 23 2017 /bin/cat
[root@centos6 data]#chmod -x /bin/cat
[root@centos6 data]#cat /etc/shadow
-bash: /bin/cat: Permission denied
“`
##### 给文件设置只写权限

“`
用户对该文件,不能cat读文件,但能将进入文件编辑,追加内容,甚至清空
“`
##### 读权限对于二进制文件不受影响

“`
[root@centos7 data]#chmod o-r /bin/cat
[root@centos7 data]#su – wang
[wang@centos7 ~]$cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
wang@centos7 ~]$file /bin/cat 不能读意味不能进入查看文件类型
/bin/cat: executable, regular file, no read permission
“`
##### 普通用户目录对只有执行权限

“`
[root@centos6 data]#ll
total 36
drwxr-xr-x. 2 root webs 4096 Apr 3 16:34 dir
[root@centos6 data]#chmod o-r dir
[root@centos6 data]#su – wang
[wang@centos6 data]$ls dir
ls: cannot open directory dir: Permission denied
[wang@centos6 data]$cat dir/aa
1234
[wang@centos6 ~]$cd /data/dir
用户不能列出目录的内容,如果事先知道目录中的文件名,可以查看,可以cd到目录。
“`
##### 普通用户对目录只有读权限
[
“`
root@centos6 data]#chmod o=r dir
[root@centos6 data]#ll
total 36
drwxr-xr–. 2 root webs 4096 Apr 3 16:34 dir
[root@centos6 data]#su – wang
[wang@centos6 data]$cd dir
-bash: cd: dir: Permission denied
[wang@centos6 data]$ls dir
ls: cannot access dir/aa: Permission denied
aa
[wang@centos6 data]$ll dir
ls: cannot access dir/aa: Permission denied
total 0
-????????? ? ? ? ? ? aa
[wang@centos6 data]$cat dir/aa
cat: dir/aa: Permission denied
普通用户不能cd进入目录,不能查看文件的属性,不能查看文件中的内容
,对于目录来说执行权限为目录的基本权限
“`
##### 用户对目录只有写权限

“`
[root@centos6 data]#chmod o=w dir
[root@centos6 data]#su – wang
[wang@centos6 data]$ll
total 36
drwxr-x-w-. 2 root webs 4096 Apr 3 16:34 dir
-rw-r—w-. 1 wang g2 24 Apr 4 20:01 file
-rw-r–r–. 1 root root 7 Apr 2 19:48 linux.txt
drwx——. 2 root root 16384 Mar 27 17:15 lost+found
-rw-r–r–. 1 root root 184 Apr 2 21:18 ls.log
-rw-r–r–. 1 root root 7 Apr 2 19:47 win.txt
[wang@centos6 data]$rm dir/aa
rm: cannot remove `dir/aa’: Permission denied

用户对目录目录只有写权限,用户不能删除目录中的文件,需要加执行权限才能删除
“`
##### 命令chmod

“`
例:–reference
[root@centos6 data]#ll
total 36
-rw-r—w-. 1 wang g2 24 Apr 4 20:01 file
-rw-r–r–. 1 root root 0 Apr 5 09:18 file1
-rw-r–r–. 1 root root 0 Apr 5 09:18 file2
[root@centos6 data]#chmod –reference=file file1 file2
[root@centos6 data]#ll
total 36
-rw-r—w-. 1 wang g2 24 Apr 4 20:01 file
-rw-r—w-. 1 root root 0 Apr 5 09:18 file1
-rw-r—w-. 1 root root 0 Apr 5 09:18 file2
“`
“`
例:-R递归设置权限
chmod -R +X dir2, X只针对目录增加执行权限,文件不增加执行权限,但是如果文件中无论哪个权限位含有执行权限,则会给所有权限位都加上执行权限。
chmod -R 和chown -R /* 递归的危险性,这样设置权限造成系统所有文件权限被更改。
“`
##### 只给目录执行权限,里面的二进制文件可以执行

“`
[root@centos6 dir]#ll -d
drwxr-x-wx. 2 root webs 4096 Apr 5 09:53 .
[root@centos6 data]#ll
total 32
d–x–x–x. 2 root webs 4096 Apr 5 09:53 dir
[root@centos6 data]#cp /bin/cat dir
[root@centos6 data]#su – wang
[wang@centos6 ~]$/data/dir/cat /etc/issue
CentOS release 6.9 (Final)
Kernel \r on an \m
“`
##### 命令umask 设置默认权限

“`
功能:取消对应的权限
对于目录权限=777-umask
对于文件权限=666-umask 如果所得结果权限位为奇数,则+1,偶数不变
umask -p >> .bashrc 把默认权限写入配置文件
例:-S模式方式显示
[wang@centos6 ~]$umask -S
u=rwx,g=rwx,o=rx
“`
##### 文件特殊权限

“`
suid:作用在二进制文件上,继承二进制文件所有者的权限。
例:
[root@centos6 dir]#ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 30768 Nov 24 2015 /usr/bin/passwd
设置suid权限:chmod u+s file或者chmod 4644 file
“`

“`
sgid:作用在二进制文件上,继承二进制文件所有组的权限
作用在目录上,则在该目录下创建新文件则自动继承目录所属组的权限
例:
[root@centos6 data]#ll
total 32
d–x–x–x. 2 root webs 4096 Apr 5 10:33 dir
[root@centos6 data]#chmod g+s dir
[root@centos6 data]#touch dir/aa
[root@centos6 data]#cd dir
[root@centos6 dir]#ll
total 48
-rw-rw-rw-. 1 root webs 0 Apr 5 10:54 aa
“`
“`
sticky:粘滞位作用在目录上,则在该目录下创建的新文件只能文件所有者能删除
设置sitcky权限:chmod o+t dir 或者chmod 1755 dir
[root@centos6 dir]#ll -d /tmp
drwxrwxrwt. 21 root root 4096 Apr 5 10:42 /tmp
“`
##### 命令chattr 可防止root误操作

“`
+i 不能删除、改名、修改 -i解锁
+a 只能追加内容,不能修改 -a解锁
+A 锁定文件atime。 -A解锁
lsattr 显示特定属性
“`
##### ACL访问控制列表

“`
在centos6上,刚装系统,所分的区默认支持acl权限,如果使后来添加的分区,则需要手动区分添加这个功能,在centos7上,默认都支持。
“`
“`
setfacl -m u:li:0 file 设置acl权限
getfacl file 查看acl权限
setfacl -x g:g1 file 删除权限
setfacl -b file 清空acl所有权限
setfacl -d:u:wang:rx dir 给dir目录设置默认acl权限
例:给wang用户设置0权限
[root@centos6 data]#ll
total 4
-rw-rw-rw-. 1 root root 6 Apr 5 11:39 file
[root@centos6 data]#setfacl -m u:wang:0 file
[root@centos6 data]#su wang
[wang@centos6 data]$cat file
cat: file: Permission denied
“`

“`
例:判断acl权限匹配顺序,把wang用户设置0权限,把他加入到的组设置rw权限
[root@centos6 data]#ll
total 4
-rw-rw-rw-. 1 root root 6 Apr 5 11:39 file
[root@centos6 data]#setfacl -m u:wang:0 file
[root@centos6 data]#groupmems -a wang -g g1
root@centos6 data]#setfacl -m g:g1:rw file
[root@centos6 data]#getfacl file
# file: file
# owner: root
# group: g1
user::rw-
user:wang:—
group::rw-
group:g1:rw-
mask::rw-
other::rw-
[root@centos6 data]#su wang
[wang@centos6 data]$cat file
cat: file: Permission denied
wang用户权限为0,但wang用户所在的g1组有rw权限,但wang用户还是不能读取文件,以上实验说,用户匹配权限是从上到下匹配,一旦匹配第一个,就不往下匹配。
“`

“`
例:用户acl权限的累加
[root@centos6 data]#groupmems -a li -g g1
[root@centos6 data]#groupmems -a li -g g2
[root@centos6 data]#id li
uid=506(li) gid=509(li) groups=509(li),507(g1),508(g2)
root@centos6 data]#setfacl -m g:g1:x file
root@centos6 data]#setfacl -m g:g2:x file
[root@centos6 data]#getfacl file
# file: file
# owner: root
# group: root
user::rw-
user:wang:—
group::rw-
group:g1:rw-
group:g2:–x
mask::rwx
other::rw-
[li@centos6 data]$cat file
[li@centos6 data]$echo hostname >>file
[li@centos6 data]$/data/file
bash: /data/file: Permission denied
[li@centos6 data]$getfacl file
# file: file
# owner: root
# group: root
user::rw-
group::r–
group:g1:rw-
group:g2:rw-
mask::rw-
other::r–
li@centos6 data]$cat file
[li@centos6 data]$echo hostname >>file
[li@centos6 data]$/data/file
centos6
g1对文件有读写权限,g2对文件有执行权限,两者累加,最后li用户对文件的权限为rwx
“`

“`
mask权限=文件所属主的权限。mask权限能约acl列表中除了user、other以外的所有用户,等于给他们设置权限的限制,他们的权限不能超过mask权限
[root@centos6 data]#getfacl file
# file: file
# owner: root
# group: root
user::rw-
group::r–
group:g1:rw-
group:g2:–x
mask::rwx
other::r–

[root@centos6 data]#setfacl -m mask::r file
[root@centos6 data]#getfacl file
# file: file
# owner: root
# group: root
user::rw-
group::r–
group:g1:rw- #effective:r–
group:g2:–x #effective:—
mask::r–
other::r–
[root@centos6 data]#ll
total 8
-rw-r–r–+ 1 root root 9 Apr 5 12:16 file
“`

“`
批量设置acl权限
[root@centos6 data]#cat >acl.txt
u:wang:r
g:g1:rw
g:g2:x
[root@centos6 data]#setfacl -M acl.txt file
[root@centos6 data]#getfacl file
# file: file
# owner: root
# group: root
user::rw-
user:wang:r–
group::r–
group:g1:rw-
group:g2:–x
mask::rwx
other::r–
同理,setfacl -X acl.txt 则是批量删除
“`

##### 文本处理工具
##### 命令cat

“`
-E显示行结束符$
“`
“`
例:-n
[root@centos6 data]#cat -n acl.txt
1 u:wang:r
2
3 g:g1:rw
4
5 g:g2:x
例:-b
[root@centos6 data]#cat -b acl.txt
1 u:wang:r

2 g:g1:rw

3 g:g2:x
例:-s 表示压缩空行
[root@centos6 data]#cat -n acl.txt
1 u:wang:r
2
3
4
5 g:g1:rw
6
7
8
9
10
11
12 g:g2:x
[root@centos6 data]#cat -sn acl.txt
1 u:wang:r
2
3 g:g1:rw
4
5 g:g2:x
“`

“`
例:-A 显示所有控制符
[root@centos6 data]#cat -A acl.txt
u:wang:r$ $表示回车
$
$
$
g:g1:rw$
$
$
$
$
$
$
g:g2:x$
“`
##### 命令rev

“`
[root@centos6 data]#echo {1..10} >file
[root@centos6 data]#rev file
01 9 8 7 6 5 4 3 2 1
“`
##### 命令head

“`
-cN取前N个字符
[root@centos6 data]#openssl rand -base64 100 |tr -dc ‘[:alnum:]’ |head -c10
uTmlhkqsqo
“`
##### 命令tail

“`
-f 跟踪文件内容的变化,常用于日志的监控
-F 跟踪文件名,如果文件名被删除,则立刻显示对应提示
“`

“`
-c 取后面的字符
[root@centos6 data]#cat file
1 2 3 4 5 6 7 8 9 10
[root@centos6 data]#tail -c4 file
10
10前面的空格和后面的换行符都算一个字符
“`
##### 命令cut

“`
cut [OPTION]… [FILE]…
-d DELIMITER: 指明分隔符,默认tab
-f FILEDS:
#: 第#个字段
#,#[,#]:离散的多个字段,例如1,3,6
#-#:连续的多个字段, 例如1-6
混合使用:1-3,7
-c 按字符切割
–output-delimiter=STRING指定输出分隔符
例:-c
[root@centos6 data]#df |cut -c44-46
Use
8
1
4
1
100
[root@centos6 data]#df |tr -s ‘ ‘ ‘%’| cut -d% -f5
Use
8
1
4
1
100
“`
“`
例:–output-delimiter=STRING指定输出分隔符
[root@centos6 data]#cut -d: -f1,3 –output-delimiter=**** /etc/passwd
root****0
bin****1
daemon****2
adm****3
lp****4
“`
##### 命令paste

“`
paste 合并两个文件同行号的列到一行
paste [OPTION]… [FILE]…
-d 分隔符:指定分隔符,默认用TAB
-s : 所有行合成一行显示
paste f1 f2
paste -s f1 f2
“`
例:

“`
[root@centos7 ~]#seq 11 20 >file2
[root@centos7 ~]#paste file1 file2
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20
[root@centos7 ~]#paste -s file1 file2
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
“`
#### 命令wc

“`
收集文本统计数据wc
计数单词总数、行总数、字节总数和字符总数
可以对文件或STDIN中的数据运行
wc story.txt
39 237 1901 story.txt
行数 字数 字节数
常用选项
-l 只计数行数
-w 只计数单词总数
-c 只计数字节总数
-m 只计数字符总数
-L 显示文件中最长行的长度
“`
##### 命令sort

“`
文本排序sort
把整理过的文本显示在STDOUT,不改变原始文件
sort [options] file(s)
常用选项
-r 执行反方向(由上至下)整理
-R 随机排序
-n 执行按数字大小整理
-f 选项忽略(fold)字符串中的字符大小写
-u 选项(独特,unique)删除输出中的重复行
-t c 选项使用c做为字段界定符
-k X 选项按照使用c字符分隔的X列来整理能够使用多次
“`

“`
例:
[root@centos7 data]#cat /etc/passwd | cut -d: -f1,3 |sort -t: -k2 -nr



rpc:32
rpcuser:29
ftp:14
games:12
operator:11
mail:8
halt:7
shutdown:6
sync:5
lp:4
adm:3
daemon:2
bin:1
root:0
“`

“`
例:-R
[root@centos7 data]#seq 100 |sort -R |head -n1
3 ###100以内生成随机数
“`
##### 命令uniq

“`
uniq命令:从输入中删除前后相接的重复的行
uniq [OPTION]… [FILE]…
-c: 显示每行重复出现的次数
-d: 仅显示重复过的行
-u: 仅显示不曾重复的行
注:连续且完全相同方为重复
常和sort 命令一起配合使用:
sort userlist.txt | uniq -c
“`
##### 统计访问网站次数最多的前10名ip

“`
[root@centos6 data]#cat access_log |cut -d ‘ ‘ -f1 | sort | uniq -c |sort -nr |head -n10
541 172.20.101.141
313 172.20.101.210
163 172.20.101.108
44 172.20.101.137
37 172.20.101.145
21 172.20.101.243
19 172.20.101.238
15 172.20.0.1
14 172.20.101.158
13 172.20.102.195
“`
##### 命令diff

“`
比较文件
比较两个文件之间的区别
diff foo.conf foo2.conf
5c5
< use_widgets = no

> use_widgets = yes
注明第5行有区别(改变)
“`

“`
diff 命令的输出被保存在一种叫做“补丁”的文件中
使用 -u 选项来输出“统一的(unified)”diff格式文件,最适用于补丁文件
patch 复制在其它文件中进行的改变(要谨慎使用)
适用 -b 选项来自动备份改变了的文件
$ diff -u foo.conf foo2.conf > foo.patch
$ patch -b foo.conf foo.patch
“`

“`
例:-u
[root@centos7 data]#diff -u fstab1 fstab2
[root@centos7 data]#diff -u fstab1 fstab2
— fstab1 2018-04-06 01:32:31.774029484 +0800
+++ fstab2 2018-04-06 01:06:09.022970781 +0800
@@ -6,7 +6,7 @@
# Accessible filesystems, by reference, are maintained under ‘/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
-UUID=d43f1144-5990-41-4891ee6c2300 / xfs defaults 0 0
+UUID=d43f1144-5990-413a-8db7-4891ee6c2300 / xfs defaults 0 0
UUID=e89222b2-322a-4ead-87dc-fb8a1d5cbe3d /boot xfs defaults 0 0
UUID=ca82caba-3048-46a3-a3cc-d2386c8b873e /data xfs defaults 0 0
-UUID=476fb1e2-8b60-4eaaaaaaaaa34-88d4-078716d95f32 swap swap defaults 0 0
+UUID=476fb1e2-8b60-4e34-88d4-078716d95f32 swap swap defaults 0 0
[root@centos7 data]#diff -u fstab1 fstab2 >diff.log
[root@centos7 data]#\rm fstab1
[root@centos7 data]#patch -b fstab2 diff.log
[root@centos7 data]#patch -b fstab2 diff.log
patching file fstab2
Reversed (or previously applied) patch detected! Assume -R? [n] y
[root@centos7 data]#ls
diff.log fstab2 fstab2.orig
“`
##### 正则表达式

“`
grep:
选项
–color=auto: 对匹配到的文本着色显示
-v: 显示不被pattern匹配到的行
-i: 忽略字符大小写
-n:显示匹配的行号
-c: 统计匹配的行数
-o: 仅显示匹配到的字符串
-q: 静默模式,不输出任何信息
-A #: after, 后#行
-B #: before, 前#行
-C #:context, 前后各#行
e:实现多个选项间的逻辑or关系
grep –e ‘cat ’ -e ‘dog’ file
-w:匹配整个单词
-f: 匹配文件列表
-E:使用ERE
-F:相当于fgrep,不支持正则表达式

例:-o
[root@centos6 ~]#grep -o “root” /etc/passwd
root
root
root
root
例:-w
[root@centos7 ~]#grep -w “root” /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
注:数字、字母、下划线都算字母一部分
例:-e
[root@centos7 ~]#grep -e “root” -e “bash” /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
hehe:x:1000:1000:hehe:/home/hehe:/bin/bash
wang:x:1003:1003::/home/wang:/bin/bash
ren:x:2001:2001::/home/ren:/bin/bash
hi:x:2003:2003:hihi,34r2,324,32:/home/hi:/bin/bash
bb:x:2004:2004::/home/bb:/bin/bash
li:x:2002:2002::/home/li:/bin/bash
gala:x:2005:2005::/data/baibai:/bin/bash
ai:x:2006:2006::/home/ai:/bin/bash
pi:x:2007:2009::/home/pi:/bin/bash
例:-f
[root@centos7 ~]#cat >file
root
bash
[root@centos7 ~]#grep -f file /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
hehe:x:1000:1000:hehe:/home/hehe:/bin/bash
wang:x:1003:1003::/home/wang:/bin/bash
ren:x:2001:2001::/home/ren:/bin/bash
hi:x:2003:2003:hihi,34r2,324,32:/home/hi:/bin/bash
bb:x:2004:2004::/home/bb:/bin/bash
li:x:2002:2002::/home/li:/bin/bash
gala:x:2005:2005::/data/baibai:/bin/bash
ai:x:2006:2006::/home/ai:/bin/bash
pi:x:2007:2009::/home/pi:/bin/bash

“`
##### 基本正则表达式元字符

“`
元字符分类:字符匹配、匹配次数、位置锚定、分组
man 7 regex
“`

“`
字符匹配:
. 匹配任意单个字符
[] 匹配指定范围内的任意单个字符
[^] 匹配指定范围外的任意单个字符
[:alnum:] 字母和数字
[:alpha:] 代表任何英文大小写字符,亦即 A-Z, a-z
[:lower:] 小写字母 [:upper:] 大写字母
[:blank:] 空白字符(空格和制表符)
[:space:] 水平和垂直的空白字符(比[:blank:]包含的范围广)
[:cntrl:] 不可打印的控制字符(退格、删除、警铃…)
[:digit:] 十进制数字 [:xdigit:]十六进制数字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 标点符号
“`

“`
匹配次数:用在要指定次数的字符后面,用于指定前面的字符要出现的次数
* 匹配前面的字符任意次,包括0次
贪婪模式:尽可能长的匹配
.* 任意长度的任意字符
\? 匹配其前面的字符0或1次
\+ 匹配其前面的字符至少1次
\{n\} 匹配前面的字符n次
\{m,n\} 匹配前面的字符至少m次,至多n次
\{,n\} 匹配前面的字符至多n次
\{n,\} 匹配前面的字符至少n次

例:\+
[root@centos7 data]#echo a.b | grep “[a-z.]\+”
a.b
注:中括号里的. 就代表点本身没特殊含义
例:取ip
方法一
[root@centos7 data]#ifconfig ens33 |grep netmask | grep -o “[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}” | head -n1
192.168.67.129
方法二
[root@centos7 data]#ifconfig ens33 |grep netmask |grep -o “inet [0-9.]\+” |cut -d” ” -f2
192.168.67.129
例:取分区利用率
[root@centos7 data]#df |grep /dev/sd |grep -o “[0-9]\{1,3\}%” | grep -o “[0-9]\+”
8
1
16
“`

“`
位置锚定:定位出现的位置
^ 行首锚定,用于模式的最左侧
$ 行尾锚定,用于模式的最右侧
^PATTERN$ 用于模式匹配整行
^$ 空行
^[[:space:]]*$ 空白行
\< 或 \b 词首锚定,用于单词模式的左侧
\> 或 \b 词尾锚定;用于单词模式的右侧
\<PATTERN\> 匹配整个单词
“`

“`
例:过滤空行
[root@centos7 data]#cat bb

aa
abbb
dd
[root@centos7 data]#cat bb |grep -v “^[[:space:]]*$”
aa
abbb
dd

例:\<或者\>
[root@centos7 data]#grep “\<root” /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos7 data]#grep “root\>” /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
adiminroot:x:2008:2010::/home/adiminroot:/bin/bash
“`

“`
分组:\(\) 将一个或多个字符捆绑在一起,当作一个整体进行处理,如:
\(root\)\+
分组括号中的模式匹配到的内容会被正则表达式引擎记录于内部的变量中,这
些变量的命名方式为: \1, \2, \3, …
\1 表示从左侧起第一个左括号以及与之匹配右括号之间的模式所匹配到的字符
示例: \(string1\+\(string2\)*\)
\1 :string1\+\(string2\)*
\2 :string2
后向引用:引用前面的分组括号中的模式所匹配字符,而非模式本身
或者:\|
示例:a\|b: a或b C\|cat: C或cat \(C\|c\)at:Cat或cat
“`

“`
例: 分组后向引用
[root@centos7 data]#cat /etc/passwd |grep “^\(.*\):.*/\1$”
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
“`

“`
例:取系统版本号
[root@centos7 data]#cat /etc/centos-release |grep -o “[0-9]\+” |head -n1
7
“`

“`
例:IP匹配
[root@centos7 ~]#ifconfig ens33 |egrep -o “(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>”
192.168.67.129
255.255.255.0
192.168.67.255
“`

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/95564

(0)
上一篇 2018-04-08 23:02
下一篇 2018-04-09 00:31

相关推荐

  • mysql

    yum安装 1、yum install mysql-server /var/lib/mysql 用户存放的位置 2、chkconfig –list mysqld 查看是否启动 3、chkconfig mysqld on 设置为开机启动 4、ss -ntl;service mysqld start 开启服务 端口号为3306 /!可以执行linux…

    Linux笔记 2018-06-14
  • shell脚本编程基础(3)——循环用法

    在前面的基础编程内容中,我们已经学习了shell脚本的顺序执行及选择执行,通过这两种方式,可以帮我们解决一些简单需求,但要想在更复杂的场景中使用的话,就需要掌握循环执行的方式了。

    2018-05-08
  • N31第二周作业

    本周(6.25–7.1)第2周 1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 目录管理类命令:cd pwd ls mkdir rmdir tree cd:change directory cd [/PATH/TO/SOMEDIR] cd:切换回家目录 注意:bash中,~表示家目录 cd~:切换回自己的家目录 cd…

    Linux笔记 2018-07-02
  • nginx

    nginx(web server,web reverse proxy) http事务:request <—-> response request: <method> <URL> <version> <HEADERS> … <body> response: <…

    2018-06-29
  • RHCE考试笔记

    上午RHCSA部分

    Linux笔记 2018-07-14
  • 命令行展开案例

    [Azuth@azuth ~]$ mkdir /home/Azuth/tmp/{a,b}_{c,d}[Azuth@azuth ~]$ ll /home/Azuth/tmp/total 0drwxrwxr-x. 2 Azuth Azuth 6 May 20 20:35 a_cdrwxrwxr-x. 2 Azuth Azuth 6 May 20 20:35 a_…

    Linux笔记 2018-05-20