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.178linux.com/23889
评论列表(1条)
写的很好,排版也很棒,加油