文本處理工具、用戶及權限管理

##### 命令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.www58058.com/95564

(0)
王琪鋒王琪鋒
上一篇 2018-04-08 23:02
下一篇 2018-04-09

相關推薦

  • joke

    后續進行修改

    Linux筆記 2018-04-08
  • mysql數據庫四

    備份和還原 主從復制,

    2018-06-11
  • Centos7登陸顏色修改

    ? 在用遠程登陸軟件登陸Centos時,登陸提示符是一串黑色的字符,假如登陸的窗口多時很容易造成各種運維事故。本來要在機器A上修改文件配置,結果沒看清在機器B上修改文件配置,造成不必要的麻煩。這時修改登陸字符顏色就顯得很重要,設置一個醒目的提示字符顏色可減少不必要的麻煩,也能夠提醒自己謹慎操作!下面給大家說明下相關配置要求。 ? ? ? ? ? ? ? ? …

    Linux筆記 2018-03-31
  • 多種協議到網絡配置

    第五周總結、協議、網絡基礎、路由的配置等

    2018-05-08
  • 第三周筆記

    Linux的文本處理三劍客> grep:文本過濾(模式:pattern)工具> grep, egrep, fgrep(不支持正則表達式搜索)> sed:stream editor,文本編輯工具> awk:Linux上的實現gawk,文本報告生成器– ### grep作用是根據“pattern”對目標文件進行搜索,并且顯示匹…

    Linux筆記 2018-04-15
欧美性久久久久