将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!"

最新文章

  1. 【清北学堂】 死亡(death)
  2. 【转帖】ActiveX部件不能创建对象的终极解决方案
  3. Nessus常见问题整理
  4. c#常用的Datable转换为json,以及json转换为DataTable操作方法
  5. app 后端技术
  6. J2EE事务
  7. hadoop 2.0 详细配置教程
  8. hadoop2.2编程:DFS API 操作
  9. hibernate_validator_08
  10. bootstrap初探
  11. *.do和*.action的区别
  12. Django之wagtail安装及配置
  13. Activity平移动画
  14. 团队项目——NABCD
  15. TZOJ :2731: 存钱计划(二)
  16. 带你入门 Docker
  17. [USACO 06DEC]Milk Patterns
  18. Linux(Ubuntu)下也能用搜狗输入法了!!!
  19. 第211天:git和github的区别和使用详解
  20. Codeforces 804D Expected diameter of a tree

热门文章

  1. 在虚拟机上配置linux lab的相关经验
  2. XML+AJAX
  3. cos距离与欧式距离
  4. 注意Thread.interrupt()方法的真正作用并不是用来中断线程
  5. gulp插件大全
  6. 浙大pat 1012题解
  7. Jmeter连接SqlServer数据库进行压力测试
  8. jquery 使用attr() 函数对复选框无效的原因
  9. Cocoapod安装使用和常见问题(转载)
  10. Quartz+log4net实现控制台程序定时运行,并且记录日志