用戶及用戶組管理使用的練習
1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。
先使用who得出登陸用戶,通過uniq去重。
[root@mail ~]# who | grep -E -o "^[[:alnum:]]+" | uniq
root
2、取出最后登錄到當前系統的用戶的相關信息。
思路:首先使用who取得最后系統的登陸用戶,然后把結果通過命令過濾得到用戶名,最后通過ID命令顯示
方法1:
[root@mail ~]# id $( who | tail -n 1 | grep -E -o "^[[:alnum:]]+" )
uid=0(root) gid=0(root) 組=0(root)
方法2:用cut代替grep
[root@mail ~]# id $(who | tail -n 1 | cut -d' ' -f1 )
uid=0(root) gid=0(root) 組=0(root)
[root@mail ~]#
3、取出當前系統上被用戶當作其默認shell的最多的那個shell
思路:先把passwd的shell取出來通過管道送個uniq去重并計算,再通過sort排序,最后用head取出最高計次的結果。
方法1:
[root@mail ~]# grep -E "/*[[:alnum:]]+/*[[:alnum:]]+$" /etc/passwd -o | uniq -cd | sort | head -n 1
16 /sbin/nologin
方法2:
用cut替代grep
[root@mail ~]# cut -d: -f7 /etc/passwd | uniq -cd | sort | head -n 1
16 /sbin/nologin
4、將/etc/passwd中的第三個字段數值最大的后10個用戶的信息全部改為大寫后保存至/tmp/maxusers.txt文件中。
思路:首先用cut命令把1和3列提取出來,通過sort利用分隔符以數字形式排序第2列的內容,再通過tail得到最大數值的用戶,最后通過tr命令把小寫字母換成大寫并重定向/tmp/maxusers
[root@mail ~]# cut -d: -f1,3 /etc/passwd | sort -t: -k2 -n | tail -n 10 | cut -d: -f1 | tr [a-z] [A-Z] > /tmp/maxusers.txt
[root@mail ~]# cat /tmp/maxusers.txt //查看結果
5、取出當前主機的IP地址,提示:對ifconfig命令的結果進行切分。
思路:先使用grep過濾不需要的信息,然后通過cut命令進行切分,得出IP
ifconfig | grep "\<inet\>" | grep "addr:[[:digit:]]*.*" -o | cut -d' ' -f1 | cut -d: -f2
6、列出/etc目錄下所有以.conf結尾的文件的文件名,并將其名字轉換為大寫后保存至/tmp/etc.conf文件中。
思路:先找出文件然后通過tr命令轉換并重定向文件中
[root@mail ~]# ls /etc/*.conf | tr [a-z] [A-Z] > /tmp/etc.conf
[root@mail ~]# cat /tmp/etc.conf
7、顯示/var目錄下一級子目錄或文件的總個數。
思路:通過wc統計行即可
[root@mail ~]# ls /var -l | wc -l
19
8、取出/etc/group文件中第三個字段數值最小的10個組的名字。
思路:用cut分割先取出排序內容,通過sort排序最后用head取出前10行即可
[root@mail ~]# cut -d: -f1,3 /etc/group | sort -t: -n -k2 | head
9、將/etc/fstab和/etc/issue文件的內容合并為同一個內容后保存至/tmp/etc.test文件中。
思路:cat能同時打開多個文件,重定向到文件即可。
[root@mail ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
[root@mail ~]# cat /tmp/etc.test
10、請總結描述用戶和組管理類命令的使用方法并完成以下練習:
- 創建組distro,其GID為2016;
[root@mail ~]# groupadd -g 2016 distro
[root@mail ~]# cat /etc/group | grep distro
distro:x:2016:
- 創建用戶mandriva, 其ID號為1005;基本組為distro;
[root@mail ~]# useradd -u 1005 -g distro mandriva
[root@mail ~]# id mandriva
uid=1005(mandriva) gid=2016(distro) 組=2016(distro)
- 創建用戶mageia,其ID號為1100,家目錄為/home/linux;
[root@mail ~]# useradd -u 1100 -m -d /home/linux mageia
[root@mail ~]# cat /etc/passwd | grep mageia
mageia:x:1100:1100::/home/linux:/bin/bash
- 給用戶mageia添加密碼,密碼為mageedu;
[root@mail ~]# echo "mageedu" | passwd --stdin mageia
更改用戶 mageia 的密碼 。
passwd: 所有的身份驗證令牌已經成功更新。
5.刪除mandriva,但保留其家目錄;
[root@mail ~]# userdel mandriva
[root@mail ~]# ls /home/mandriva -d
/home/mandriva
- 創建用戶slackware,其ID號為2002,基本組為distro,附加組peguin;
[root@mail ~]# useradd -g distro -G peguin -u 2002 slackware
[root@mail ~]# cat /etc/passwd | grep slackware
slackware:x:2002:2016::/home/slackware:/bin/bash
[root@mail ~]# cat /etc/group | grep peguin
peguin:x:5001:gentoo,slackware
[root@mail ~]# cat /etc/group | grep distro
distro:x:2016:
- 修改slackware的默認shell為/bin/zsh;
[root@mail ~]# usermod -s /bin/zsh slackware
[root@mail ~]# cat /etc/passwd | grep slackware
slackware:x:2002:2016::/home/slackware:/bin/zsh
8.為用戶slackware新增附加組admins;
[root@mail ~]# groupadd admins
[root@mail ~]# usermod -G admins slackware
[root@mail ~]# cat /etc/group | grep admins
admins:x:5003:slackware
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/92651