1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[root@localhost ~]# who
root pts/0 2017-12-16 22:46 (192.168.43.233)
root pts/1 2017-12-16 22:46 (192.168.43.233)
root pts/2 2017-12-16 22:46 (192.168.43.233)
root pts/3 2017-12-16 22:46 (192.168.43.233)
[root@localhost ~]# who | cut -d” ” -f1
root
root
root
root
[root@localhost ~]# who | cut -d” ” -f1 | sort
root
root
root
root
[root@localhost ~]# who | cut -d” ” -f1 | sort | uniq
root
[root@localhost ~]# who | cut -d” ” -f1 | uniq
root
2、取出最后登录到当前系统的用户的相关信息。
[root@localhost ~]# who
root pts/0 2017-12-16 22:46 (192.168.43.233)
root pts/1 2017-12-16 22:46 (192.168.43.233)
root pts/2 2017-12-16 22:46 (192.168.43.233)
root pts/3 2017-12-16 22:46 (192.168.43.233)
[root@localhost ~]# who | tail -1
root pts/3 2017-12-16 22:46 (192.168.43.233)
[root@localhost ~]# who | tail -1 | cut -d” ” -f1 | id
uid=0(root) gid=0(root) groups=0(root)
3、取出当前系统上被用户当作其默认shell的最多的那个shell。
[root@localhost ~]# cat /etc/passwd | cut -d: -f7
/bin/bash
……
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | uniq -c
1 /bin/bash
4 /sbin/nologin
1 /bin/sync
1 /sbin/shutdown
1 /sbin/halt
10 /sbin/nologin
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | uniq -c | sort -nr
10 /sbin/nologin
4 /sbin/nologin
1 /sbin/shutdown
1 /sbin/halt
1 /bin/sync
1 /bin/bash
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | uniq -c | sort -nr | head -1
10 /sbin/nologin
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | uniq -c | tail -1
10 /sbin/nologin
4、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[root@localhost ~]# sort -t: -k3 -n /etc/passwd
[root@localhost ~]# sort -t: -k3 -n /etc/passwd | tail -10 | tr ‘a-z’ ‘A-Z’
MAIL:X:8:12:MAIL:/VAR/SPOOL/MAIL:/SBIN/NOLOGIN
……
[root@localhost ~]# sort -t: -k3 -n /etc/passwd | tail -10 | tr ‘a-z’ ‘A-Z’ > /tmp/maxusers.txt
[root@localhost ~]# cat /tmp/maxusers.txt
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
SSHD:X:74:74:PRIVILEGE-SEPARATED SSH:/VAR/EMPTY/SSHD:/SBIN/NOLOGIN
DBUS:X:81:81:SYSTEM MESSAGE BUS:/:/SBIN/NOLOGIN
POSTFIX:X:89:89::/VAR/SPOOL/POSTFIX:/SBIN/NOLOGIN
NOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGIN
SYSTEMD-NETWORK:X:192:192:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGIN
POLKITD:X:999:997:USER FOR POLKITD:/:/SBIN/NOLOGIN
5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[root@localhost ~]# ifconfig ens33 | egrep -o ‘(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.?){4}’
192.168.43.191
255.255.255.0
192.168.43.255
[root@localhost ~]# ifconfig ens33 | egrep -o ‘(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.?){4}’ | head -1
192.168.43.191
6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[root@localhost ~]# find /etc -name “*.conf” | tr ‘a-z’ ‘A-Z’ > /tmp/etc.conf
[root@localhost ~]# cat /tmp/etc.conf
/ETC/OPENLDAP/LDAP.CONF
……
7、显示/var目录下一级子目录或文件的总个数。
[root@localhost ~]# ls /var | wc -l
8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
[root@localhost ~]# sort -t: -k3 -n /etc/group | head
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
[root@localhost ~]# sort -t: -k3 -n /etc/group | head | cut -d: -f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
[root@localhost ~]# cat /etc/fstab /etc/issue
#
# /etc/fstab
# Created by anaconda on Thu Nov 30 17:46:50 2017
#
# 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=c7d8f391-9c0a-4a23-8b2f-f203b303d7c5 / ext4 defaults 1 1
UUID=8c4f6d0e-5c4d-4971-b776-7728ffa0a791 /boot ext4 defaults 1 2
UUID=601d68f6-bf3c-4e01-9e03-c000bce30829 swap swap defaults 0 0
\S
Kernel \r on an \m
[root@localhost ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
[root@localhost ~]# cat /tmp/etc.test
#
# /etc/fstab
# Created by anaconda on Thu Nov 30 17:46:50 2017
#
# 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=c7d8f391-9c0a-4a23-8b2f-f203b303d7c5 / ext4 defaults 1 1
UUID=8c4f6d0e-5c4d-4971-b776-7728ffa0a791 /boot ext4 defaults 1 2
UUID=601d68f6-bf3c-4e01-9e03-c000bce30829 swap swap defaults 0 0
\S
Kernel \r on an \m
10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
练习1:创建用户gentoo,UID为4001,基本组为gentoo,附加组为distro(GID为5000)和peguin(GID为5001);
[root@localhost ~]# groupadd -g 5000 distro
[root@localhost ~]# groupadd -g 5001 penguin
[root@localhost ~]# useradd gentoo -u 4001 -G distro,penguin
[root@localhost ~]# tail -1 /etc/passwd
gentoo:x:4001:4001::/home/gentoo:/bin/bash
[root@localhost ~]# tail -3 /etc/group
distro:x:5000:gentoo
penguin:x:5001:gentoo
gentoo:x:4001:
练习2:创建用户fedora,其注释信息为”Fedora Core”,默认shell为/bin/tcsh;
[root@localhost ~]# useradd fedora -s /bin/tcsh -c “Fedora Core”
[root@localhost ~]# tail -1 /etc/passwd
fedora:x:4002:4002:Fedora Core:/home/fedora:/bin/tcsh
练习3:修改gentoo用户的家目录为/var/tmp/gentoo;要求其原有文件仍能被用户访问;
[root@localhost ~]# usermod -d /var/tmp/gentoo -m gentoo
练习4:为gentoo新增附加组netadmin;
[root@localhost ~]# groupadd netadmin
[root@localhost ~]# usermod -a -G netadmin gentoo
[root@localhost ~]# id gentoo
uid=4001(gentoo) gid=4001(gentoo) groups=4001(gentoo),5000(distro),5001(penguin),5002(netadmin)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(1)、创建组distro,其GID为2016;
[root@localhost ~]# groupadd -g 2016 distro
[root@localhost ~]# tail -1 /etc/group
distro:x:2016:
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[root@localhost ~]# useradd mandriva -u 1005 -g distro
[root@localhost ~]# id mandriva
uid=1005(mandriva) gid=2016(distro) groups=2016(distro)
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@localhost ~]# useradd mageia -u 1100 -d /home/linux
[root@localhost ~]# tail -n 1 /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash
(4)、给用户mageia添加密码,密码为mageedu;
[root@localhost ~]# echo “mageedu” | passwd –stdin mageia
Changing password for user mageia.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# tail -1 /etc/shadow
mageia:$6$p2GTrIVS$lsnXrakfyBgdIKKCtnZH5hhDr4MNOik/pZDRiOXtP5hG3g.qR0A2dKXDTUj8vvgdKi.7ARnDWMeD99V3CrjmA1:17516:0:99999:7:::
(5)、删除mandriva,但保留其家目录;
[root@localhost ~]# userdel mandriva
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@localhost ~]# groupadd penguin
[root@localhost ~]# useradd slackware -u 2002 -g distro -G penguin
[root@localhost ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),5001(penguin)
(7)、修改slackware的默认shell为/bin/tcsh;
[root@localhost ~]# usermod slackware -s /bin/tcsh
[root@localhost ~]# tail -1 /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/tcsh
(8)、为用户slackware新增附加组admins;
[root@localhost ~]# groupadd admins
[root@localhost ~]# usermod -a -G admins slackware
[root@localhost ~]# id slackware
uid=2002(slackware) gid=2016(distro) groups=2016(distro),5001(penguin),5003(admins)
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/90189
评论列表(1条)
赞~实验步骤的每一步都描述出来,对学习理解有很大帮助~中间的脑图总结的知识点也非常到位,非常赞的一篇~加油