1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
1
2
3
4
5
6
7
8
|
[root@c6-1 ~] # who
root pts /0 2016-08-22 01:40 (10.3.20.100)
zdw pts /1 2016-08-22 01:45 (10.3.20.100)
zdw pts /2 2016-08-22 01:45 (10.3.20.100)
root pts /3 2016-08-22 01:46 (10.3.20.100)
[root@c6-1 ~] # who | cut -d' ' -f1 | sort -u
root zdw |
2、取出最后登录到当前系统的用户的相关信息。
1
2
|
[root@c6-1 ~] # last | head -1
root pts /3 10.3.20.100 Mon Aug 22 01:46 still logged in
|
3、取出当前系统上被用户当作其默认shell的最多的那个shell。
1
2
|
[root@c6-1 ~] # cat /etc/passwd | cut -d: -f7 | sort | uniq -c | sort -n | tail -1
15 /sbin/nologin
|
4、将/etc/passwd 中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@c6-1 ~] # sort -t: -k3 -n /etc/passwd | tail -10 | tr "a-z" "A-Z" > /tmp/maxusers.txt
[root@c6-1 ~] # cat /tmp/maxusers.txt
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
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
ZDW:X:500:500:: /HOME/ZDW : /BIN/BASH
|
5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
1
2
|
[root@c6-1 ~] # ifconfig | grep inet | cut -d: -f2 | cut -d' ' -f1 |head -1
10.3.20.80 |
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@c6-1 ~] # ls /etc/*.conf | tr "a-z" "A-Z" > /tmp/etc.conf
[root@c6-1 ~] # cat /tmp/etc.conf
/ETC/DRACUT .CONF
/ETC/GAI .CONF
/ETC/GRUB .CONF
/ETC/HOST .CONF
/ETC/KRB5 .CONF
/ETC/LD .SO.CONF
/ETC/LIBAUDIT .CONF
/ETC/LIBUSER .CONF
/ETC/LOGROTATE .CONF
/ETC/MKE2FS .CONF
/ETC/NSSWITCH .CONF
/ETC/RESOLV .CONF
/ETC/RSYSLOG .CONF
/ETC/SESTATUS .CONF
/ETC/SUDO .CONF
/ETC/SUDO-LDAP .CONF
/ETC/SYSCTL .CONF
/ETC/YUM .CONF
|
7、显示/var目录下一级子目录或文件的总个数。
1
2
|
[root@c6-1 ~] # ls /var/ | wc -l
16 |
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
1
2
3
4
5
6
7
8
9
10
11
|
[root@c6-1 ~] # sort -t: -k3 -n /etc/group | head -10 | cut -d: -f 1
root bin daemon sys adm tty disk lp mem kmem |
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@c6-1 ~] # cat /etc/{fstab,issue} > /tmp/etc.test
[root@c6-1 ~] # cat /tmp/etc.test
# # /etc/fstab # Created by anaconda on Mon Feb 29 01:31:34 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 # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=e880d9c0-5a7e-4e59-a49b-06488a6e4208 /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap 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 |
10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;
1
2
3
|
[root@c6-1 ~] # groupadd -g 2016 distro
[root@c6-1 ~] # tail -1 /etc/group
distro:x:2016: |
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
1
2
3
|
[root@c6-1 ~] # useradd -u 1005 -g distro mandriva
[root@c6-1 ~] # id mandriva
uid=1005(mandriva) gid=2016(distro) groups =2016(distro)
|
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
1
2
3
|
[root@c6-1 ~] # useradd -u 1100 -d /home/linux mageia
[root@c6-1 ~] # tail -1 /etc/passwd
mageia:x:1100:1100:: /home/linux : /bin/bash
|
(4)、给用户mageia添加密码,密码为mageedu;
1
2
3
|
[root@c6-1 ~] # echo "password" | passwd --stdin mageia
Changing password for user mageia.
passwd : all authentication tokens updated successfully.
|
(5)、删除mandriva,但保留其家目录;
1
2
3
4
5
|
[root@c6-1 ~] # userdel mandriva
[root@c6-1 ~] # ls /home/
linux mandriva zdw [root@c6-1 ~] # id mandriva
id : mandriva: No such user
|
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
1
2
3
4
|
[root@c6-1 ~] # groupadd peguin
[root@c6-1 ~] # useradd -u 2002 -g distro -G peguin slackware
[root@c6-1 ~] # id slackware
uid=2002(slackware) gid=2016(distro) groups =2016(distro),2017(peguin)
|
(7)、修改slackware的默认shell为/bin/tcsh;
1
2
3
|
[root@c6-1 ~] # usermod -s /bin/tcsh slackware
[root@c6-1 ~] # grep 'slackware' /etc/passwd
slackware:x:2002:2016:: /home/slackware : /bin/tcsh
|
(8)、为用户slackware新增附加组admins;
1
2
3
4
|
[root@c6-1 ~] # groupadd admins
[root@c6-1 ~] # usermod -a -G admins slackware
[root@c6-1 ~] # id slackware
uid=2002(slackware) gid=2016(distro) groups =2016(distro),2017(peguin),2018(admins)
|
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@c6-1 ~] # echo "123456" | passwd --stdin slackware
Changing password for user slackware.
passwd : all authentication tokens updated successfully.
[root@c6-1 ~] # passwd -n 3 -x 180 -w 3 slackware
Adjusting aging data for user slackware.
passwd : Success
[root@c6-1 ~] # chage -l slackware
Last password change : Aug 21, 2016 Password expires : Feb 17, 2017 Password inactive : never Account expires : never Minimum number of days between password change : 3 Maximum number of days between password change : 180 Number of days of warning before password expires : 3 |
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
1
2
3
4
5
|
[root@c6-1 ~] # groupadd clouds
[root@c6-1 ~] # groupadd nova
[root@c6-1 ~] # useradd -u 3003 -g clouds -G peguin,nova openstack
[root@c6-1 ~] # id openstack
uid=3003(openstack) gid=2019(clouds) groups =2019(clouds),2017(peguin),2020(nova)
|
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
1
2
3
|
[root@c6-1 ~] # useradd -s /sbin/nologin mysql
[root@c6-1 ~] # grep 'mysql' /etc/passwd
mysql:x:3004:3004:: /home/mysql : /sbin/nologin
|
(12)、使用echo命令,非交互式为openstack添加密码。
1
2
3
|
[root@c6-1 ~] # echo "password" | passwd --stdin openstack
Changing password for user openstack.
passwd : all authentication tokens updated successfully.
|