1.列出當前系統上所有已登錄用戶的用戶名,同一個用戶登錄,則只顯示一次
[root@localhost ~]# who root tty1 2016-08-23 06:20 root pts/0 2016-08-26 05:09 (172.16.16.201) user1 pts/1 2016-08-26 05:11 (172.16.16.201) [root@localhost ~]# who | cut -d' ' -f1 | uniq root user1
2.取出最后登錄到當前系統的用戶的相關信息
[user1@localhost ~]$ last | head -1 user1 pts/1 172.16.16.201 Fri Aug 26 05:36 still logged in
3.取出當前系統上被用戶當做其默認shell最多的那個shell
# 取/etc/passwd中的第7字段排序并計數 [root@localhost ~]# cut -d: -f7 /etc/passwd | sort | uniq -c 2 /bin/bash 4 /bin/sh 1 /bin/sync 1 /sbin/halt 15 /sbin/nologin 1 /sbin/shutdown # 以數值排序 [root@localhost ~]# cut -d: -f7 /etc/passwd | sort | uniq -c | sort -n 1 /bin/sync 1 /sbin/halt 1 /sbin/shutdown 2 /bin/bash 4 /bin/sh 15 /sbin/nologin # 取最后一行的shell # 最終結果 [root@localhost ~]# cut -d: -f7 /etc/passwd | sort | uniq -c | sort -n | tail -1 | grep -o [^[:space:]]*$ /sbin/nologin
4.將/etc/passwd中的第三個字段數值最大的后10個用戶的信息全部改為大寫后保存至/tmp/maxusers.txt文件中
# 把/etc/passwd文件以冒號分隔各字段以第3字段數值大小排序,取后10行 [root@localhost ~]# sort -n -t: -k3 /etc/passwd | tail vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin user1:x:500:500::/home/user1:/bin/bash gentoo:x:4001:4001::/home/gentoo:/bin/sh fedora:x:4002:4002:Fedora Core:/home/fedora:/bin/sh user2:x:4003:4003::/home/user2:/bin/sh user3:x:4004:4004::/home/user3:/bin/sh # 把上述結果通過管道送給命令“tr”變為大寫,重定向至/tmp/users.txt即可 # 最終結果 [root@localhost ~]# sort -n -t: -k3 /etc/passwd | tail | tr 'a-z' 'A-Z' > /tmp/maxusers.txt
5.對命令ifconfig的結果切分,取出當前主機的IP地址
# 取出命令“ifconfig”結果中與addr相連的地址 [root@localhost ~]# ifconfig | grep -E -o "addr:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" addr:172.16.16.1 addr:127.0.0.1 # 管道送給命令“cut”,取出地址 # 最終結果 [root@localhost ~]# ifconfig | grep -E -o "addr:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | cut -d: -f2 172.16.16.1127.0.0.1
6.列出/etc目錄下所有以.conf結尾的文件的文件名,并將其名字轉換為大寫后保存至/tmp/etc.conf中
[root@localhost ~]# ls /etc/*.conf | tr 'a-z' 'A-Z' > /tmp/etc.conf
7.顯示/var目錄下,一級子目錄或文件的總個數
[root@localhost ~]# ls /var | wc -l19
8.取出/etc/group文件中第3個字段數值最小的10個組的名字
# 第3字段按數值排序,取前10行 [root@localhost ~]# sort -t: -k3 -n /etc/group | head root:x:0: bin:x:1:bin,daemon daemon:x:2:bin,daemon sys:x:3:bin,adm adm:x:4:adm,daemon tty:x:5: disk:x:6: lp:x:7:daemon mem:x:8: kmem:x:9: # 通過管道送給“cut”,取組名 # 最終結果 [root@localhost ~]# sort -t: -k3 -n /etc/group | head | cut -d: -f1 root bin daemon sys adm tty disk lp mem kmem
9.將/etc/fstab和/etc/issue文件的內容合并為同一個內容后保存至/tmp/etc.test文件中
[root@localhost ~]# cat /etc/fstab > /tmp/etc.test [root@localhost ~]# cat /etc/issue >> /tmp/etc.test
10.用戶、組管理類命令練習
10.1 創建組distro,其GID為2016
[root@localhost ~]# tail /etc/group [root@localhost ~]# tail -1 /etc/group distro:x:2016:
10.2 創建用戶mandriva,其ID號為1005;基本組為distro
[root@localhost ~]# useradd -u 1005 -g distro mandriva [root@localhost ~]# id mandriva uid=1005(mandriva) gid=2016(distro) groups=2016(distro)
10.3 創建用戶mageia,其ID號為1100,家目錄為/home/linux
[root@localhost ~]# useradd -u 1100 -d /home/linux mageia [root@localhost ~]# tail -1 /etc/passwd mageia:x:1100:1100::/home/linux:/bin/sh
10.4 給用戶mageia添加密碼,密碼為mageedu
[root@localhost ~]# echo "mageedu" | passwd --stdin mageia Changing password for user mageia. passwd: all authentication tokens updated successfully.
10.5 刪除mandriva,但保留其家目錄
直接刪除用戶mandriva即可,因為默認家目錄是保留的。
[root@localhost ~]# userdel mandriva [root@localhost ~]# ls /home fedora gentoo linux lost+found mandriva user1 user2 user3
10.6 創建用戶slackware,其ID號為2002,基本組為distro,附加組為peguin
[root@localhost ~]# useradd -u 2002 -g distro -G peguin slackware [root@localhost ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),5001(peguin)
10.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
10.8 為用戶slackware新增附加組admins
[root@localhost ~]# groupadd admins [root@localhost ~]# usermod -a -G admins slackware [root@localhost ~]# id slackware uid=2002(slackware) gid=2016(distro) groups=2016(distro),5001(peguin),5003(admins)
10.9 為slackware添加密碼,且要求密碼最短使用期限為3天,最長為180天,警告為3天
[root@localhost ~]# echo "slackware" | passwd --stdin slackware Changing password for user slackware. passwd: all authentication tokens updated successfully. [root@localhost ~]# grep ^slackware /etc/shadow | cut -d: -f4-6 3:180:3
10.10 添加用戶openstack,其ID號為3003,基本組為clouds,附加組為peguin和nova
[root@localhost ~]# useradd -u 3003 -g clouds -G peguin openstack [root@localhost ~]# usermod -a -G nova openstack [root@localhost ~]# id openstack uid=3003(openstack) gid=5004(clouds) groups=5004(clouds),5001(peguin),5005(nova)
10.11 添加系統用戶mysql,要求其shell為/sbin/nologin
[root@localhost ~]# useradd -r -s /sbin/nologin mysql [root@localhost ~]# tail -1 /etc/passwd mysql:x:498:498::/home/mysql:/sbin/nologin
10.12 使用echo命令,非交互式為openstack添加密碼
[root@localhost ~]# echo "openstack" | passwd --stdin openstack Changing password for user openstack. passwd: all authentication tokens updated successfully.
原創文章,作者:wangzhenyu177,如若轉載,請注明出處:http://www.www58058.com/47648