?
?
?
#!?/bin/bash #?Function:?根據(jù)用戶輸入的PID,過濾出該P(yáng)ID所有的信息 read?-p?"請輸入要查詢的PID:?"?P n=`ps?-aux|?awk?'$2~/^'$P'$/{print?$11}'|wc?-l` if?[?$n?-eq?0?];then ?echo?"該P(yáng)ID不存在!!" ?exit fi echo?"--------------------------------" echo?"進(jìn)程PID:?$P" echo?"進(jìn)程命令:`ps -aux| awk '$2~/^'$P'$/{print?$11}'`" echo?"進(jìn)程所屬用戶:?`ps?-aux|?awk?'$2~/^'$P'$/{print?$1}'`" echo?"CPU占用率:`ps -aux| awk '$2~/^'$P'$/{print?$3}'`%" echo?"內(nèi)存占用率:`ps -aux| awk '$2~/^'$P'$/{print?$4}'`%" echo?"進(jìn)程開始運(yùn)行的時(shí)刻:`ps -aux| awk '$2~/^'$P'$/{print?$9}'`" echo?"進(jìn)程運(yùn)行的時(shí)間:`ps -aux| awk '$2~/^'$P'$/{print?$10}'`" echo?"進(jìn)程狀態(tài):`ps -aux| awk '$2~/^'$P'$/{print?$8}'`" echo?"進(jìn)程虛擬內(nèi)存:`ps -aux| awk '$2~/^'$P'$/{print?$5}'`" echo?"進(jìn)程共享內(nèi)存:`ps -aux| awk '$2~/^'$P'$/{print?$6}'`" echo?"--------------------------------"
?
二、根據(jù)進(jìn)程名過濾進(jìn)程信息
會(huì)顯示出該進(jìn)程名包含的所有線程
?
#!?/bin/bash #?Function:?根據(jù)輸入的程序的名字過濾出所對應(yīng)的PID,并顯示出詳細(xì)信息,如果有幾個(gè)PID,則全部顯示 read?-p?"請輸入要查詢的進(jìn)程名:"?NAME N=`ps?-aux?|?grep?$NAME?|?grep?-v?grep?|?wc?-l`?##統(tǒng)計(jì)進(jìn)程總數(shù) if?[?$N?-le?0?];then ??echo?"該進(jìn)程名沒有運(yùn)行!" fi i=1 while?[?$N?-gt?0?] do ??echo?"進(jìn)程PID:?`ps?-aux?|?grep?$NAME?|?grep?-v?grep?|?awk?'NR=='$i'{print?$0}'|?awk?'{print?$2}'`" ??echo?"進(jìn)程命令:`ps -aux | grep $NAME?|?grep?-v?grep?|?awk?'NR=='$i'{print?$0}'|?awk?'{print?$11}'`" ??echo?"進(jìn)程所屬用戶:?`ps?-aux?|?grep?$NAME?|?grep?-v?grep?|?awk?'NR=='$i'{print?$0}'|?awk?'{print?$1}'`" ??echo?"CPU占用率:`ps -aux | grep $NAME?|?grep?-v?grep?|?awk?'NR=='$i'{print?$0}'|?awk?'{print?$3}'`%" ??echo?"內(nèi)存占用率:`ps -aux | grep $NAME?|?grep?-v?grep?|?awk?'NR=='$i'{print?$0}'|?awk?'{print?$4}'`%" ??echo?"進(jìn)程開始運(yùn)行的時(shí)刻:`ps -aux | grep $NAME?|?grep?-v?grep?|?awk?'NR=='$i'{print?$0}'|?awk?'{print?$9}'`" ??echo?"進(jìn)程運(yùn)行的時(shí)間:` ps -aux | grep $NAME?|?grep?-v?grep?|?awk?'NR=='$i'{print?$0}'|?awk?'{print?$11}'`" ??echo?"進(jìn)程狀態(tài):`ps -aux | grep $NAME?|?grep?-v?grep?|?awk?'NR=='$i'{print?$0}'|?awk?'{print?$8}'`" ??echo?"進(jìn)程虛擬內(nèi)存:`ps -aux | grep $NAME?|?grep?-v?grep?|?awk?'NR=='$i'{print?$0}'|?awk?'{print?$5}'`" ??echo?"進(jìn)程共享內(nèi)存:`ps -aux | grep $NAME?|?grep?-v?grep?|?awk?'NR=='$i'{print?$0}'|?awk?'{print?$6}'`" ??echo?"***************************************************************" ??let?N--?i++ done
?
三、根據(jù)用戶名查詢該用戶的相關(guān)信息
?
#!?/bin/bash # Function:根據(jù)用戶名查詢該用戶的所有信息 read?-p?"請輸入要查詢的用戶名:"?A echo?"------------------------------" n=`cat?/etc/passwd?|?awk?-F:?'$1~/^'$A'$/{print}'?|?wc?-l` if?[?$n?-eq?0?];then echo?"該用戶不存在" echo?"------------------------------" else ??echo?"該用戶的用戶名:$A" ??echo?"該用戶的UID:`cat /etc/passwd | awk -F:?'$1~/^'$A'$/{print}'|awk?-F:?'{print?$3}'`" ??echo?"該用戶的組為:`id $A?|?awk?{'print?$3'}`" ??echo?"該用戶的GID為:`cat /etc/passwd | awk -F:?'$1~/^'$A'$/{print}'|awk?-F:?'{print?$4}'`" ??echo?"該用戶的家目錄為:`cat /etc/passwd | awk -F:?'$1~/^'$A'$/{print}'|awk?-F:?'{print?$6}'`" ??Login=`cat?/etc/passwd?|?awk?-F:?'$1~/^'$A'$/{print}'|awk?-F:?'{print?$7}'` ??if?[?$Login?==?"/bin/bash"?];then ??echo?"該用戶有登錄系統(tǒng)的權(quán)限!!" ??echo?"------------------------------" ??elif?[?$Login?==?"/sbin/nologin"?];then ??echo?"該用戶沒有登錄系統(tǒng)的權(quán)限!!" ??echo?"------------------------------" ??fi fi
?
四、加固系統(tǒng)的一些配置
?
#!?/bin/bash #?Function:對賬戶的密碼的一些加固 read?-p?"設(shè)置密碼最多可多少天不修改:"?A read?-p?"設(shè)置密碼修改之間最小的天數(shù):"?B read?-p?"設(shè)置密碼最短的長度:"?C read?-p?"設(shè)置密碼失效前多少天通知用戶:"?D sed?-i?'/^PASS_MAX_DAYS/cPASS_MAX_DAYS?'$A''?/etc/login.defs sed?-i?'/^PASS_MIN_DAYS/cPASS_MIN_DAYS?'$B''?/etc/login.defs sed?-i?'/^PASS_MIN_LEN/cPASS_MIN_LEN?'$C''?/etc/login.defs sed?-i?'/^PASS_WARN_AGE/cPASS_WARN_AGE?'$D''?/etc/login.defs echo?"已對密碼進(jìn)行加固,新用戶不得和舊密碼相同,且新密碼必須同時(shí)包含數(shù)字、小寫字母,大寫字母!!" sed?-i?'/pam_pwquality.so/cpassword?requisite?pam_pwquality.so?try_first_pass?local_users_only?retry=3?authtok_type=?difok=1?minlen=8?ucredit=-1?lcredit=-1?dcredit=-1'?/etc/pam.d/system-auth echo?"已對密碼進(jìn)行加固,如果輸入錯(cuò)誤密碼超過3次,則鎖定賬戶!!" n=`cat?/etc/pam.d/sshd?|?grep?"auth?required?pam_tally2.so?"|wc?-l` if?[?$n?-eq?0?];then sed?-i?'/%PAM-1.0/aauth?required?pam_tally2.so?deny=3?unlock_time=150?even_deny_root?root_unlock_time300'?/etc/pam.d/sshd fi echo??"已設(shè)置禁止root用戶遠(yuǎn)程登錄!!" sed?-i?'/PermitRootLogin/cPermitRootLogin?no'??/etc/ssh/sshd_config read?-p?"設(shè)置歷史命令保存條數(shù):"?E read?-p?"設(shè)置賬戶自動(dòng)注銷時(shí)間:"?F sed?-i?'/^HISTSIZE/cHISTSIZE='$E''?/etc/profile sed?-i?'/^HISTSIZE/aTMOUT='$F''?/etc/profile echo?"已設(shè)置只允許wheel組的用戶可以使用su命令切換到root用戶!" sed?-i?'/pam_wheel.so?use_uid/cauth?required?pam_wheel.so?use_uid?'?/etc/pam.d/su n=`cat?/etc/login.defs?|?grep?SU_WHEEL_ONLY?|?wc?-l` if?[?$n?-eq?0?];then echo?SU_WHEEL_ONLY?yes?>>?/etc/login.defs fi echo?"即將對系統(tǒng)中的賬戶進(jìn)行檢查...." echo?"系統(tǒng)中有登錄權(quán)限的用戶有:" awk?-F:?'($7=="/bin/bash"){print?$1}'?/etc/passwd echo?"********************************************" echo?"系統(tǒng)中UID=0的用戶有:" awk?-F:?'($3=="0"){print?$1}'?/etc/passwd echo?"********************************************" N=`awk?-F:?'($2==""){print?$1}'?/etc/shadow|wc?-l` echo?"系統(tǒng)中空密碼用戶有:$N" if?[?$N?-eq?0?];then ?echo?"恭喜你,系統(tǒng)中無空密碼用戶!!" ?echo?"********************************************" else ?i=1 ?while?[?$N?-gt?0?] ?do ????None=`awk?-F:?'($2==""){print?$1}'?/etc/shadow|awk?'NR=='$i'{print}'` ????echo?"------------------------" ????echo?$None ????echo?"必須為空用戶設(shè)置密碼!!" ????passwd?$None ????let?N-- ?done ?M=`awk?-F:?'($2==""){print?$1}'?/etc/shadow|wc?-l` ?if?[?$M?-eq?0?];then ??echo?"恭喜,系統(tǒng)中已經(jīng)沒有空密碼用戶了!" ?else echo?"系統(tǒng)中還存在空密碼用戶:$M" ?fi fi echo?"即將對系統(tǒng)中重要文件進(jìn)行鎖定,鎖定后將無法添加刪除用戶和組" read?-p?"警告:此腳本運(yùn)行后將無法添加刪除用戶和組!!確定輸入Y,取消輸入N;Y/N:"?i case?$i?in ??????[Y,y]) ????????????chattr?+i?/etc/passwd ????????????chattr?+i?/etc/shadow ????????????chattr?+i?/etc/group ????????????chattr?+i?/etc/gshadow ????????????echo?"鎖定成功!" ;; ??????[N,n]) ????????????chattr?-i?/etc/passwd ????????????chattr?-i?/etc/shadow ????????????chattr?-i?/etc/group ????????????chattr?-i?/etc/gshadow ????????????echo?"取消鎖定成功!!" ;; ???????*) ????????????echo?"請輸入Y/y?or?N/n" esac
?
五、實(shí)現(xiàn)磁盤分區(qū)的
只支持分配主分區(qū)和標(biāo)準(zhǔn)的linux文件系統(tǒng)(ext4/xfs)的分區(qū)
?
#!?/bin/bash #?Function:對硬盤進(jìn)行分區(qū),得到一個(gè)標(biāo)準(zhǔn)的linux文件系統(tǒng)(ext4/xfs)的主分區(qū) cat?/proc/partitions?>?old read?-p?"請輸入你要分區(qū)的硬盤(寫絕對路徑,如:/dev/sda):"?A if?[?-e?$A?];then ??echo?"true" else ??echo?"該設(shè)備不存在!!" ??exit fi read?-p?"請輸入你要?jiǎng)?chuàng)建的磁盤分區(qū)類型(這里只能是主分區(qū),默認(rèn)按回車即可):"?B read?-p?"請輸入分區(qū)數(shù)字,范圍1-4,默認(rèn)從1開始,默認(rèn)按回車即可:"?C read?-p?"請輸入扇區(qū)起始表號,默認(rèn)按回車即可:"?D read?-p?"請輸入你要分區(qū)的分區(qū)大小(格式:如?+5G?):"?E fdisk?$A?<?new F=`diff?new?old?|?grep?"<"?|?awk?'{print?$5}'` echo?"-------------------------------" echo?$F echo?"你想對新分區(qū)設(shè)定什么類型的文件系統(tǒng)?有以下選項(xiàng):" echo?"A:ext4文件系統(tǒng)" echo?"B:xfs文件系統(tǒng)" read?-p?"請輸入你的選擇:"?G case?$G?in ????????a|A) ???????????mkfs.ext4?/dev/$F ???????????echo?"該分區(qū)將被掛載在?"/mnt/$F"?下"? ???????????m=`ls?/mnt/|grep?$F?|?wc?-l` ???????????if?[?$m?-eq?0?];then ????????????mkdir?/mnt/$F ???????????fi ???????????n=`cat?/etc/fstab?|?grep?/dev/$F|?wc?-l` ???????????if?[?$n?-eq?0?];then ??????????????echo?"/dev/$F?????/mnt/$F?????ext4?????????defaults??????????0??????0"?>>?/etc/fstab ???????????else ??????????????sed?-i?'/^/dev/$F/c/dev/$F?????/mnt/$F?????ext4?????????defaults??????????0??????0'?/etc/fstab ???????????fi ???????????mount?-a ???????????df?-Th ;; ????????b|B) ???????????mkfs.xfs?-f?/dev/$F ???????????echo?"該分區(qū)將被掛載在?"/mnt/$F"?下"? ???????????m=`ls?/mnt/|grep?$F?|?wc?-l` ???????????if?[?$m?-eq?0?];then ??????????????mkdir?/mnt/$F ???????????fi ???????????n=`cat?/etc/fstab?|?grep?/dev/$F?|?wc?-l` ???????????if?[?$n?-eq?0?];then ??????????????echo?"/dev/$F?????/mnt/$F??????xfs???????defaults??????????0??????0"?>>?/etc/fstab ???????????else ??????????????sed?-i?'/^/dev/$F/c/dev/$F?????/mnt/$F?????xfs?????????defaults??????????0??????0'?/etc/fstab ???????????fi ???????????mount?-a ???????????df?-Th ;; ????????*) ???????????echo?"你的輸入有誤!!" esac
?
六、使用一整塊硬盤創(chuàng)建邏輯卷
?
#!/bin/bash #?Function:使用一整塊硬盤創(chuàng)建LVM邏輯卷 read?-p?"請輸入你要做成邏輯卷的硬盤(寫絕對路徑,如:/dev/sda):"?path if?[?-e?$path?];then ??echo?"true" else ??echo?"該設(shè)備不存在!!" ??exit fi pvcreate?$path echo?"該硬盤已做成物理卷!" vgcreate?myvg?$path echo?"該物理卷已加入卷組?myvg?中" vgs free=`vgs|?awk?'$1~/myvg/{print}'|awk?'{print?$6}'` echo?"該物理卷剩余的空間大小為:$free?" read?-p?"請輸入你要?jiǎng)?chuàng)建邏輯卷的大小(如:1G):"?repy2 lvcreate?-L?$repy2?-n?mylv?myvg echo?"已成功創(chuàng)建邏輯卷mylv" echo?"------------------------" lvs echo?"------------------------" echo?"你想對新分區(qū)設(shè)定什么類型的文件系統(tǒng)?有以下選項(xiàng):" echo?"A:ext4文件系統(tǒng)" echo?"B:xfs文件系統(tǒng)" read?-p?"請輸入你的選擇:"?repy3 case?$repy3?in ????????a|A) ???????????mkfs.ext4?/dev/myvg/mylv ???????????echo?"該分區(qū)將被掛載在?"/mnt/mylv"?下"? ???????????m=`ls?/mnt/|grep?mylv?|?wc?-l` ???????????if?[?$m?-eq?0?];then ????????????mkdir?/mnt/mylv ???????????fi ???????????echo?"/dev/myvg/mylv?????/mnt/mylv?????ext4?????????defaults??????????0??????0"?>>?/etc/fstab ???????????mount?-a ???????????df?-Th ;; ????????b|B) ???????????mkfs.xfs?-f?/dev/myvg/mylv ???????????echo?"該分區(qū)將被掛載在?"/mnt/mylv"?下"? ???????????m=`ls?/mnt/|grep?mylv?|?wc?-l` ???????????if?[?$m?-eq?0?];then ??????????????mkdir?/mnt/mylv ???????????fi ???????????echo?"/dev/myvg/mylv?????/mnt/mylv??????xfs???????defaults??????????0??????0"?>>?/etc/fstab ???????????mount?-a ???????????df?-Th ;; ????????*) ???????????echo?"你的輸入有誤!!" esac
?
七、將一塊硬盤分區(qū),然后分區(qū)設(shè)置為虛擬卷
?
#!?/bin/bash #?Author:謝公子 #?Date:2018-10-13 #?Function:新建一個(gè)分區(qū),并做成邏輯卷 cat?/proc/partitions?>?old read?-p?"請輸入你要分區(qū)的硬盤(寫絕對路徑,如:/dev/sda):"?A if?[?-e?$A?];then ??echo?"true" else ??echo?"該設(shè)備不存在!!" ??exit fi read?-p?"請輸入你要?jiǎng)?chuàng)建的磁盤分區(qū)類型(這里只能是主分區(qū),默認(rèn)按回車即可):"?B read?-p?"請輸入分區(qū)數(shù)字,范圍1-4,默認(rèn)從1開始,默認(rèn)按回車即可:"?C read?-p?"請輸入扇區(qū)起始表號,默認(rèn)按回車即可:"?D read?-p?"請輸入你要分區(qū)的分區(qū)大小(格式:如?+5G?):"?E read?-p?"請輸入你要?jiǎng)澐譃檫壿嬀淼姆謪^(qū)盤符(默認(rèn)回車即可):"?Z fdisk?$A?<?new F=`diff?new?old?|?grep?"<"?|?awk?'{print?$5}'` echo?"-------------------------------" echo?$F pvcreate?/dev/$F echo?"該硬盤已做成物理卷!" n=`vgs?|?grep?myvg?|wc?-l` if?[?$n?-eq?0?];then ???vgcreate?myvg?/dev/$F ???echo?"該物理卷已加入卷組myvg中" else ???vgextend?myvg?/dev/$F ???echo??"該物理卷已加入卷組myvg中" ???vgs ???free=`vgs|?awk?'$1~/myvg/{print}'|awk?'{print?$7}'` ???echo?"該卷組剩余的空間大小為:$free?" ???lvs ???exit fi vgs free=`vgs|?awk?'$1~/myvg/{print}'|awk?'{print?$6}'` echo?"該卷組剩余的空間大小為:$free?" read?-p?"請輸入你要?jiǎng)?chuàng)建邏輯卷的大小(如:1G):"?repy2 lvcreate?-L?$repy2?-n?mylv?myvg echo?"已成功創(chuàng)建邏輯卷mylv" echo?"------------------------" lvs echo?"------------------------" echo?"你想對新分區(qū)設(shè)定什么類型的文件系統(tǒng)?有以下選項(xiàng):" echo?"A:ext4文件系統(tǒng)" echo?"B:xfs文件系統(tǒng)" read?-p?"請輸入你的選擇:"?G case?$G?in ????????a|A) ???????????mkfs.ext4?/dev/myvg/mylv ???????????echo?"該分區(qū)將被掛載在?"/mnt/$F"?下"? ???????????m=`ls?/mnt/|grep?$F?|?wc?-l` ???????????if?[?$m?-eq?0?];then ????????????mkdir?/mnt/$F ???????????fi ???????????echo?"/dev/myvg/mylv?????/mnt/$F?????ext4?????????defaults??????????0??????0"?>>?/etc/fstab ???????????mount?-a ???????????df?-Th ;; ????????b|B) ???????????mkfs.xfs?-f?/dev/myvg/mylv ???????????echo?"該分區(qū)將被掛載在?"/mnt/$F"?下"? ???????????m=`ls?/mnt/|grep?$F?|?wc?-l` ???????????if?[?$m?-eq?0?];then ??????????????mkdir?/mnt/$F ???????????fi ???????????echo?"/dev/myvg/mylv?????/mnt/$F??????xfs???????defaults??????????0??????0"?>>?/etc/fstab ???????????mount?-a ???????????df?-Th ;; ????????*) ???????????echo?"你的輸入有誤!!" esac
?
審核編輯:湯梓紅
評論
查看更多