1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
答:
[root@localhost ~]# who
root tty1 2016-12-07 00:24
root pts/0 2016-12-13 10:06 (192.168.1.105)
[root@localhost ~]# who | cut -d' ' -f1 | sort -u
root
2、取出当前系统上被用户当作其默认shell的最多的那个shell。
答:
[root@localhost ~]# cat /etc/passwd | cut -d: -f7 | sort | uniq -c | sort -n | tail -1
17 /sbin/nologin
3、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
答:
[root@localhost ~]# sort -t: -k3 -n /etc/passwd | tail -10 | tr "a-z" "A-Z" > /tmp/maxusers.txt[root@localhost ~]# cat /tmp/maxusers.txt
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
4、取出当前主机的IP地址.
答:
[root@localhost ~]# ifconfig | grep inet | cut -d: -f2 | cut -d' ' -f10 | head -1
192.168.1.10
5、显示/var目录下一级子目录或文件的总个数。
答:
[root@localhost ~]# ls /var/ | wc -l
21
6、取出/etc/group文件中第三个字段数值最小的10个组的名字。
答:
[root@localhost ~]# sort -t: -k3 -n /etc/group | head -10 | cut -d: -f 1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
7、将/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 Jun 20 21:48:15 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=6a04eed5-dda0-4745-9afa-202cad0f80a2 / xfs defaults 0 0
UUID=09871ed2-ad0c-4e18-bf1d-7d69575e0631 /boot xfs defaults 0 0
UUID=cb6118a4-3685-4414-b3ac-a25e5231cb41 swap swap defaults 0 0
\S
Kernel \r on an \m
8、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;
[root@localhost ~]# groupadd -g 2016 distro
[root@localhost ~]# tail -1 /etc/group
(2)、创建用户mandriva, 其ID号为1005;基本组为distro;
[root@localhost ~]# useradd -u 1005 -g distro mandriva
[root@localhost ~]# id mandriva
uid=1005(mandriva) gid=2016(distro) 组=2016(distro)
(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
useradd -u 1100 -d /home/linux mageia
[root@localhost ~]# tail -1 /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash
(4)、给用户mageia添加密码,密码为mageedu;
[root@localhost ~]# echo "mageedu" | passwd --stdin mageia
更改用户 mageia 的密码 。
passwd:所有的身份验证令牌已经成功更新。
(5)、删除mandriva,但保留其家目录;
[root@localhost ~]# userdel mandriva
[root@localhost ~]# ls /home/
linux mandriva
[root@localhost ~]# id mandriva
id: mandriva: no such user
(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@localhost ~]# groupadd peguin
[root@localhost ~]# useradd -u 2002 -g distro -G peguin slackware
[root@localhost ~]# id slackware
uid=2002(slackware) gid=2016(distro) 组=2016(distro),2017(peguin)
(7)、修改slackware的默认shell为/bin/tcsh;
[root@localhost ~]# usermod -s /bin/tcsh slackware
[root@localhost ~]# grep "slackware" /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) 组=2016(distro),2017(peguin),2018(admins)
(9)、为slackware添加密码,且要求密码最短使用期限为3天,最长为180天,警告为3天;
[root@localhost ~]# echo "123456" | passwd --stdin slackware
更改用户 slackware 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@localhost ~]# passwd -n 3 -x 180 -w 3 slackware
调整用户密码老化数据slackware。
passwd: 操作成功
[root@localhost ~]# chage -l slackware
最近一次密码修改时间 :12月 13, 2016
密码过期时间 :6月 11, 2017
密码失效时间 :从不
帐户过期时间 :从不
两次改变密码之间相距的最小天数 :3
两次改变密码之间相距的最大天数 :180
在密码过期之前警告的天数 :3
(10)、添加用户openstack,其ID号为3003, 基本组为clouds,附加组为peguin和nova;
[root@localhost ~]# groupadd clouds
[root@localhost ~]# groupadd nova
[root@localhost ~]# useradd -u 3003 -g clouds -G peguin,nova openstack
[root@localhost ~]# id openstack
uid=3003(openstack) gid=2019(clouds) 组=2019(clouds),2017(peguin),2020(nova)
(11)、添加系统用户mysql,要求其shell为/sbin/nologin;
[root@localhost ~]# useradd mysql -s /sbin/nologin
[root@localhost ~]# grep "mysql" /etc/passwd
mysql:x:3004:3004::/home/mysql:/sbin/nologin
(12)、使用echo命令,非交互式为openstack添加密码。
[root@localhost ~]# echo "1234567" | passwd --stdin openstack
更改用户 openstack 的密码 。
passwd:所有的身份验证令牌已经成功更新。
9、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。
答:
[root@localhost ~]# cp -r /etc/skel/ /home/tuser1
[root@localhost ~]# ls -ld /home/tuser1/
drwxr-xr-x. 2 root root 59 12月 13 11:16 /home/tuser1/
[root@localhost ~]# ll -d /home/tuser1/
drwx------. 2 root root 59 12月 13 11:16 /home/tuser1/
[root@localhost ~]# ll -a /home/tuser1/
总用量 12
drwx------. 2 root root 59 12月 13 11:16 .
drwxr-xr-x. 8 root root 90 12月 13 11:16 ..
-rwx------. 1 root root 18 12月 13 11:16 .bash_logout
-rwx------. 1 root root 193 12月 13 11:16 .bash_profile
-rwx------. 1 root root 231 12月 13 11:16 .bashrc
10、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;
答:
方法一:
[root@localhost ~]# grep ^[sS] /proc/meminfo
SwapCached: 0 kB
SwapTotal: 1048572 kB
SwapFree: 1048572 kB
Shmem: 4568 kB
Slab: 55240 kB
SReclaimable: 21528 kB
SUnreclaim: 33712 kB
方法二:
[root@localhost ~]# grep -i ^s /proc/meminfo
SwapCached: 0 kB
SwapTotal: 1048572 kB
SwapFree: 1048572 kB
Shmem: 4568 kB
Slab: 55240 kB
SReclaimable: 21528 kB
SUnreclaim: 33712 kB
11、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户。
答:
[root@localhost ~]# grep -v “/sbin/nologin$” /etc/passwd | cut -d: -f1
root
sync
shutdown
halt
mageia
slackware
openstack
12、显示/etc/passwd文件中其默认shell为/bin/bash的用户;
答:
[root@localhost ~]# grep "/bin/bash$" /etc/passwd | cut -d: -f1
root
mageia
openstack
13、找出/etc/passwd文件中的一位数或两位数;
答:
[root@localhost ~]# grep "\<[0-9]\{1,2\}\>" /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
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
14、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
答:
[root@localhost ~]# grep "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}" /etc/rc.d/rc.sysinit
# /etc/rc.d/rc.sysinit - run once at boot time
# Taken in part from Miquel van Smoorenburg's bcheckrc.
# Check SELinux status
# Print a text banner.
# Only read this once.
# Initialize hardware
# Set default affinity
# Load other user-defined modules
# Load modules (for backward compatibility with VARs)
# Configure kernel parameters
# Set the hostname.
# Sync waiting for storage.
# Device mapper & related initialization
# Start any MD RAID arrays that haven't been started yet
# Remount the root filesystem read-write.
# Clean up SELinux labels
# If relabeling, relabel mount points.
# Mount all other filesystems (except for NFS and /proc, which is already
# mounted). Contrary to standard usage,
# filesystems are NOT unmounted in single user mode.
# The 'no' applies to all listed filesystem types. See mount(8).
# Update quotas if necessary
# Check to see if a full relabel is needed
# Initialize pseudo-random number generator
# Configure machine if necessary.
# Clean out /.
# Do we need (w|u)tmpx files? We don't set them up, but the sysadmin might...
# Clean up /var.
# Clean up utmp/wtmp
# Clean up various /tmp bits
# Make ICE directory
# Start up swapping.
# Set up binfmt_misc
# Boot time profiles. Yes, this should be somewhere else.
# Now that we have all of our basic modules loaded and the kernel going,
# let's dump the syslog ring somewhere so we can find it later
# create the crash indicator flag to warn on crashes, offer fsck with timeout
# Let rhgb know that we're leaving rc.sysinit
15、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;
答:
[root@localhost ~]# netstat -tan | grep "LISTEN[[:space:]]*$"
tcp 0 0 0.0.0.0:58370 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::111 :::* LISTEN
tcp 0 0 :::56211 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:631 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
16、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;
答:
[root@localhost ~]# useradd bash
[root@localhost ~]# useradd testbash
[root@localhost ~]# useradd basher
[root@localhost ~]# useradd -s /sbin/nologin nologin
[root@localhost ~]# grep "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:3005:3005::/home/bash:/bin/bash
nologin:x:3008:3008::/home/nologin:/sbin/nologin