将Linux普通用户添加为系统管理员在Gnome或KDE这样强大与完善的桌面环境下是非常简单的事情,一般来说在用户设置的对话框里就直接有相应选项。不过,出于简洁与高效的风格,自己目前并未使用这些高端但吃内存的“重量级”桌面环境,使用的就是最基本的X视窗+Sawfish窗口管理器的组合。在这样的环境下进行用户管理,都是通过命令行来完成。如,使用useradd命令添加新用户。不过,由useradd命令添加的用户只具有普通使用者的权限,不具备系统管理的能力。这样一来,就给一些常见的操作带来不便,如,使用sudo命令临时升级为管理员,刻录光盘,访问蓝牙设备等。导致此现象的原因是,由useradd命令生成的用户默认不属于一些关键的系统管理组,比如:

adm dialout fax cdrom floppy tape sudo audio dip video plugdev netdev bluetooth lpadmin fuse scanner powerdev burning

为此,只要使用usermod命令将用户添加到这些组即可。但每回如此操作多有不便,于是就可以写一个脚本程序来自动做这件事情。脚本设计功能为:

  1. 将上述列出的组定义为系统管理员组列表。
  2. 脚本程序可将命令行中指定的用户添加到每个组中。若不指定用户名,则将当前登录的用户加入到组中。注,当前登录的用户名可以用whoami命令查询。
  3. 脚本中定义函数add_to_groups。其第一个参数是待加为管理员的用户名,第二个及之后所有的参数为上述管理员组列表。函数会检查指定用户是否已经在管理员组中。如果不在,则使用usermod命令将其加到组内。

基于自己制订的Bash脚本模板,写成的脚本add_admin.sh如下:

#!/bin/bash

script_name="add_admin.sh"
script_usage=$(cat <<EOF
$script_name [USER NAME]
EOF
)
script_function=$(cat <<EOF
This script is used to add the current or specified users as system administrator.
EOF
)
script_doc=$(cat <<EOF
-h Display this help.
EOF
)
script_examples=$(cat <<EOF
EOF
)
state_prefix="==="
warning_prefix="***"
error_prefix="!!!" function display_help() {
if [ -n "$script_usage" ]; then
echo -e "Usage: $script_usage"
fi if [ -n "$script_function" ]; then
echo -e "$script_function"
fi if [ -n "$script_doc" ] ; then
echo -e "\n$script_doc"
fi if [ -n "$script_examples" ]; then
echo -e "\nExamples"
echo -e "$script_examples"
fi
} function add_to_groups() {
the_user="$1"
shift for the_group in "$@" ; do
if [ -n "`cat /etc/group | grep $the_group`" ]; then
if [ -n "`groups $the_user | grep $the_group | cut -d ':' -f 2`" ]; then
echo "$warning_prefix User '$the_user' has already been in the group '$the_group'!"
else
sudo usermod -a -G $the_group $the_user
echo "$state_prefix User '$the_user' has been added to the group '$the_group'!"
fi
else
echo "$warning_prefix The group '$the_group' does not exist!"
fi
done
} # Process command options
while getopts ":h" opt; do
case $opt in
h ) display_help
exit ;;
\? ) display_help
exit ;;
esac
done
shift $(($OPTIND - )) admin_groups="adm dialout fax cdrom floppy tape sudo audio dip video plugdev netdev bluetooth lpadmin fuse scanner powerdev burning" # Start execute the command
if [ $OSTYPE = 'linux-gnu' ]; then
# Get the user name
if [ -n "$*" ]; then
for the_user in "$@" ; do
if [ -n "`cat /etc/passwd | grep $the_user | cut -d ':' -f 1`" ]; then
add_to_groups $the_user $admin_groups
echo "$state_prefix User '$the_user' has been set as administrator!"
else
echo "$warning_prefix '$the_user' is not a valid user!"
fi
done
else
the_user=`whoami`
echo "$state_prefix The current logged-on user '$the_user' will be set as administrator!"
add_to_groups $the_user $admin_groups
echo "$state_prefix User '$the_user' has been set as administrator!"
fi
exit
fi echo "$warning_prefix Operating system or host name is not supported!"

