一、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可.
[root@test ~]# who | cut -d' ' -f1 | sort -u
二、取出最后登錄到當前系統的用戶的相關信息。
[root@test ~]# who | tail -1 | cut -d' ' -f1 | id
三、取出當前系統上被用戶當作其默認shell的最多的那個shell。
[root@test ~]# cut -d: -f7 /etc/passwd | sort | uniq -c | sort -n | tail -1
四、將/etc/passwd中的第三個字段數值最大的后10個用戶的信息全部改為大寫后保存至/tmp/maxusers.txt文件中。
[root@test ~]# cat /etc/passwd | sort -n -t: -k3 | tail | tr 'a-z' 'A-Z' > /tmp/maxusers.txt
五、取出當前主機的IP地址,提示:對ifconfig命令的結果進行切分。
[root@test ~]# ifconfig | grep "inet addr:" | grep -v "127.0.0.1" | cut -d: -f2 | cut -d' ' -f1
六、列出/etc目錄下所有以.conf結尾的文件的文件名,并將其名字轉換為大寫后保存至/tmp/etc.conf文件中。
[root@test ~]# ls /etc | grep ".conf$" | tr 'a-z' 'A-Z' > /tmp/etc.conf
七、顯示/var目錄下一級子目錄或文件的總個數。
[root@test ~]# ls -R /var | wc -l
八、取出/etc/group文件中第三個字段數值最小的10個組的名字。
[root@test ~]# cat /etc/group | sort -n -t: -k3 | head | cut -d: -f1
九、將/etc/fstab和/etc/issue文件的內容合并為同一個內容后保存至/tmp/etc.test文件中。
[root@test ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
十、請總結描述用戶和組管理類命令的使用方法并完成以下練習:
使用方法如下:
創建用戶
useradd:創建新用戶
-u:指定uid
-g:指定gid
-s:指定登錄的shell
-G:指定附加組
-d:指定home目錄
-c:用戶注釋說明
-r:指定新建用戶屬于系統用戶
創建組
groupadd:創建新組
-g:指定gid
-r:指定為系統組
編輯用戶
usermod:編輯已有用戶
-a:添加進附加組
-G:指定附加組
-c:修改注釋信息
-u:修改uid
-g:修改gid
-d:修改變更為新的home目錄
-L:鎖定用戶
-U:解鎖用戶
-l login_name:指定新的用戶登錄名
編輯組
groupmod:編輯已有組
-g:修改gid
-n group_name:指定新的組名
查詢
id:查詢當前用戶信息
id username:查詢指定用戶名信息
cat /etc/passwd :顯示系統下所有用戶的信息列表
cat /etc/group:顯示系統下所有組以及組內用戶列表信息
刪除用戶
userdel:刪除用戶
-f –force:強制刪除,即使用戶當前屬于登錄狀態
-r –remove:刪除用戶時,其home目錄一并刪除
刪除組
groupdel:刪除組
(1)、創建組distro,其GID為2016;
[root@test ~]# groupadd distro -g 2016
(2)、創建用戶mandriva, 其ID號為1005;基本組為distro;
[root@test ~]# groupadd distro -g 2016
(3)、創建用戶mageia,其ID號為1100,家目錄為/home/linux;
[root@test ~]# useradd mageia -u 1100 -d /home/linux
(4)、給用戶mageia添加密碼,密碼為mageedu;
[root@test ~]#echo "mageedu" | passwd –stdin mageia
(5)、刪除mandriva,但保留其家目錄;
[root@test ~]# userdel mandriva
(6)、創建用戶slackware,其ID號為2002,基本組為distro,附加組peguin;
[root@test ~]# groupadd peguin
[root@test ~]# useradd slackware -u 2002 -g distro -G peguin
(7)、修改slackware的默認shell為/bin/tcsh;
[root@test ~]#usermod -s /bin/tcsh slackware
(8)、為用戶slackware新增附加組admins;
[root@test ~]#groupadd admins
[root@test ~]#usermod -G admins slackware
(9)、為slackware添加密碼,且要求密碼最短使用期限為3天,最長為180天,警告為3天;
[root@test ~]passwd -n 3 -x 180 -w 3 slackware
(10)、添加用戶openstack,其ID號為3003, 基本組為clouds,附加組為peguin和nova;
[root@test ~]#groupadd clouds && groupadd nova
[root@test ~]#useradd openstack -u 3003 -g clouds -G peguin,nova
(11)、添加系統用戶mysql,要求其shell為/sbin/nologin;
[root@test ~]#useradd mysql -s /sbin/nologin
(12)、使用echo命令,非交互式為openstack添加密碼。
[root@test ~]#echo "password" | passwd –stdin openstack
原創文章,作者:lyj821202,如若轉載,請注明出處:http://www.www58058.com/63853