Linux基本内容介绍(3)-- 用户管理类命令示例

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

who |awk '{print $1}'|sort |uniq

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

#!/bin/bash
username=`last -1|awk '{print $1}'|head -1`
user_info=`grep -E "^$username" /etc/passwd`
echo "user name is "`echo $user_info|cut -d: -f1`
echo "UID is "`echo $user_info|cut -d: -f3`
echo "GID is "`echo $user_info|cut -d: -f4`
echo "Home directory is "`echo $user_info|cut -d: -f6`
echo "Shell is "`echo $user_info|cut -d: -f7`

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

cat /etc/passwd|cut -d: -f7|uniq -c|sort -nr|head -1|awk '{print $2}'

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

#!/bin/bash
for s in `cat /etc/passwd|cut -d: -f3|sort -nr|head -10`;do
for i in `cat /etc/passwd`;do
check=$(echo $i|cut -d: -f3)
[ ! -z $check ] && [ `echo $check|grep -E '^[[:digit:]]*$'|wc -l` -eq 1 ] && [ $check -eq $s ] && echo $i |tr [a-z] [A-Z] >> /tmp/maxusers.txt
done
done

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

ifconfig eth0|grep "inet addr"|cut -d: -f2|sed 's/  Bcast//'

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

find /etc/ -type f -name "*.conf" |tr [a-z] [A-Z] >/tmp/etc.conf

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

ls /var |wc -l

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

sort -t: -k3 -n /etc/group |cut -d: -f1|head -10

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

cat /etc/fstab /etc/issue > /tmp/etc.test

10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
(1)、创建组distro,其GID为2016;

groupadd -g 2016 distro

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

扫描二维码关注公众号,回复: 3003632 查看本文章
useradd -u 1005 -g distro mandriva

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

useradd -u 1100 -d /home/linux mageia

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

echo "mageedu"|passwd --stdin mageia

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

userdel mandriva

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

groupadd peguin
useradd -u 2002 -g distro -G peguin slackware

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

 usermod -s /bin/tcsh slackware

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

usermod -a -G admins slackware

猜你喜欢

转载自blog.csdn.net/espressomike/article/details/82254546