1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。
[root@localhost ~]# who test tty1 2016-07-11 23:32 root pts/0 2016-07-11 23:44 (10.3.71.179) [root@localhost ~]# who | cut -d" " -f1 | uniq test root
2、取出最后登錄到當前系統的用戶的相關信息。
[root@test1 ~]# who test tty1 2016-07-03 22:55 root pts/0 2016-07-03 22:53 (10.3.71.179) [root@test1 ~]# id $(last | head -1 | cut -d " " -f1) uid=501(test) gid=501(test) groups=501(test) [root@test1 ~]#
思路:最后登錄當前系統的用戶當作一個變量,用last列出目前與過去登入系統的用戶相關信息,head 取第一行,cut切出用戶名,最后id 這個變量也就是用戶名,列出相關信息
3、取出當前系統上被用戶當作其默認shell的最多的那個shell。
[root@test1 ~]# cut -d: -f7 /etc/passwd|uniq -c 1 /bin/bash 4 /sbin/nologin 1 /bin/sync 1 /sbin/shutdown 1 /sbin/halt 15 /sbin/nologin 5 /bin/bash 1 /sbin/nologin 1 /bin/bash [root@test1 ~]# cut -d: -f7 /etc/passwd|uniq -c |sort -n 1 /bin/bash 1 /bin/bash 1 /bin/sync 1 /sbin/halt 1 /sbin/nologin 1 /sbin/shutdown 4 /sbin/nologin 5 /bin/bash 15 /sbin/nologin [root@test1 ~]# cut -d: -f7 /etc/passwd|uniq -c |sort -n|tail -n 1 15 /sbin/nologin [root@test1 ~]#
思路:先用cut已:分割的第七段,統計次數,排序,列出倒數第一行
uniq -c 在輸出行前面加上每行在輸入文件中出現的次數。
4、將/etc/passwd中的第三個字段數值最大的后10個用戶的信息全部改為大寫后保存至/tmp/maxusers.txt文件中。
[root@test1 ~]# cat /etc/passwd |sort -t : -k 3 -n |tail -10 |tr 'a-z' 'A-Z' >/tmp/maxusers.txt [root@test1 ~]# cat /tmp/maxusers.txt DHCPD:X:177:177:DHCP SERVER:/:/SBIN/NOLOGIN SASLAUTH:X:499:76:"SASLAUTHD USER":/VAR/EMPTY/SASLAUTH:/SBIN/NOLOGIN OLDBOY:X:500:500::/HOME/OLDBOY:/BIN/BASH TEST:X:501:501::/HOME/TEST:/BIN/BASH BASH:X:502:502::/HOME/BASH:/BIN/BASH BASHER:X:503:503::/HOME/BASHER:/BIN/BASH TESTBASH:X:504:504::/HOME/TESTBASH:/BIN/BASH NOLOGIN:X:505:505::/HOME/NOLOGIN:/SBIN/NOLOGIN USER1:X:506:506::/HOME/USER1:/BIN/BASH NFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN [root@test1 ~]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin dhcpd:x:177:177:DHCP server:/:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin oldboy:x:500:500::/home/oldboy:/bin/bash test:x:501:501::/home/test:/bin/bash bash:x:502:502::/home/bash:/bin/bash basher:x:503:503::/home/basher:/bin/bash testbash:x:504:504::/home/testbash:/bin/bash nologin:x:505:505::/home/nologin:/sbin/nologin user1:x:506:506::/home/user1:/bin/bash
思路:把cat的結果用sort以:為分隔符的第三段按照數字排序,用tail 列出后10行,用tr替換大小寫,輸出到txt文檔
5、取出當前主機的IP地址,提示:對ifconfig命令的結果進行切分。
[root@test1 ~]# ifconfig | grep 'inet addr' |cut -d ':' -f2 10.3.71.183 Bcast 127.0.0.1 Mask [root@test1 ~]# ifconfig | grep 'inet addr' |cut -d ':' -f2 | head -n 1|cut -d ' ' -f1 10.3.71.183 [root@test1 ~]#
思路:在ifconfig命令顯示結果中查找 inet addr字符,用cut 切出來,用head列出第一行,在此用cut 切出IP地址
6、列出/etc目錄下所有以.conf結尾的文件的文件名,并將其名字轉換為大寫后保存至/tmp/etc.conf文件中。
[root@test1 ~]# ls /etc/*.conf |tr a-z A-Z > /tmp/etc.conf [root@test1 ~]# cat /tmp/etc.conf /ETC/DRACUT.CONF /ETC/GAI.CONF /ETC/GRUB.CONF /ETC/GSSAPI_MECH.CONF /ETC/HOST.CONF /ETC/IDMAPD.CONF /ETC/KRB5.CONF /ETC/LD.SO.CONF /ETC/LIBAUDIT.CONF /ETC/LIBUSER.CONF /ETC/LOGROTATE.CONF /ETC/MKE2FS.CONF /ETC/NFSMOUNT.CONF /ETC/NSSWITCH.CONF /ETC/REQUEST-KEY.CONF /ETC/RESOLV.CONF /ETC/RSYSLOG.CONF /ETC/SESTATUS.CONF /ETC/SUDO.CONF /ETC/SUDO-LDAP.CONF /ETC/SYSCTL.CONF /ETC/YUM.CONF [root@test1 ~]#
7、顯示/var目錄下一級子目錄或文件的總個數。
[root@test1 ~]# ls -ld /var/* | wc -l 19 [root@test1 ~]#
8、取出/etc/group文件中第三個字段數值最小的10個組的名字。
[root@test1 ~]# head /etc/group | cut -d ':' -f1 root bin daemon sys adm tty disk lp mem kmem [root@test1 ~]#
9、將/etc/fstab和/etc/issue文件的內容合并為同一個內容后保存至/tmp/etc.test文件中。
[root@test1 ~]# cat /etc/fstab /etc/issue > /tmp/etc.test [root@test1 ~]# cat /tmp/etc.test # # /etc/fstab # Created by anaconda on Tue May 10 14:39:35 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=08fbed64-cdd4-4ecd-b543-bbcea28461a2 / ext4 defaults 1 1 UUID=953b6e77-d528-4e8a-bc66-c79378e17b0d /boot ext4 defaults 1 2 UUID=414c094b-a9ae-4dc8-8653-12f8d79a09a9 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 CentOS release 6.5 (Final) Kernel \r on an \m [root@test1 ~]#
10、請總結描述用戶和組管理類命令的使用方法并完成以下練習:
用戶和組管理命令:
/etc/passwd :用戶賬號信息
1
2
3
|
各字段的含義 root:x:0:0:root: /root : /bin/bash 賬戶:密碼占位符:UID:GID:注釋信息:家目錄:用戶默認shell |
/etc/shadow :用戶密碼和賬號設定
1
2
|
root:$6$VfkLhLFa$va6OlLz /I5w .KdIbHEklKu8xC /iL84MgNcK86GeK769ATv27ngCpa8imV2CDGvY5Ec3bGzftSuLT .jcQB /dZ8 .:16882:0:99999:7::: 用戶名:加密的密碼:最近一次密碼修改的時間:最短密碼使用期限:最長密碼使用期限:密碼過期提前幾天警告:密碼非活動期限:賬號過期期限:保留區域 |
/etc/group :用戶組信息
1
2
|
adminuser:x:1003:natasha,harry 組名:組密碼占位符:GID:以逗號分隔屬于此組的用戶列表 |
/etc/gshadow:用戶組密碼
1
|
<br> |
命令總結:
useradd :創建用戶
-u : 指定uid
-g : 指定基本組
-G : 指定附加組
-s : 指定shell
-r : 創建系統用戶
groupad :創建組
-g gid : 指定gid
passwd : 更改密碼
–stdin :可不通過交互方式給用戶改密碼。
userdel : 刪除用戶
-r : 一并刪除用戶和家目錄
groupdel : 刪除組
usermod : usermod [option] username
-u :更改uid
-g :更改基本組
-G :更改附加組,通常與-a 一起使用追加附加組
-d :修改家目錄位置,加-m 可以把用戶原有文件遷移到新的家目錄中
-s : 更改shell
chage : 更改用戶密碼策略 chage [option] username
總結:
useradd, groupadd, su, id, usermod, userdel, groupmod, groupdel,
passwd, newgrp, pwck, gpasswd, chage, chsh, chfn, finger,chmod, chown, chgrp, umask
(1)、創建組distro,其GID為2016;
[root@test1 ~]# groupadd -g 2016 distro [root@test1 ~]# tail -l /etc/group rpcuser:x:29: nfsnobody:x:65534: oldboy:x:500: test:x:501: bash:x:502: basher:x:503: testbash:x:504: nologin:x:505: user1:x:506: distro:x:2016: [root@test1 ~]#
(2)、創建用戶mandriva, 其ID號為1005;基本組為distro;
[root@test1 ~]# useradd -u 1005 -g distro mandriva [root@test1 ~]# tail -l /etc/passwd rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin oldboy:x:500:500::/home/oldboy:/bin/bash test:x:501:501::/home/test:/bin/bash bash:x:502:502::/home/bash:/bin/bash basher:x:503:503::/home/basher:/bin/bash testbash:x:504:504::/home/testbash:/bin/bash nologin:x:505:505::/home/nologin:/sbin/nologin user1:x:506:506::/home/user1:/bin/bash mandriva:x:1005:2016::/home/mandriva:/bin/bash
(3)、創建用戶mageia,其ID號為1100,家目錄為/home/linux;
[root@test1 ~]# useradd -u 1100 -d /home/linux megeia [root@test1 ~]# tail -l /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin oldboy:x:500:500::/home/oldboy:/bin/bash test:x:501:501::/home/test:/bin/bash bash:x:502:502::/home/bash:/bin/bash basher:x:503:503::/home/basher:/bin/bash testbash:x:504:504::/home/testbash:/bin/bash nologin:x:505:505::/home/nologin:/sbin/nologin user1:x:506:506::/home/user1:/bin/bash mandriva:x:1005:2016::/home/mandriva:/bin/bash megeia:x:1100:1100::/home/linux:/bin/bash [root@test1 ~]#
(4)、給用戶mageia添加密碼,密碼為mageedu;
[root@test1 ~]# passwd mageia New password: BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple Retype new password: passwd: all authentication tokens updated successfully. [root@test1 ~]#
(5)、刪除mandriva,但保留其家目錄;
[root@test1 ~]# userdel mandriva [root@test1 ~]# cd /home [root@test1 home]# ls bash basher linux mandriva nologin oldboy test testbash user1 [root@test1 home]#
(6)、創建用戶slackware,其ID號為2002,基本組為distro,附加組peguin;
[root@test1 home]# useradd -u 2002 -g distro -G peguin slackware useradd: group 'peguin' does not exist [root@test1 home]# groupadd peguin #注意:附加組必須事先存在 [root@test1 home]# useradd -u 2002 -g distro -G peguin slackware [root@test1 home]# tail -l /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin oldboy:x:500:500::/home/oldboy:/bin/bash test:x:501:501::/home/test:/bin/bash bash:x:502:502::/home/bash:/bin/bash basher:x:503:503::/home/basher:/bin/bash testbash:x:504:504::/home/testbash:/bin/bash nologin:x:505:505::/home/nologin:/sbin/nologin user1:x:506:506::/home/user1:/bin/bash mageia:x:1100:1100::/home/linux:/bin/bash slackware:x:2002:2016::/home/slackware:/bin/bash [root@test1 home]# tail -l /etc/group oldboy:x:500: test:x:501: bash:x:502: basher:x:503: testbash:x:504: nologin:x:505: user1:x:506: distro:x:2016: mageia:x:1100: peguin:x:2017:slackware [root@test1 home]#
(7)、修改slackware的默認shell為/bin/tcsh;
[root@test1 ~]# usermod -s /bin/tcsh slackware [root@test1 ~]# tail -l /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin oldboy:x:500:500::/home/oldboy:/bin/bash test:x:501:501::/home/test:/bin/bash bash:x:502:502::/home/bash:/bin/bash basher:x:503:503::/home/basher:/bin/bash testbash:x:504:504::/home/testbash:/bin/bash nologin:x:505:505::/home/nologin:/sbin/nologin user1:x:506:506::/home/user1:/bin/bash mageia:x:1100:1100::/home/linux:/bin/bash slackware:x:2002:2016::/home/slackware:/bin/tcsh [root@test1 ~]#
(8)、為用戶slackware新增附加組admins;
[root@test1 ~]# usermod -G admins -a slackware [root@test1 ~]# tail -l /etc/group test:x:501: bash:x:502: basher:x:503: testbash:x:504: nologin:x:505: user1:x:506: distro:x:2016: mageia:x:1100: peguin:x:2017:slackware admins:x:2018:slackware [root@test1 ~]#
(9)、為slackware添加密碼,且要求密碼最短使用期限為3天,最長為180天,警告為3天;
[root@test1 ~]# passwd -n 3 -x 180 -w 3 slackware Adjusting aging data for user slackware. passwd: Success [root@test1 ~]#
(10)、添加用戶openstack,其ID號為3003, 基本組為clouds,附加組為peguin和nova;
[root@test1 ~]# useradd -u 3003 -g clouds -G peguin,nova openstack [root@test1 ~]# tail -l /etc/group testbash:x:504: nologin:x:505: user1:x:506: distro:x:2016: mageia:x:1100: peguin:x:2017:slackware,openstack admins:x:2018:slackware clouds:x:2019: noua:x:2020: nova:x:2021:openstack [root@test1 ~]# tail -l /etc/passwd oldboy:x:500:500::/home/oldboy:/bin/bash test:x:501:501::/home/test:/bin/bash bash:x:502:502::/home/bash:/bin/bash basher:x:503:503::/home/basher:/bin/bash testbash:x:504:504::/home/testbash:/bin/bash nologin:x:505:505::/home/nologin:/sbin/nologin user1:x:506:506::/home/user1:/bin/bash mageia:x:1100:1100::/home/linux:/bin/bash slackware:x:2002:2016::/home/slackware:/bin/tcsh openstack:x:3003:2019::/home/openstack:/bin/bash [root@test1 ~]#
(11)、添加系統用戶mysql,要求其shell為/sbin/nologin;
[root@test1 ~]# useradd -s /sbin/nologin -r mysql [root@test1 ~]# tail -l /etc/passwd test:x:501:501::/home/test:/bin/bash bash:x:502:502::/home/bash:/bin/bash basher:x:503:503::/home/basher:/bin/bash testbash:x:504:504::/home/testbash:/bin/bash nologin:x:505:505::/home/nologin:/sbin/nologin user1:x:506:506::/home/user1:/bin/bash mageia:x:1100:1100::/home/linux:/bin/bash slackware:x:2002:2016::/home/slackware:/bin/tcsh openstack:x:3003:2019::/home/openstack:/bin/bash mysql:x:498:498::/home/mysql:/sbin/nologin [root@test1 ~]#
(12)、使用echo命令,非交互式為openstack添加密碼。
[root@test1 ~]# echo 12345 | passwd --stdin openstack
原創文章,作者:zhutoyearn,如若轉載,請注明出處:http://www.www58058.com/23889
寫的很好,排版也很棒,加油