2020-12-21 第四周

1、統(tǒng)計出/etc/passwd文件中其默認shell為非/sbin/nologin的用戶個數(shù),并將用戶都顯示出來

[22:39:34? root@centos8 ~]#egrep "/sbin/nologin$" /etc/passwd |wc -l

20

[22:40:04? root@centos8 ~]#egrep "/sbin/nologin$" /etc/passwd | cat -n

1? bin:x:1:1:bin:/bin:/sbin/nologin

2? daemon:x:2:2:daemon:/sbin:/sbin/nologin

3? adm:x:3:4:adm:/var/adm:/sbin/nologin

4? lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

5? mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

6? operator:x:11:0:operator:/root:/sbin/nologin

7? games:x:12:100:games:/usr/games:/sbin/nologin

8? ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

9? nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin

10? dbus:x:81:81:System message bus:/:/sbin/nologin

11? systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin

12? systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin

13? tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin

14? polkitd:x:998:996:Userforpolkitd:/:/sbin/nologin

15? unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin

16? sssd:x:996:993:Userforsssd:/:/sbin/nologin

17? sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

18? saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin

19? tcpdump:x:72:72::/:/sbin/nologin

20? slackware:x:2002:2019::/home/slackware:/sbin/nologin

?

2、查出用戶UID最大值的用戶名、UID及shell類型

sort命令選項:

? ? -n:依照數(shù)值得大小排序

? ? -r:以相反的順序進行排序

? ? -t:指定分割符

? ? -K:指定列數(shù)

[01:24:22? root@centos8 ~]#cat /etc/passwd | cut -d: -f1,3,7 | sort -t: -k 2 -nr

nobody:65534:/sbin/nologin

user3:2005:/bin/bash

user2:2004:/bin/bash

user1:2003:/bin/bash

slackware:2002:/sbin/nologin

mageia:1100:/bin/bash

zf:1000:/bin/bash

systemd-coredump:999:/sbin/nologin

polkitd:998:/sbin/nologin

unbound:997:/sbin/nologin

sssd:996:/sbin/nologin

saslauth:995:/sbin/nologin

systemd-resolve:193:/sbin/nologin

dbus:81:/sbin/nologin

sshd:74:/sbin/nologin

tcpdump:72:/sbin/nologin

tss:59:/sbin/nologin

ftp:14:/sbin/nologin

games:12:/sbin/nologin

operator:11:/sbin/nologin

mail:8:/sbin/nologin

halt:7:/sbin/halt

shutdown:6:/sbin/shutdown

sync:5:/bin/sync

lp:4:/sbin/nologin

adm:3:/sbin/nologin

daemon:2:/sbin/nologin

bin:1:/sbin/nologin

root:0:/bin/bash

[01:24:31? root@centos8 ~]#cat /etc/passwd | cut -d: -f1,3,7 | sort -t: -k 2 -nr | head -1

nobody:65534:/sbin/nologin

?

[01:30:55? root@centos8 ~]#sort -t: -k 3 -nr /etc/passwd | cut -d : -f1,3,7 | head -1

nobody:65534:/sbin/nologin

3、統(tǒng)計當(dāng)前連接本機的每個遠程主機IP的連接數(shù),并按從大到小排序

[22:09:58? root@centos8 scripts]#w | egrep "^root" | tr -s ' ' | cut -d ' ' -f 3 | uniq -c | sort -nr

2 192.168.10.3

11 92.168.10.22

4、編寫腳本disk.sh,顯示當(dāng)前硬盤分區(qū)中空間利用率最大的值

[22:22:35? root@centos8 scripts]#df -h | tr -s ' ' % | tr -d '[a-z|A-Z]'|cut -d % -f 5 | sort -nr

15

11

2

0

0

0

0

5、編寫腳本 systeminfo.sh,顯示當(dāng)前主機系統(tǒng)信息,包括:主機名,IPv4地址,操作系統(tǒng)版本,內(nèi)核版本,CPU型號,內(nèi)存大小,硬盤大小

[01:26:23? root@node scripts]#cat systeminfo.sh

#!/bin/bash

echo "-------------------system info--------------------------"

echo "當(dāng)前主機名是: ? ? ? ? ?? `hostname`"

echo "當(dāng)前的主機IP地址是: ? ?? `ifconfig | grep inet | egrep -o '[0-9]{1,3}(\.[0-9]+){1,3}[0-9]{1,3}' | head -1`"

