N25_第三周作业_leon
1. 列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
[root@localhost ~]# who 全部
jj tty1 2016-12-30 09:09
root pts/0 2016-12-30 05:35 (192.168.73.1)
root pts/1 2016-12-30 09:08 (192.168.73.1)
root pts/2 2016-12-30 09:09 (192.168.73.1)
[root@localhost ~]# who | cut -d ' ' -f 1 取出第一列 用户名
jj
root
root
root
[root@localhost ~]# who | cut -d ' ' -f 1 | sort –u 去重
jj
root
[root@localhost ~]# who | cut -d ' ' -f 1 | sort -u |id 用户信息
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
2. 取出最后登录到当前系统的用户的相关信息。
3. 取出当前系统上被用户当作其默认shell的最多的那个shell。
[root@localhost ~]# cat /etc/passwd | cut -d : -f 7 取出系统上用户的默认shell
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync
/sbin/shutdown
/sbin/halt
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/bash
/bin/bash
/bin/bash
[root@localhost ~]# cat /etc/passwd | cut -d : -f 7 | uniq –c 在输出行前面加上每行出现的次数
1 /bin/bash
4 /sbin/nologin
1 /bin/sync
1 /sbin/shutdown
1 /sbin/halt
13 /sbin/nologin
3 /bin/bash
[root@localhost ~]# cat /etc/passwd | cut -d : -f 7 | uniq -c | sort –n 顺序排列
1 /bin/bash
1 /bin/sync
1 /sbin/halt
1 /sbin/shutdown
3 /bin/bash
4 /sbin/nologin
13 /sbin/nologin
[root@localhost ~]# cat /etc/passwd | cut -d : -f 7 | uniq -c | sort -n | tail -1 取出出现次数最多的shell
13 /sbin/nologin
4. 将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
[root@localhost ~]# cat /etc/passwd | sort -n -k 3 -t : 将第三个字段进行排序
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
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/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
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
jj:x:1000:1000::/home/jj:/bin/bash
kk:x:1001:1001::/home/kk:/bin/bash
ll:x:1002:1002::/home/ll:/bin/bash
[root@localhost ~]# cat /etc/passwd | sort -n -k 3 -t : | tail -10 取出最大的后10个
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
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
jj:x:1000:1000::/home/jj:/bin/bash
kk:x:1001:1001::/home/kk:/bin/bash
ll:x:1002:1002::/home/ll:/bin/bash
[root@localhost ~]# cat /etc/passwd | sort -n -k 3 -t : | tail -10 | tr 'a-z' 'A-Z' 大小写更改
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
AVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGIN
POLKITD:X:997:995:USER FOR POLKITD:/:/SBIN/NOLOGIN
SYSTEMD-NETWORK:X:998:996:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGIN
SYSTEMD-BUS-PROXY:X:999:997:SYSTEMD BUS PROXY:/:/SBIN/NOLOGIN
JJ:X:1000:1000::/HOME/JJ:/BIN/BASH
KK:X:1001:1001::/HOME/KK:/BIN/BASH
LL:X:1002:1002::/HOME/LL:/BIN/BASH
[root@localhost ~]# cat /etc/passwd | sort -n -k 3 -t : | tail -10 | tr 'a-z' 'A-Z' > /tmp/maxusers.txt 保存至文件
[root@localhost ~]# cat /tmp/maxusers.txt
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
AVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGIN
POLKITD:X:997:995:USER FOR POLKITD:/:/SBIN/NOLOGIN
SYSTEMD-NETWORK:X:998:996:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGIN
SYSTEMD-BUS-PROXY:X:999:997:SYSTEMD BUS PROXY:/:/SBIN/NOLOGIN
JJ:X:1000:1000::/HOME/JJ:/BIN/BASH
KK:X:1001:1001::/HOME/KK:/BIN/BASH
LL:X:1002:1002::/HOME/LL:/BIN/BASH
5. 取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
[root@localhost ~]# ifconfig IP地址
eth0 Link encap:Ethernet HWaddr 00:0C:29:82:D9:99
inet addr:192.168.73.3 Bcast:192.168.73.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe82:d999/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:31 errors:0 dropped:0 overruns:0 frame:0
TX packets:60 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4633 (4.5 KiB) TX bytes:9273 (9.0 KiB)
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:82:D9:99
inet addr:192.168.73.4 Bcast:192.168.73.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:306 (306.0 b) TX bytes:306 (306.0 b)
[root@localhost ~]# ifconfig | grep 'inet addr' 取出 inet addr行
inet addr:192.168.73.3 Bcast:192.168.73.255 Mask:255.255.255.0
inet addr:192.168.73.4 Bcast:192.168.73.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0
[root@localhost ~]# ifconfig | grep 'inet addr' | head -2 取出前两行IP地址
inet addr:192.168.73.3 Bcast:192.168.73.255 Mask:255.255.255.0
inet addr:192.168.73.4 Bcast:192.168.73.255 Mask:255.255.255.0
[root@localhost ~]# ifconfig | grep 'inet addr' | head -2 | cut -d : -f 2 切割第一次以冒号切割
192.168.73.3 Bcast
192.168.73.4 Bcast
[root@localhost ~]# ifconfig | grep 'inet addr' | head -2 | cut -d : -f 2 | cut -d ' ' -f 1 切割第二次以空格切割
192.168.73.3
192.168.73.4
6. 列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
[root@localhost tmp]# ls
vgauthsvclog.txt.0 vmware-root
[root@localhost tmp]# touch etc.conf 创建目标文件
[root@localhost tmp]# ls
etc.conf vgauthsvclog.txt.0 vmware-root
[root@localhost tmp]# find /etc/*.conf 找出/etc下以.conf结尾的文件
/etc/asound.conf
/etc/dracut.conf
/etc/fprintd.conf
/etc/gai.conf
/etc/grub.conf
/etc/host.conf
/etc/kdump.conf
/etc/krb5.conf
/etc/ld.so.conf
/etc/libaudit.conf
/etc/libuser.conf
/etc/logrotate.conf
/etc/mke2fs.conf
/etc/nsswitch.conf
/etc/ntp.conf
/etc/pm-utils-hd-apm-restore.conf
/etc/prelink.conf
/etc/readahead.conf
/etc/resolv.conf
/etc/rsyslog.conf
/etc/sestatus.conf
/etc/smartd.conf
/etc/sos.conf
/etc/sudo-ldap.conf
/etc/sysctl.conf
/etc/updatedb.conf
/etc/warnquota.conf
/etc/yum.conf
[root@localhost tmp]# find /etc/*.conf | tr 'a-z' 'A-Z' 转换大小写
/ETC/ASOUND.CONF
/ETC/DRACUT.CONF
/ETC/FPRINTD.CONF
/ETC/GAI.CONF
/ETC/GRUB.CONF
/ETC/HOST.CONF
/ETC/KDUMP.CONF
/ETC/KRB5.CONF
/ETC/LD.SO.CONF
/ETC/LIBAUDIT.CONF
/ETC/LIBUSER.CONF
/ETC/LOGROTATE.CONF
/ETC/MKE2FS.CONF
/ETC/NSSWITCH.CONF
/ETC/NTP.CONF
/ETC/PM-UTILS-HD-APM-RESTORE.CONF
/ETC/PRELINK.CONF
/ETC/READAHEAD.CONF
/ETC/RESOLV.CONF
/ETC/RSYSLOG.CONF
/ETC/SESTATUS.CONF
/ETC/SMARTD.CONF
/ETC/SOS.CONF
/ETC/SUDO-LDAP.CONF
/ETC/SYSCTL.CONF
/ETC/UPDATEDB.CONF
/ETC/WARNQUOTA.CONF
/ETC/YUM.CONF
[root@localhost tmp]# find /etc/*.conf | tr 'a-z' 'A-Z' | cut -d '/' -f 3 切割
ASOUND.CONF
DRACUT.CONF
FPRINTD.CONF
GAI.CONF
GRUB.CONF
HOST.CONF
KDUMP.CONF
KRB5.CONF
LD.SO.CONF
LIBAUDIT.CONF
LIBUSER.CONF
LOGROTATE.CONF
MKE2FS.CONF
NSSWITCH.CONF
NTP.CONF
PM-UTILS-HD-APM-RESTORE.CONF
PRELINK.CONF
READAHEAD.CONF
RESOLV.CONF
RSYSLOG.CONF
SESTATUS.CONF
SMARTD.CONF
SOS.CONF
SUDO-LDAP.CONF
SYSCTL.CONF
UPDATEDB.CONF
WARNQUOTA.CONF
YUM.CONF
[root@localhost tmp]# find /etc/*.conf | tr 'a-z' 'A-Z' | cut -d '/' -f 3 >> /tmp/etc.conf 保存到文件
[root@localhost tmp]# more /tmp/etc.conf
ASOUND.CONF
DRACUT.CONF
FPRINTD.CONF
GAI.CONF
GRUB.CONF
HOST.CONF
KDUMP.CONF
KRB5.CONF
LD.SO.CONF
LIBAUDIT.CONF
LIBUSER.CONF
–More–(35%)
7. 显示/var目录下一级子目录或文件的总个数
[root@localhost ~]# ls /var
account cache crash db empty games lib local lock log mail nis opt preserve run spool tmp yp
[root@localhost ~]# ls /var | wc -l
18
8. 取出/etc/group文件中第三个字段数值最小的10个组的名字
[root@localhost ~]# cat /etc/group 文件内容
root:x:0:
bin:x:1:bin,daemon
daemon:x:2:bin,daemon
sys:x:3:bin,adm
adm:x:4:adm,daemon
tty:x:5:
disk:x:6:
lp:x:7:daemon
mem:x:8:
kmem:x:9:
wheel:x:10:
mail:x:12:mail,postfix
uucp:x:14:
man:x:15:
games:x:20:
gopher:x:30:
video:x:39:
dip:x:40:
ftp:x:50:
lock:x:54:
audio:x:63:
nobody:x:99:
users:x:100:
dbus:x:81:
utmp:x:22:
utempter:x:35:
floppy:x:19:
vcsa:x:69:
abrt:x:173:
cdrom:x:11:
tape:x:33:
dialout:x:18:
haldaemon:x:68:haldaemon
ntp:x:38:
saslauth:x:76:
postdrop:x:90:
postfix:x:89:
avahi:x:70:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
sshd:x:74:
tcpdump:x:72:
slocate:x:21:
test:x:500:
[root@localhost ~]# cat /etc/group | sort -n -k 3 -t: 第三个字段排序
root:x:0:
bin:x:1:bin,daemon
daemon:x:2:bin,daemon
sys:x:3:bin,adm
adm:x:4:adm,daemon
tty:x:5:
disk:x:6:
lp:x:7:daemon
mem:x:8:
kmem:x:9:
wheel:x:10:
cdrom:x:11:
mail:x:12:mail,postfix
uucp:x:14:
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
slocate:x:21:
utmp:x:22:
gopher:x:30:
tape:x:33:
utempter:x:35:
ntp:x:38:
video:x:39:
dip:x:40:
ftp:x:50:
lock:x:54:
audio:x:63:
haldaemon:x:68:haldaemon
vcsa:x:69:
avahi:x:70:
tcpdump:x:72:
sshd:x:74:
saslauth:x:76:
dbus:x:81:
postfix:x:89:
postdrop:x:90:
nobody:x:99:
users:x:100:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
abrt:x:173:
test:x:500:
[root@localhost ~]# cat /etc/group | sort -n -k 3 -t: | head -10 取前10个
root:x:0:
bin:x:1:bin,daemon
daemon:x:2:bin,daemon
sys:x:3:bin,adm
adm:x:4:adm,daemon
tty:x:5:
disk:x:6:
lp:x:7:daemon
mem:x:8:
kmem:x:9:
[root@localhost ~]# cat /etc/group | sort -n -k 3 -t: | head -10 | cut -d : -f 1 切割名字
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 >> /tmp/etc.test
[root@localhost ~]# cat /tmp/etc.test
#
# /etc/fstab
# Created by anaconda on Mon Nov 14 22:36:22 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=e6060ca1-a25c-4cc4-8812-a87c6ee54a42 / ext4 defaults 1 1
UUID=3c00e0d6-9967-4a54-9d53-d936734183c1 /boot ext4 defaults 1 2
UUID=1973c173-2348-4ba2-89c2-7de18380bb5b 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.3 (Final)
Kernel \r on an \m
10. 请总结描述用户和组管理类命令的使用方法并完成以下练习
(1) 创建组distro,其GID为2016
[root@localhost ~]# groupadd distro -g 2016
[root@localhost ~]# tail /etc/group
postfix:x:89:
avahi:x:70:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
sshd:x:74:
tcpdump:x:72:
slocate:x:21:
test:x:500:
distro:x:2016:
(2) 创建用户mandriva,其ID号为1005;基本组为distro;
[root@localhost ~]# useradd mandriva -u 1005 -g distro
(3) 创建用户mageia,其ID号为1100,家目录为/home/linux
[root@localhost ~]# useradd mageia -u 1100 -d /home/linux
(4) 给用户mageia添加密码,密码为mageedu
[root@localhost ~]# passwd mageia
Changing password for user 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.
(5) 删除mandriva,但保留其家目录
[root@localhost ~]# userdel mandriva
(6) 创建用户slackware,其ID号为2002,基本组为distro,附加组peguin
[root@localhost ~]# useradd slackware -u 2002 -g distro -G peguin
useradd: group 'peguin' does not exist
[root@localhost ~]# groupadd peguin
[root@localhost ~]# useradd slackware -u 2002 -g distro -G peguin
[root@localhost ~]# tail /etc/passwd
slackware:x:2002:2016::/home/slackware:/bin/bash
(7) 修改slackware的默认 shell为bin/tcsh
[root@localhost ~]# usermod -s slackware /bin/tcsh
(8) 为用户slackware新增附加组admins
[root@localhost ~]# usermod slackware -a -G admins
(9) 为Slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天
[root@localhost ~]# echo "111111" | passwd –stdin slackware && chage -m 3 -M 180 -W 3 slackware
Changing password for user slackware.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# cat /etc/shadow | grep slackware
slackware:$6$ITrEbQG9$uZo2SBv8bDanYWEtZckZA9weAfKnVEvvkPIk6Lr7fFmB7yEZA1kMjqqVBBCkZ1VqP3B8EXmimPlY8TPJ48A7B0:17171:3:180:3:::
(10) 添加用户openstack,其ID号为3003,基本组为clouds,附件组为peguin和nova
[root@localhost ~]# useradd openstack -u 3003 -g clouds -G peguin,nova
(11) 添加系统用户mysql,要求其shell为/sbin/nologin
[root@localhost ~]# useradd -r mysql -s /sbin/nologin
(12) 使用echo命令,非交互式为openstack添加密码
[root@localhost ~]# echo "111" | passwd –stdin openstack
Changing password for user openstack.
passwd: all authentication tokens updated successfully.
原创文章,作者:leon,如若转载,请注明出处:http://www.178linux.com/65991