本週作業內容:
1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。
[root@localhost ~]# who | cut -d' ' -f1 | sort -u
2、取出最後登錄到當前系統的用戶的相關信息。
[root@localhost ~]# who | tail -n1 # 或 [root@localhost ~]# last | head -n1
3、取出當前系統上被用戶當作其默認shell的最多的那個shell。
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | sort | uniq -c | sort -n | tail -n1
# 輸出結果爲: 20 /sbin/nologin
4、將/etc/passwd 中的第三個字段數值最大的後10個用戶的信息全部改爲大寫後保存至/tmp/maxusers.txt文件中。
# 創建maxusers.txt文件: [root@localhost ~]# touch /tmp/maxusers.txt
#將結果保存至maxusers.txt中: [root@localhost ~]# sort -t: -k3 -n /etc/passwd | tail -n10 | tr 'a-z' 'A-Z' > /tmp/maxusers.txt
# 結果顯示(驗證): [root@localhost ~]# cat /tmp/maxusers.txt
5、取出當前主機的IP地址,提示:對ifconfig命令的結果進行切分。
對於CentOS6.x可以採用: [root@localhost ~]# ifconfig | grep " inet " | cut -d: -f2 | cut -d' ' -f1 | head -1 192.168.178.129 但CentOS7.x上該命令並不可用,爲何? 對比兩個版本,會發覺同樣的ifconfig命令,IP前面的標識名稱,位置是不一樣的,而且網卡名稱也不相同。 有沒有一種辦法可以實現通用。經過各方面嘗試,以及跟同學們的討論後得出,使用多次grep加正則表達式可實現: 對CentOS6.x和CentOS7.x均適用的命令: [root@localhost ~]# ifconfig |grep -A 7 "^e[tn]" |grep -o -i " inet .*$" | grep -o "[0-9].*$"| cut -d' ' -f1 192.168.178.129
6、列出/etc目錄下所有以.conf結尾的文件的文件名,並將其名字轉換爲大寫後保存至/tmp/etc.conf文件中。
# 創建etc.conf文件: [root@localhost ~]# touch /tmp/etc.conf
# 保存輸出結果: [root@localhost ~]# ls -a /etc/*.conf | tr 'a-z' 'A-Z' > /tmp/etc.conf
# 顯示輸出(驗證結果正確性): [root@localhost ~]# cat /tmp/etc.conf
7、顯示/var目錄下一級子目錄或文件的總個數。
[root@localhost ~]# ls -a /var | wc -w 26
8、取出/etc/group文件中第三個字段數值最小的10個組的名字。
[root@localhost ~]# cat /etc/group | sort -t: -k3 -n | head -n 10
# 或: [root@localhost ~]# sort -t: -k3 -n /etc/group | head -n 10
9、將/etc/fstab和/etc/issue文件的內容合併爲同一個內容後保存至/tmp/etc.test文件中。
# 創建etc.test文件: [root@localhost ~]# touch /tmp/etc.test
#將兩文件內容合併保存在etc.test文件中 [root@localhost ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
# 結果顯示: [root@localhost ~]# cat /tmp/etc.test
10、請總結描述用戶和組管理類命令的使用方法並完成以下練習:
(1)、創建組distro,其GID爲2016;
[root@localhost ~]# group -g 2016
(2)、創建用戶mandriva, 其ID號爲1005;基本組爲distro;
[root@localhost ~]# useradd mandriva -u 1005 -g distro
(3)、創建用戶mageia,其ID號爲1100,家目錄爲/home/linux;
[root@localhost ~]# useradd mageia -u 1100 -d /home/linux
(4)、給用戶mageia添加密碼,密碼爲mageedu;
[root@localhost ~]# echo 'mageedu' | passwd mageia --stdin
(5)、刪除mandriva,但保留其家目錄;
# 刪除用戶mandriva: [root@localhost ~]# userdel mandriva
結果驗證:
#(1)查看賬號是否刪除: [root@localhost ~]# cat /etc/passwd
#(2)查看刪除後mandriva家目錄是否存在: [root@localhost ~]# ll /home
(6)、創建用戶slackware,其ID號爲2002,基本組爲distro,附加組peguin;
# 創建組peguin: [root@localhost ~]# groupadd peguin
# 創建用戶slackware: [root@localhost ~]# useradd slackware -u 2002 -g distro -G peguin
# 驗證結果: [root@localhost ~]# id slackware
(7)、修改slackware的默認shell爲/bin/tcsh;
[root@localhost ~]# usermod -s /bin/tcsh slackware
(8)、爲用戶slackware新增附加組admins;
# 新增組admins: [root@localhost ~]# groupadd admins
# 爲用戶slackware新增附加組admins: [root@localhost ~]# usermod -G admins slackware
(9)、爲slackware添加密碼,且要求密碼最短使用期限爲3天,最長爲180天,警告爲3天;
[root@localhost ~]# passwd slackware [root@localhost ~]# passwd -n3 -x180 -w3 slackware
(10)、添加用戶openstack,其ID號爲3003, 基本組爲clouds,附加組爲peguin和nova;
# 創建clouds、nova組: [root@localhost ~]# groupadd clouds [root@localhost ~]# groupadd nova # 創建用戶openstack: [root@localhost ~]# useradd openstack -u 3003 -g clouds -G peguin,nova
# 結果驗證: [root@localhost ~]# id openstack uid=3003(openstack) gid=2020(clouds) groups=2020(clouds),2017(peguin),2021(nova)
(11)、添加系統用戶mysql,要求其shell爲/sbin/nologin;
[root@localhost ~]# useradd mysql -r -s /sbin/nologin
# 查看: [root@localhost ~]# cat /etc/passwd | tail -1 mysql:x:498:498::/home/mysql:/sbin/nologin
(12)、使用echo命令,非交互式爲openstack添加密碼。
[root@localhost ~]# echo "mylinux" | passwd --stdin openstack