1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。
[root@localhost ~]# useradd tom [root@localhost ~]# echo "123456" | passwd --stdin tom …… [c:\~]$ ssh tom@192.168.0.101 …… [root@localhost ~]# who (unknown) :0 2017-01-20 07:24 (:0) root pts/0 2017-01-20 07:26 (192.168.0.103) tom pts/1 2017-01-20 08:07 (192.168.0.103) root pts/2 2017-01-20 08:08 (192.168.0.103) [root@localhost ~]# who | grep -o "^[[:alpha:]]\+\>" | sort -u root tom
2、取出最后登錄到當前系統的用戶的相關信息。
[root@localhost ~]# who | tail -n 1 | grep -o "^[[:alpha:]]\+\>" | id uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
3、取出當前系統上被用戶當作其默認shell的最多的那個shell。
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | sort | uniq -c | sort -n | tail -n 1 | grep -o "[/[:alpha:]]\+"
4、將/etc/passwd中的第三個字段數值最大的后10個用戶的信息全部改為大寫后保存至/tmp/maxusers.txt文件中。
[root@localhost ~]# cat /etc/passwd | sort -n -t: -k 3 | tail -n 10 | tr 'a-z' 'A-Z' > /tmp/maxusers.txt
5、取出當前主機的IP地址,提示:對ifconfig命令的結果進行切分。
[root@localhost ~]# ifconfig | grep -o "inet [\.0-9]\{7,15\}" | cut -d' ' -f 2 | sort -r | head -n 1
6、列出/etc目錄下所有以.conf結尾的文件的文件名,并將其名字轉換為大寫后保存至/tmp/etc.conf文件中。
[root@localhost ~]# find /etc -iname "*.conf" | tr 'a-z' 'A-Z' > /tmp/etc.conf
7、顯示/var目錄下一級子目錄或文件的總個數。
[root@localhost ~]# ls -A /var | wc -w
8、取出/etc/group文件中第三個字段數值最小的10個組的名字。
[root@localhost ~]# cat /etc/group | sort -n -t : -k 3 | head -n 10 | cut -d: -f1
9、將/etc/fstab和/etc/issue文件的內容合并為同一個內容后保存至/tmp/etc.test文件中。
[root@localhost ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
10、請總結描述用戶和組管理類命令的使用方法并完成以下練習:
用戶管理類命令有;useradd usermod userdel
useradd [options] LOGIN 創建新用戶
useradd -D 查看默認新用戶信息
useradd -D [options] 更新默認新用戶信息
usermod [options] LOGIN 更改用戶信息
userdel [options] LOGIN 刪除用戶賬號和相關文件
組管理類命令有:groupadd groupmod groupdel
groupadd [options] group 創建新組
groupmod [options] GROUP 修改組
groupdel [options] GROUP 刪除組
密碼修改
passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [–stdin] [username] 更新用戶認證信息
(1)、創建組distro,其GID為2016;
[root@localhost ~]# groupadd -g 2016 distro [root@localhost ~]# tail -n 1 /etc/group distro:x:2016:
(2)、創建用戶mandriva, 其ID號為1005;基本組為distro;
[root@localhost ~]# useradd -u 1005 -g distro mandrive [root@localhost ~]# id mandriva uid=1005(mandriva) gid=2016(distro) groups=2016(distro)
(3)、創建用戶mageia,其ID號為1100,家目錄為/home/linux;
[root@localhost ~]# useradd -u 1100 -d /home/linux mageia [root@localhost ~]# tail -n 1 /etc/passwd mageia:x:1100:1100::/home/linux:/bin/bash
(4)、給用戶mageia添加密碼,密碼為mageedu;
[root@localhost ~]# passwd mageia Changing password for user mageia. New password: Retype new password: passwd: all authentication tokens updated successfully.
(5)、刪除mandriva,但保留其家目錄;
[root@localhost ~]# userdel mandriva [root@localhost ~]# ls /home eric linux mandriva tom
(6)、創建用戶slackware,其ID號為2002,基本組為distro,附加組peguin;
[root@localhost ~]# groupadd peguin [root@localhost ~]# useradd -u 2002 -g distro -G peguin slackware [root@localhost ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2017(peguin)
(7)、修改slackware的默認shell為/bin/tcsh;
[root@localhost ~]# usermod -s /bin/tcsh slackware [root@localhost ~]# grep "^slackware\>" /etc/passwd slackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)、為用戶slackware新增附加組admins;
[root@localhost ~]# grep "^admins\>" /etc/group ; test $? -eq 0 || groupadd admins [root@localhost ~]# usermod -G admins slackware [root@localhost ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),2018(admins)
(9)、為slackware添加密碼,且要求密碼最短使用期限為3天,最長為180天,警告為3天;
[root@localhost ~]# passwd slackware Changing password for user slackware. New password: Retype new password: passwd: all authentication tokens updated successfully. [root@localhost ~]# passwd -n 3 -x 180 -w 3 slackware
(10)、添加用戶openstack,其ID號為3003, 基本組為clouds,附加組為peguin和nova;
[root@localhost ~]# grep "^nova\>" /etc/group; test $? -eq 0 || groupadd nova [root@localhost ~]# grep "^clouds\>" /etc/group; test $? -eq 0 || groupadd clouds [root@localhost ~]# useradd -u 3003 -g clouds -G "nova,peguin" openstack [root@localhost ~]# id openstack uid=3003(openstack) gid=2020(clouds) groups=2020(clouds),2017(peguin),2019(nova)
(11)、添加系統用戶mysql,要求其shell為/sbin/nologin;
[root@localhost ~]# useradd -r -s /sbin/nologin mysql
(12)、使用echo命令,非交互式為openstack添加密碼。
[root@localhost ~]# echo "subway@163.com" | passwd --stdin openstack Changing password for user openstack. passwd: all authentication tokens updated successfully.
原創文章,作者:和風細雨,如若轉載,請注明出處:http://www.www58058.com/66924
品質保持的不錯。