用Bash脚本将Linux普通用户添加为系统管理员的更多相关文章

  1. linux 中将用户添加到组的 4 个方法

    Linux 组是用于管理 Linux 中用户帐户的组织单位.对于 Linux 系统中的每一个用户和组,它都有惟一的数字标识号.它被称为 用户 ID(UID)和组 ID(GID).组的主要目的是为组的成 ...

  2. Linux给用户添加sudo权限

    一.linux给用户添加sudo权限: 有时候,linux下面运行sudo命令,会提示类似: xxxis not in the sudoers file.  This incident will be ...

  3. Linux将用户添加到组的指令

    原文:https://blog.csdn.net/youmatterhsp/article/details/80549683:           https://www.cnblogs.com/cl ...

  4. Linux 中将用户添加到组的指令

    在 Linux 操作系统下,如何添加一个新用户到一个特定的组中?如何同时将用户添加到多个组中?又如何将一个已存在的用户移动到某个组或者给他增加一个组?对于不常用 Linux 的人来讲,记忆 Linux ...

  5. linux 给用户添加进新的组

    给用户user1添加一个新的组group1 usermod -G group1 #给当前登录用户所在组设置为 group1 注意:上面的命令有个问题需要知道,这个操作是重置用户所在组,也就是会让当前用 ...

  6. Linux 中将用户添加到指定组的指令

    将一个已有用户 testuser 增加到一个已有用户组 root 中,使此用户组成为该用户的附加用户组,可以使用带 -a 参数的 usermod  指令.-a 代表 append, 也就是将用户添加到 ...

  7. linux把用户添加到组

    使用 usermod 命令 将现有的用户添加到多个次要组或附加组 # usermod -a -G GroupName UserName id 命令查看输出 # id UserName 用户添加到多个次 ...

  8. 为linux普通用户添加超级用户权限sudo

    问题:假设用户名为:ali如果用户名没有超级用户权限,当输入 sudo + 命令 时, 系统提示: ali is not in the sudoers file.  This incident wil ...

  9. linux普通用户添加root权限

    新增一个普通用户并进入该用户: [root@VM_0_7_centos ~]# groupadd mall [root@VM_0_7_centos ~]# useradd mall -m -d /ho ...

随机推荐

  1. android studio修改项目包名

    公司项目都是用eclipse开发的,但是android studio开发已经是大势所趋了,所以在闲暇之余使用了一下androidstudio,这里对androidstudio更改项目包名做一下总结,因 ...

  2. android studio 将library导出为jar 亲测成功

    本人使用的是helloChart这个开源项目,其主要用于图表,来自git 地址为:https://github.com/lecho/hellocharts-android 下载命令为 git clon ...

  3. Java模拟post-get提交

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  4. Python中fileinput模块使用

    fileinput模块可以对一个或多个文件中的内容进行迭代.遍历等操作.该模块的input()函数有点类似文件 readlines()方法,区别在于前者是一个迭代对象,需要用for循环迭代,后者是一次 ...

  5. VMware虚拟机服务的vmware-hostd自动启动和停止

    安装了虚拟机 任务管理器会出现vmware-hostd.exe  占用了80端口,导致xampp打不开,所以就想关闭vmware,解决方案如下: 开始——运行——services.msc,找到VM打头 ...

  6. Java中的Class类

    Class 类是在Java语言中定义一个特定类的实现.一个类的定义包含成员变量,成员方法,还有这个类实现的接口,以及这个类的父类.Class类的对象用于表示当前运行的 Java 应用程序中的类和接口. ...

  7. shell-逐行读取文件

    代码: #!/bin/bash echo 方法1 while read line do echo $line; done < testdata echo "" echo 方法 ...

  8. [SOJ] DAG?

    Description 输入一个有向图,判断该图是否是有向无环图(Directed Acyclic Graph). Input 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n ...

  9. tomcat解压版安装(摘自网络)

    配置Tomcat[解压版] 选择解压版的Tomcat的理由是可以让我们使用多个Tomcat,但是配置上就会出现一些问题,需要我们手动进行更改配置.我的Tomcat版本是:apache-tomcat-6 ...

  10. php笔记(八)PHP类与对象之抽象类

    <?php //通过abstract关键字定义一个抽象类 abstract class ACanEat{ //通过abstract关键字定一个不用具体实现的抽象方法eat() abstract ...