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,shell 脚本 格式要求:首行shebang机制                  #!/bin/bash       &n…

    2017-08-05
  • 8.3作业

    创建sysadmins组  将user1 user2 user3 加入组中 将user3设置为管理员 用user3登录,将user2从组中移除 设置sysadmins口令centos 设置user1在创建新文件时所属组为sysadmins 删除User1-3 删除sysadmins [root@localhost testdir]#&nb…

    Linux干货 2016-08-04
  • Acheng学习宣言

    我是Acheng,我参加了马哥Linux运维网络班,从2016年9月开始我将跟随马哥学习Linux技术。我的学习宣言是:再努力也不为过!朝着活学活用,做到做精的方向努力吧! 

    Linux干货 2016-10-25
  • 设计模式原则详解

        我们在应用程序开发中,一般要求尽量两做到可维护性和可复用性。       应用程序的复用可以提高应用程序的开发效率和质量,节约开发成本,恰当的复用还可以改善系统的可维护性。而在面向对象的设计里面,可维护性复用都是以面向对象设计原则为基础的,这些设计原则首先都是复用的原则,遵循这些设…

    Linux干货 2015-04-07
  • btrfs文件系统——更加高级的文件系统

    Btrfs 文件系统: Btrfs ( B-tree ,Butter FS ,Better FS ) GPL ,Oracle公司在2007年研发,支持CoW(对文件快照),为了取代ext3,ext4,xfs 核心特点:          多物理卷支持:btrfs可由多个底层物理卷…

    Linux干货 2016-08-31
  • 马哥教育网络班22期第六周课程练习1

    1、1,$s/^\s\+/#/g 2、1,$s/^\s\+$//g 3、1,$s/^#\s//g 4、1,3s/^/#/g 5、sed -e s/gpgcheck=1/gpgcheck=0/g -e s/enabled=0/enabled=1/g /etc/yum.repos.d/CentOS-Media.repo  6、0 /4 * * * cp…

    Linux干货 2016-09-26