linux基础之用户管理

介绍了一些基础命令,比如cut、head传输到管道时的使用,并通过详细的介绍、大量的习题完成了用户管理的相关知识

Linux用户管理

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

##########################################################################
  # 使用who可以查看所有登录的用户,然后使用cut命令将结果进行切分,取得用户名,最后使用sort命令,去重。
[liuqing@localhost ~]$ who
root tty1 2017-11-13 18:02
root pts/0 2017-12-15 11:05 (192.168.58.158)
liuqing pts/1 2017-12-15 12:04 (192.168.58.158)
[liuqing@localhost ~]$ who | cut -d’ ‘ -f1 | sort -u
liuqing
root
##########################################################################

二、列出最后登录到当前系统的用户的相关信息

##########################################################################
# 使用last命令,查找登录的最后的用户列表,使用head取出第一行数据,使用cut取得用户名,最后使用id获取用户的相关信息
[liuqing@localhost ~]$ last | head -1 | cut -d ‘ ‘ -f1 | id
uid=1000(liuqing) gid=1000(liuqing) 组=1000(liuqing) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
##########################################################################

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

##########################################################################
# 先读取passwd文件,再使用cut取出所有的shell,然后使用uniq进行统计每行出现的次数,再使用sort按数值排序,然后取出最后一行。
[liuqing@localhost ~]$ cat /etc/passwd | cut -d: -f7 | uniq -c | sort -n | tail -1
16 /sbin/nologin
#########################################################################

四、 将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保至/tmp/maxusers.txt文件中

#########################################################################
# 先使用cat读取文件,然后使用sort将第3字段按数值排序,使用tail取出后10行,接着使用tr转换小写字母到大写字母,然后写入/tmp/maxusers.txt文件中。
[root@localhost tmp]# cat /etc/passwd | sort -n -t: -k3 | tail -10 | tr ‘a-z’ ‘A-Z’ > /tmp/maxusers.txt
[root@localhost tmp]# cat /tmp/maxusers.txt
NOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGIN
ABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGIN
SYSTEMD-NETWORK:X:192:192:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGIN
CHRONY:X:997:995::/VAR/LIB/CHRONY:/SBIN/NOLOGIN
LIBSTORAGEMGMT:X:998:996:DAEMON ACCOUNT FOR LIBSTORAGEMGMT:/VAR/RUN/LSM:/SBIN/NOLOGIN
POLKITD:X:999:997:USER FOR POLKITD:/:/SBIN/NOLOGIN
LIUQING:X:1000:1000:LIUQING:/HOME/LIUQING:/BIN/BASH
USER2:X:1002:1002::/HOME/USER2:/BIN/BASH
USER3:X:1003:1003::/HOME/USER3:/BIN/BASH
USER1:X:1004:1004::/HOME/USER1:/BIN/BASH
##########################################################################

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

##########################################################################
ifconfig eth0 | egrep -o “\<(([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][1-9]|[2][0-5][0-4])\.){3}([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][1-9]|[2][0-5][0-4])\>”
[root@localhost tmp]# ifconfig eth0 | egrep “\<(([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][1-9]|[2][0-5][0-4])\.){3}([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][1-9]|[2][0-5][0-4])\>” | cut -d’ ‘ -f10
##########################################################################

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

##########################################################################
find /etc -name “*.conf” | tr ‘a-z’ ‘A-Z’ > /tmp/etc.conf
[root@localhost tmp]# cat /tmp/etc.conf
/ETC/RESOLV.CONF
/ETC/PKI/CA-TRUST/CA-LEGACY.CONF
/ETC/YUM/PLUGINCONF.D/FASTESTMIRROR.CONF
/ETC/YUM/PLUGINCONF.D/LANGPACKS.CONF
/ETC/YUM/PROTECTED.D/SYSTEMD.CONF
/ETC/YUM/VERSION-GROUPS.CONF
……
##########################################################################

七、 显示/var目录下一级子目录或者文件的总个数。

##########################################################################
[root@localhost tmp]# tree -L 1 /etc | tail -1 | cut -d’ ‘ -f1
89
##########################################################################

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

