1.列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则显示一次即可。
# who | cut - d" " - f1 | uniq
root
2.取出最后登录到当前系统的用户的相关信息。
# last | tac | tail - n1
root pts/ 0 192.168 .121 .1 Thu Dec 6 07 : 23 still logged in
[ root@localhost ~ ] #
3.取出当前系统上被用户当作其默认shell的最多的那个shell.
[ root@localhost ~ ] # cut - d: - f7 / etc/ passwd | uniq - c | sort - n | tail - n 1
10 / sbin/ nologin
[ root@localhost ~ ] #
4.将/etc/passwd中的第三字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[ root@localhost ~ ] # cut - d: - f7 / etc/ passwd | uniq - c | sort - n | tail - n 1
10 / sbin/ nologin
[ root@localhost ~ ] # sort - n - t: - k3 / etc/ passwd | tail | tr ‘a- z’ ‘A - Z ’ > / tmp/ maxusers. txt
[ root@localhost ~ ] # cat / tmp/ maxusers. txt
SYSTEMD - NETWORK : X : 192 : 192 : SYSTEMD NETWORK MANAGEMENT : / : / SBIN / NOLOGIN
POLKITD : X : 999 : 998 : USER FOR POLKITD : / : / SBIN / NOLOGIN
LUOKUN : X : 1000 : 1000 : LUOKUN : / HOME / LUOKUN : / BIN / BASH
TCWY_LK : X : 1001 : 1001 : : / HOME / TCWY_LK : / BIN / BASH
CENTOS : X : 1002 : 1002 : : / HOME / CENTOS : / BIN / BASH
SLACKWARE : X : 1004 : 1004 : : / HOME / SLACKWARE : / BIN / TCSH
CLOUDSTACK : X : 1006 : 1005 : : / HOME / CLOUDSTACK : / BIN / BASH
GENTOO : X : 4001 : 4001 : : / HOME / GENTOO : / BIN / BASH
FEDORA : X : 4002 : 4002 : FEDORA CORE : / HOME / FEDORA : / BIN / TCSH
DOCKER : X : 4003 : 4003 : DOCKER , WHITE HOUSE , 911 , 110 : / HOME / DOCKER : / BIN / BASH
[ root@localhost ~ ] # 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
operator: x: 11 : 0 : operator: / root: / sbin/ nologin
games: x: 12 : 100 : games: / usr/ games: / sbin/ nologin
ftp: x: 14 : 50 : FTP User: / var / ftp: / sbin/ nologin
nobody: x: 99 : 99 : Nobody: / : / sbin/ nologin
systemd- network: x: 192 : 192 : systemd Network Management: / : / sbin/ nologin
dbus: x: 81 : 81 : System message bus: / : / sbin/ nologin
polkitd: x: 999 : 998 : User for polkitd: / : / sbin/ nologin
sshd: x: 74 : 74 : Privilege- separated SSH : / var / empty/ sshd: / sbin/ nologin
postfix: x: 89 : 89 : : / var / spool/ postfix: / sbin/ nologin
luokun: x: 1000 : 1000 : luokun: / home/ luokun: / bin/ bash
tcwy_lk: x: 1001 : 1001 : : / home/ tcwy_lk: / bin/ bash
centos: x: 1002 : 1002 : : / home/ centos: / bin/ bash
slackware: x: 1004 : 1004 : : / home/ slackware: / bin/ tcsh
cloudstack: x: 1006 : 1005 : : / home/ cloudstack: / bin/ bash
gentoo: x: 4001 : 4001 : : / home/ gentoo: / bin/ bash
fedora: x: 4002 : 4002 : Fedora Core: / home/ fedora: / bin/ tcsh
docker: x: 4003 : 4003 : Docker, white house, 911 , 110 : / home/ docker: / bin/ bash
[ root@localhost ~ ] #
5.取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[ root@localhost ~ ] # ifconfig ens33 | grep inet | cut - d" " - f 10
192.168 .121 .129
fe80: : 20 c: 29f f: fe3f: 7 b29
[ root@localhost ~ ] # ifconfig ens33 | grep inet | cut - d" " - f 10 | head - 1
192.168 .121 .129
[ root@localhost ~ ] #
6.列出/etc目录下所有以.conf的文件的文件名,并将其名字转换为大写后保存至tmp/etc.conf文件中。
[ root@localhost ~ ] # ls - l / etc
7.显示/var目录下一级子目录或文件的总个数。
[root@localhost ~]# ls /var | wc -l
22
[root@localhost ~]#
8.取出/etc/group文件中第三个字段数值最小的10个组的名字。
[ root@localhost ~ ] # sort - t: - k3 - n / etc/ group | head | cut - d: - f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
[ root@localhost ~ ] #
9.将/etc/fatab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[ root@localhost ~ ] # cat / tmp/ etc.
etc. conf etc. test etc. text
[ root@localhost ~ ] # cat / tmp/ etc. test
#
# / etc/ fstab
# Created by anaconda on Wed Sep 12 07 : 25 : 28 2018
#
# 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/ centos- root / xfs defaults 0 0
UUID= 313e5448 - f3e9- 44f 5 - bbe3- a7ad1c580864 / boot xfs defaults 0 0
/ dev/ mapper/ centos- swap swap swap defaults 0 0
\S
Kernel \r on an \m
[ root@localhost ~ ] #
10.请总结描述用户和组管理类命令的使用方法并完成以下练习
(1)、创建组distro,其GID为2016:
[ root@localhost ~ ] # cat / etc/ group | grep distro
distro: x: 5000 :
[ root@localhost ~ ] # groupadd - g 2016 distro
groupadd: group 'distro' already exists
[ root@localhost ~ ] # cat / etc/ group | grep distro
distro: x: 5000 :
[ root@localhost ~ ] # groupdel distro
[ root@localhost ~ ] # groupadd - g 2016 distro
[ root@localhost ~ ] # cat / etc/ group | grep distro
distro: x: 2016 :
[ root@localhost ~ ] #
(2)、创建用户mandriva,其ID号为1005;基本组为distro;
[ root@localhost ~ ] # useradd - g distro - u 1005 mandriva
[ root@localhost ~ ] # cat / etc/ pa
pam. d/ passwd passwd-
[ root@localhost ~ ] # cat / etc/ passwd | grep mandriva
mandriva: x: 1005 : 2016 : : / home/ mandriva: / bin/ bash
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[ root@localhost ~ ] # useradd - d / home/ linux - u 1100 mageia
[ root@localhost ~ ] # cat / etc/ passwd | grep mageia
mageia: x: 1100 : 1100 : : / home/ linux: / bin/ bash
[ root@localhost ~ ] #
(4)、给用户mageia添加密码,密码为mageedu;
[ root@localhost ~ ] # echo 'mageedu' | passwd -- stdin mageia
Changing password for user mageia.
passwd: all authentication tokens updated successfully.
[ root@localhost ~ ] # cat / etc/ passwd | grep mageia
mageia: x: 1100 : 1100 : : / home/ linux: / bin/ bash
[ root@localhost ~ ] # su - luokun
Last login: Sat Dec 1 19 : 35 : 11 CST 2018 on pts/ 1
[ luokun@localhost ~ ] $ su - mageia
Password:
[ mageia@localhost ~ ] $
(5)、删除mandriva,但保留其家目录;
[ root@localhost ~ ] # userdel mandriva
userdel: user 'mandriva' does not exist
[ root@localhost ~ ] # ll / home/ | mandriva
- bash: mandriva: command not found
[ root@localhost ~ ] # ll / home/ | grep mandriva
drwx-- -- -- . 2 1005 distro 62 Dec 6 08 : 21 mandriva
[ root@localhost ~ ] #
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
root@localhost ~ ] # groupadd peguin
[ root@localhost ~ ] # useradd - u 2002 - g distro - G peguin slackware
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists
[ root@localhost ~ ] # cat / etc/ passwd | grep slaclware
[ root@localhost ~ ] # cat / etc/ passwd | grep slackware
slackware: x: 2002 : 2016 : : / home/ slackware: / bin/ bash
[ root@localhost ~ ] # cat / etc/ group | grep slackware
peguin: x: 4004 : slackware
[ root@localhost ~ ] #
(7)、修改slackware的默认shell 为/bin/tcsh;
slackware: x: 2002 : 2016 : : / home/ slackware: / bin/ bash
[ root@localhost ~ ] # cat / etc/ group | grep slackware
peguin: x: 4004 : slackware
[ root@localhost ~ ] # usermod - s / bin/ tcsh slackware
[ root@localhost ~ ] # cat / etc/ passwd | grep slackware
slackware: x: 2002 : 2016 : : / home/ slackware: / bin/ tcsh
[ root@localhost ~ ] #
(8)、为用户slackware新增附加组admins;
[ root@localhost ~ ] # groupadd admins
\[ root@localhost ~ ] # usermod - G admins slackware
[ root@localhost ~ ] # cat / etc/ passwd | grep slackware
slackware: x: 2002 : 2016 : : / home/ slackware: / bin/ tcsh
[ root@localhost ~ ] # cat / etc/ group | grep slackware
admins: x: 4005 : slackware
[ root@localhost ~ ] #