echo "當(dāng)前的操作系統(tǒng)版本是: ?? `cat /etc/redhat-release`"

echo "當(dāng)前的內(nèi)核版本是: ? ? ?? `uname -r`"

echo "當(dāng)前的cpu型號是: ? ? ? ? `lscpu | grep Model | cut -d: -f2 | tail -1 | tr -s ' '`"

echo "當(dāng)前的系統(tǒng)的總內(nèi)存是: ? ? `free -h | grep Mem | tr -s ' ' : | cut -d: -f2`"

echo "當(dāng)前的硬盤的大小是: ? ? ? `lsblk | grep disk | tr -s ' ' : | cut -d: -f5 `"

echo "--------------------------------------------------------"

6、20分鐘內(nèi)通關(guān)vimtutor(可參考https://yyqing.me/post/2017/2017-02-22-vimtutor-chinese-summary

vim 文本編輯工具

官方網(wǎng)站:www.vim.org

幫助手冊:vimtutor

vim基礎(chǔ)用法:

命令格式:

vim[options] [file ..]

常用選項

+#:打開文件讓光標處于第#行,+默認文件的行尾

+/pattern:讓光標處于第一個被pattern匹配到的行首

-bfile:二進制的方式打開文件

-dfile1 file2:比較多個文件,相當(dāng)于vimdiff

-mfile:只讀方式打開文件

-efile:直接進入ex模式,相當(dāng)于執(zhí)行ex file

備注說明:

文件存在,文件被打開并顯示文件內(nèi)容

文件不存在,當(dāng)編輯第一次文件存盤時會創(chuàng)建它

vim的三種模式及模式的轉(zhuǎn)換:

三種常見的模式:

命令模式:默認文件打開就是命令模式,可以實現(xiàn)光標的移動,剪切或粘貼文本

插入模式:可以修改文件的內(nèi)容

末行模式:可以保存退出或進行文本的替換,查尋等功能

三種模式的轉(zhuǎn)換方法:

命令模式 --> 插入模式

i:在光標所在處輸入內(nèi)容

I:在光標所在行的行首輸入

a:在光標所在的后面輸入

A:在光標所在行的行尾輸入

o:在光標所在行的下方打開一個新行進行輸入

O:在光標所在行的上方打開一個新行進行輸入

插入模式-->esc-->命令模式--:--> 末行模式(wq(保存退出文檔))

末行模式常用用法:

基本命令的使用:

w:寫(存)磁盤文件

wq:寫入并退出

x:寫入并退出

X: 將編輯的文件進行加密

q:退出

q!:不存盤退出,即使更改的數(shù)據(jù)都將丟失

r file: 將文件的內(nèi)容讀取到當(dāng)前文件中

w file:將當(dāng)前文件的內(nèi)容寫入到另一個文件

!command:執(zhí)行命令行命令

r!command:讀入命令的輸入到當(dāng)前文件

地址定界的命令使用:

命令格式:

:start_pos,end_pos CMD

地址定界語法格式:

#:具體的第#行

#,#:表示第幾行到第幾行

#,+#:表示的當(dāng)前行到后面的第幾行,例如2,+3:代表第2行到第5行

.:代表當(dāng)前行

$:代表最后一行

.,$-1:當(dāng)前行到倒數(shù)第二行

%:顯示文件全文

/pattern/:從當(dāng)前行向下查找,直到匹配到pattern的第一行

/pat1/,/pat2/:從第一次被pat1模式匹配到行的開始,一直到第一次被pat2匹配到的行結(jié)束

#,/pattern/:從指定行開始,一直找到匹配到pattern的行

/pat/,$:向下找到第一個匹配到pattern的行到整個文件的結(jié)尾的所有行。

地址定界后跟一個編輯命令

d:刪除

y:復(fù)制

w file:將范圍內(nèi)的行另存至指定的文件中

r file:在指定的位置插入指定文件中的所有內(nèi)容

查找和替換的命令使用:

命令格式:

s/要查找的內(nèi)容/替換為的內(nèi)容/修飾符

? ? 備注:

? ? ? ? 要查找的內(nèi)容可以使用基本的正則表達式

? ? ? ? 替換為的內(nèi)容,不能使用模式,但可以使用\1,\2等后向引用的符號。

? ? ? ? 查找替代中的分割符/可以使用其他的字符替換,如#,@

修飾符:

i:忽略大小寫

g:全局替換,默認情況下,每行只替換第一次出現(xiàn)

gc:全局替換,每次替換前詢問

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容