#########################################################################
[root@localhost tmp]# cat /etc/group | sort -n -k3 -t: | head -10 | cut -d: -f1
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
##########################################################################

九、 将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中

##########################################################################
[root@localhost tmp]# cat /etc/fstab /etc/issue | tee > /tmp/etc.test
[root@localhost tmp]# cat /tmp/etc.test
#
# /etc/fstab
# Created by anaconda on Tue Nov 14 01:28:16 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
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=3e44d68c-8902-4591-935d-57ea49691301 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
\S
Kernel \r on an \m
##########################################################################

十、 请总结描述用户和组管理类命令的使用方法并完成以下练习:

总结和描述详见:另一篇博客。网址为:http://www.178linux.com/90202
##########################################################################

1. 创建组distro,其GID为2016;

[root@localhost ~]# groupadd -g 2016 distro
[root@localhost ~]# cat /etc/group | grep distro
distro:x:2016:

2. 创建用户mandriva,其ID号是1005,基本组为distro;

[root@localhost tmp]# useradd -u 1100 -g distro mandriva
[root@localhost tmp]# id mandriva
uid=1100(mandriva) gid=2016(distro) 组=2016(distro)

3. 创建用户mageia,其ID号是1100,家目录为/home/linux

[root@localhost tmp]# useradd -u 1100 -d /home/linux mageia
[root@localhost tmp]# cat /etc/passwd | grep “mageia”
mageia:x:1100:1100::/home/linux:/bin/bash

4. 组用户mageia添加密码,密码为mageedu;

[root@localhost tmp]# passwd mageia
更改用户 mageia 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

5. 删除mandriva,但保留其家目录;

[root@localhost test3]# userdel mandriva

6. 创建用户slackware,其ID号为2002,基本组distro,附加组peguin;

[root@localhost test3]# groupadd peguin
[root@localhost test3]# useradd -u 2002 -g distro -G peguin slackware
[root@localhost test3]# id slackware
uid=2002(slackware) gid=2016(distro) 组=2016(distro),2017(peguin)

7. 修改slackware的默认shell为/bin/tcsh;

[root@localhost test3]# usermod -s /tin/tcsh slackware
[root@localhost test3]# id slackware
uid=2002(slackware) gid=2016(distro) 组=2016(distro),2017(peguin)

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),2019(admins)
##########################################################################

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

(2)
神策神策
上一篇 2017-12-15 18:07
下一篇 2017-12-15 21:39

相关推荐

  • 第二周- -问题总结

    前言:这周学习了文件的基本操作、用户和权限管理、重定向和管道。 1.在centos6.9当中,创建虚拟机的时候,网络无法获取地址,ping也不通,出现  device eth0 does not seem to be present, delaying initialization 答:(1).虚拟机启动出错,就把这个虚拟机删除掉重新建立,系统虚拟…

    2017-07-23
  • LVS

    LVS概述 1.LVS:Linux Virtual Server全称叫做linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。四层路由器(交换),根据请求报文的目标IP和目标协议及端口将其调度转发至后端主机集群中的某台RealServer(真实服务器),根据调度算法来挑选RS; 主要有…

    Linux干货 2016-11-07
  • N25-第八周作业

    第八周 1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态; 在线的主机使用绿色显示; 不在线的主使用红色显示; #!/bin/bash # ip=172.16.250. for i in {1..254}; do if ping -w 1 -c 1 $ip$i &> /dev/n…

    Linux干货 2017-03-09
  • 网络管理基础-子网划分及网络配置练习

    1、某公司申请到一个C 类IP 地址,但要连接6 个的子公司,最大的一个子  公司有26 台计算机,每个子公司在一个网段中,则子网掩码应设为?  192.168.100.1 网络位192.168.100 192.168.100.00000000 C类掩码255.255.255.0 192.168.100. 000 00001  …

    Linux干货 2016-09-05
  • 第五周作业

    1. 显示当前系统上root、fedora或user1用户的默认shell; grep -E "^(root|centos|user1)\>" /etc/passwd 或 awk -F: '{if($1=="root" || $1==…

    Linux干货 2017-01-10