用Bash脚本将Linux普通用户添加为系统管理员
将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命令将用户添加到这些组即可。但每回如此操作多有不便,于是就可以写一个脚本程序来自动做这件事情。脚本设计功能为:
- 将上述列出的组定义为系统管理员组列表。
- 脚本程序可将命令行中指定的用户添加到每个组中。若不指定用户名,则将当前登录的用户加入到组中。注,当前登录的用户名可以用whoami命令查询。
- 脚本中定义函数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普通用户添加为系统管理员的更多相关文章
- linux 中将用户添加到组的 4 个方法
Linux 组是用于管理 Linux 中用户帐户的组织单位.对于 Linux 系统中的每一个用户和组,它都有惟一的数字标识号.它被称为 用户 ID(UID)和组 ID(GID).组的主要目的是为组的成 ...
- Linux给用户添加sudo权限
一.linux给用户添加sudo权限: 有时候,linux下面运行sudo命令,会提示类似: xxxis not in the sudoers file. This incident will be ...
- Linux将用户添加到组的指令
原文:https://blog.csdn.net/youmatterhsp/article/details/80549683: https://www.cnblogs.com/cl ...
- Linux 中将用户添加到组的指令
在 Linux 操作系统下,如何添加一个新用户到一个特定的组中?如何同时将用户添加到多个组中?又如何将一个已存在的用户移动到某个组或者给他增加一个组?对于不常用 Linux 的人来讲,记忆 Linux ...
- linux 给用户添加进新的组
给用户user1添加一个新的组group1 usermod -G group1 #给当前登录用户所在组设置为 group1 注意:上面的命令有个问题需要知道,这个操作是重置用户所在组,也就是会让当前用 ...
- Linux 中将用户添加到指定组的指令
将一个已有用户 testuser 增加到一个已有用户组 root 中,使此用户组成为该用户的附加用户组,可以使用带 -a 参数的 usermod 指令.-a 代表 append, 也就是将用户添加到 ...
- linux把用户添加到组
使用 usermod 命令 将现有的用户添加到多个次要组或附加组 # usermod -a -G GroupName UserName id 命令查看输出 # id UserName 用户添加到多个次 ...
- 为linux普通用户添加超级用户权限sudo
问题:假设用户名为:ali如果用户名没有超级用户权限,当输入 sudo + 命令 时, 系统提示: ali is not in the sudoers file. This incident wil ...
- linux普通用户添加root权限
新增一个普通用户并进入该用户: [root@VM_0_7_centos ~]# groupadd mall [root@VM_0_7_centos ~]# useradd mall -m -d /ho ...
随机推荐
- usaco月赛,2017.1总结
T1:跳舞的奶牛 大致题意:一个体积为k的舞台能够同时容纳k只奶牛一起跳舞,他们每头奶牛的跳舞时间不同,如果有一只奶牛跳完了第k+1头奶牛就会立刻上场跳舞,当所有奶牛跳完舞以后我们认为这次表演结束.现 ...
- HTTP 错误 403.14 - Forbidden的解决办法
错误: HTTP 错误 403.14 - ForbiddenWeb 服务器被配置为不列出此目录的内容. 原因: 出现这个错误,是因为默认文档中没有增加index.aspx导致的. 解决方法: 打开 ...
- Chapter 2 Open Book——37
I couldn't concentrate on Mike's chatter as we walked to Gym, and RE. didn't do much to hold my atte ...
- 3-Dom
HTML DOM 对象DOM DocumentDOM ElementDOM AttributeDOM Event Document对象 每个载入浏览器的 HTML 文档都会成为 Document 对象 ...
- python 之 批量替换文件中文本后缀
代码示例如下: #!/usr/local/bin python import os def swapextensions(dir, before, after): if before[:1] != ' ...
- Android UI方面的学习记录
1,android:textAllCaps=“false” android5.0后有可能button的text显示全是大写,设置这个后才能正常显示小写 2,优化listview性能: 1,view重用 ...
- 使用webview加载html图片、表单超屏幕问题
webView加载html代码时,使用webView自带的 scalesPageToFit 可以解决图片所带来的超过屏幕问题:但是,所带来的问题就是文字变小了,怎样让图片边小,并且文字还是原来html ...
- BuildingAndRunningUAFServerUsingMaven
https://github.com/eBay/UAF/wiki/BuildingAndRunningUAFServerUsingMaven(CLIonly) 实现uaf的demo,使用ebay的方案 ...
- sql语句的学习(2)
7.统计:学号.姓名.语文.数学.英语.总分.平均成绩 8.列出各门课程的平均成绩.课程,平均成绩 9.列出数学成绩的排名(要求显示字段:学号,姓名,成绩,排名) 10.列出数学成绩在2-3名的学生( ...
- ARM驱动调试方法、思路总结、笔记
驱动程序的调试一. 打印: prink, 自制proc文件UBOOT传入console=ttySAC0 console=tty11. 内核处理UBOOT传入的参数console_setup add_p ...