CentOS 7 用户以及组管理常见解决问题

Listen

1.列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录对此,则只显示一次即可

[root@bogon ~]# who | cut -d’  ‘ -f1|uniq
root

2.取出最后登录到当前系统的用户的相关信息

[root@localhost ~]# w
04:26:19 up 5:21, 1 user, load average: 0.08, 0.02, 0.01
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.1.113 23:05 0.00s 0.21s 0.06s w

3.取出当前系统上被用户当作其默认shell的最多的那个shell

[root@localhost ~]# sort -t: -k7 /etc/passwd | cut -d: -f7 | uniq -c | sort -rn | head -1 | cut -c 9-

4.将/etc/passed/中的第三个字段数值最大的后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
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
TEST:X:500:500::/HOME/TEST:/BIN/BASH
LISTEN:X:501:501::/HOME/LISTEN:/BIN/TCSH
A:X:502:502::/HOME/A:/BIN/BASH
B:X:503:503::/HOME/B:/BIN/BASH
C:X:504:504::/HOME/C:/BIN/BASH
MAGEIA:X:1100:1100::/HOME/LINUX:/BIN/BASH
SLACKWARE:X:2002:2016::/HOME/SLACKWARE:/BIN/TCSH
[root@localhost ~]#

5.取出当前主机的IP地址,提示,对ifconfig命令的结果进行切分

[root@localhost ~]# ifconfig | egrep -o “inet ([0-9.]+)” | cut -d” ” -f2

6.列出/etc/目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中

find /etc/ -name “*.conf” | tr ‘a-z‘ ‘A-Z‘ > /tmp/etc.conf
cat /tmp/etc.conf

7.显示/var目录下一级子目录或文件的总个数

[root@localhost ~]# ls -d /var/* | wc -l
16

8.取出/etc/group文件中第三个字段数值最小的10个组的名字

[root@localhost ~]# sort -t: -k3 -n /etc/group | cut -d: -f1 | head
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 Sep 18 01:25:11 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=92316d57-6dc5-45aa-83ae-801037e14eb4 / ext4 defaults 1 1
UUID=c6870226-bc06-4545-aed9-381c0d974a23 /boot ext4 defaults 1 2
UUID=508c0af4-d750-4395-9167-2b41edc42242 /home ext4 defaults 1 2
UUID=aededa68-1ac4-4071-a952-198c93f8deb7 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

[root@localhost ~]# groupadd -g 2016 distro
[root@localhost ~]# tail -2 /etc/group
c:x:504:
distro:x:2016:

(2)、创建用户mandriva,其ID号为1005,基本组为distro

[root@localhost ~]# useradd -u 1005 -g distro mandriva
[root@localhost ~]# tail -1 /etc/passwd
mandriva:x:1005:2016::/home/mandriva:/bin/bash
[root@localhost ~]# tail -1 /etc/group
distro:x:2016:
[root@localhost ~]#

(3)、创建用户mageia,其ID号为1100,家目录为/home/linux

[root@localhost ~]# useradd -u 1100 -d /home/linux mageia
[root@localhost ~]# tail -2 /etc/passwd
mandriva:x:1005:2016::/home/mandriva:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash

(4)、给用户mageia添加密码,密码为mageedu

[root@localhost ~]# passwd mageia
更改用户 mageia 的密码 。
新的 密码:
无效的密码: 它基于字典单词
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[root@localhost ~]#

(5)、删除mandriva,但保留其家目录

[root@localhost ~]# userdel mandriva
[root@localhost ~]# ls -a /home/mandriva/
. .. .bash_logout .bash_profile .bashrc
[root@localhost ~]#

(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组为peguin

[root@localhost ~]# useradd -u 2002 -g distro -G peguin slackware
[root@localhost ~]# tail -2 /etc/group
mageia:x:1100:
peguin:x:2017:slackware

(7)、修改slackware的默认shell为/bin/tcsh

[root@localhost ~]# usermod -s /bin/tcsh slackware
[root@localhost ~]# tail -2 /etc/passwd
mageia:x:1100:1100::/home/linux:/bin/bash
slackware:x:2002:2016::/home/slackware:/bin/tcsh

(8)、为用户slackware新增附加组admins

[root@localhost ~]# groupadd admins
[root@localhost ~]# usermod -G admins slackware
[root@localhost ~]# tail -2 /etc/group
peguin:x:2017:
admins:x:2018:slackware
[root@localhost ~]#

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/100247

(0)
上一篇 2018-06-03 18:39
下一篇 2018-06-03 19:59

相关推荐