用户及用户组管理使用的练习

用户及用户组管理使用的练习

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

先使用who得出登陆用户,通过uniq去重。

[root@mail ~]# who | grep -E -o "^[[:alnum:]]+"  | uniq 
root

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

思路:首先使用who取得最后系统的登陆用户,然后把结果通过命令过滤得到用户名,最后通过ID命令显示

方法1:
[root@mail ~]# id $( who | tail -n 1 | grep -E -o "^[[:alnum:]]+" )
uid=0(root) gid=0(root) 组=0(root)

方法2:用cut代替grep
[root@mail ~]# id $(who | tail -n 1 | cut -d' ' -f1 )
uid=0(root) gid=0(root) 组=0(root)
[root@mail ~]# 

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

思路:先把passwd的shell取出来通过管道送个uniq去重并计算,再通过sort排序,最后用head取出最高计次的结果。

方法1:
[root@mail ~]# grep -E "/*[[:alnum:]]+/*[[:alnum:]]+$" /etc/passwd -o  | uniq -cd | sort | head -n 1
     16 /sbin/nologin
方法2:
用cut替代grep
[root@mail ~]# cut -d: -f7 /etc/passwd  | uniq -cd | sort | head -n 1
     16 /sbin/nologin

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

思路:首先用cut命令把1和3列提取出来,通过sort利用分隔符以数字形式排序第2列的内容,再通过tail得到最大数值的用户,最后通过tr命令把小写字母换成大写并重定向/tmp/maxusers

[root@mail ~]# cut -d: -f1,3 /etc/passwd  | sort -t: -k2 -n | tail -n 10 | cut -d: -f1 | tr [a-z] [A-Z] > /tmp/maxusers.txt
[root@mail ~]# cat /tmp/maxusers.txt    //查看结果

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

思路:先使用grep过滤不需要的信息,然后通过cut命令进行切分,得出IP

ifconfig | grep "\<inet\>"  | grep "addr:[[:digit:]]*.*" -o | cut -d' ' -f1 | cut -d: -f2

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

思路:先找出文件然后通过tr命令转换并重定向文件中

[root@mail ~]# ls /etc/*.conf | tr [a-z] [A-Z] > /tmp/etc.conf
[root@mail ~]# cat /tmp/etc.conf 

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

思路:通过wc统计行即可

[root@mail ~]# ls /var -l | wc -l
19

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

思路:用cut分割先取出排序内容,通过sort排序最后用head取出前10行即可

[root@mail ~]# cut -d: -f1,3 /etc/group | sort -t: -n -k2 | head 

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

思路:cat能同时打开多个文件,重定向到文件即可。

[root@mail ~]# cat /etc/fstab /etc/issue > /tmp/etc.test
[root@mail ~]# cat /tmp/etc.test 

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

  1. 创建组distro,其GID为2016;
[root@mail ~]# groupadd -g 2016 distro
[root@mail ~]# cat /etc/group | grep distro 
distro:x:2016:
  1. 创建用户mandriva, 其ID号为1005;基本组为distro;
[root@mail ~]# useradd -u 1005 -g distro mandriva
[root@mail ~]# id mandriva
uid=1005(mandriva) gid=2016(distro) 组=2016(distro)
  1. 创建用户mageia,其ID号为1100,家目录为/home/linux;
[root@mail ~]# useradd -u 1100 -m -d /home/linux mageia
[root@mail ~]# cat /etc/passwd | grep mageia
mageia:x:1100:1100::/home/linux:/bin/bash
  1. 给用户mageia添加密码,密码为mageedu;
[root@mail ~]# echo "mageedu" | passwd --stdin mageia
更改用户 mageia 的密码 。
passwd: 所有的身份验证令牌已经成功更新。

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

[root@mail ~]# userdel mandriva 
[root@mail ~]# ls /home/mandriva -d
/home/mandriva
  1. 创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
[root@mail ~]# useradd -g distro -G peguin -u 2002 slackware
[root@mail ~]# cat /etc/passwd | grep slackware
slackware:x:2002:2016::/home/slackware:/bin/bash
[root@mail ~]# cat /etc/group | grep peguin
peguin:x:5001:gentoo,slackware
[root@mail ~]# cat /etc/group | grep distro 
distro:x:2016:
  1. 修改slackware的默认shell为/bin/zsh;
[root@mail ~]# usermod -s /bin/zsh slackware
[root@mail ~]# cat /etc/passwd | grep slackware
slackware:x:2002:2016::/home/slackware:/bin/zsh

8.为用户slackware新增附加组admins;

[root@mail ~]# groupadd admins
[root@mail ~]# usermod -G admins slackware
[root@mail ~]# cat /etc/group | grep admins
admins:x:5003:slackware

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

(0)
oneeonee
上一篇 2018-03-18 18:39
下一篇 2018-03-18 20:51

相关推荐

  • 磁盘管理、文件系统管理

    磁盘管理、文件系统管理 磁盘管理 磁盘设备 机械硬盘(非固态) I/O Ports —– I/O 设备地址 块设备:Block 存取单位“块”;(磁盘) 字符设备:char 存取单位“字符”;(键盘) 设备文件:关联一个设备驱动程序,进而能够跟与之对应的硬件设备进行通信; – 设备号码: 主设备号 major number…

    Linux干货 2016-07-22
  • shell脚本编程3

    补充,shift使用方法 root@localhost wang]# cat jiaoben1.sh  #!/bin/bash echo "$1" echo "$*" shift echo "$1" echo &quot…

    Linux干货 2016-08-24
  • N25 – Week 5 blog

    1. 显示当前系统上root, fedora或user1用户的默认shell [root@dhcp-10-129-6-166 ~]# grep -E "root|fedora|user1" /etc/passwd | grep -o "[^…

    Linux干货 2016-12-27
  • LINUX下用户管理命令简述

    LINUX下用户管理命令简述 添加用户并设置密码 useradd [用户名] 创建用户 [root@localhost ~]# useradd jack [root@localhost ~]# cat /etc/shadow | grep jack jack:!!:17257:0:99999:7::: passwd [用户名] 设置密码 [root@loca…

    Linux干货 2017-04-05
  • 01Linux的发展历史

    1、1965年时,贝尔实验室(Bell Labs)加入一项由通用电气(General Electric)和麻省理工学院(MIT)合作的项目;该项目要建立一套多使用者、多任务、多层次(multi-user、multi-task、multi-level)的MULTICS操作系统。但是由于整个目标过于庞大,糅合了太多的特性,Multics虽然发布了一些产品,但是性…

    Linux干货 2016-10-14
  • python文件操作

    文件操作使用的函数open 打开read 读write 写close 关其他非常用的 seek 设置读取指针 tell 读取读取指针位置 windows中def encode(self, encoding=’utf-8′, errors=’strict’)open 和它的参数open(file,mode=&#8…

    Linux干货 2017-10-31