用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 ...
随机推荐
- ascii 转换为 utf-8
Python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错: UnicodeDecodeError: 'ascii' codec can't de ...
- 【转】python删除文件里包含关键词的行
import shutil with open('/path/to/file', 'r') as f: with open('/path/to/file.new', 'w') as g: for li ...
- Apache 关于 mod_rewrite 遇到 %2F或%5C (正反斜杠)等特殊符号导致URL重写失效出现404的问题
.htaccess 文件 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d Rew ...
- 在Eclipse中用TODO标签管理任务(Task)
Elipse为Java项目的时候,有一个很人性化的“任务管理”功能,利用这个功能可以方便地将项目中一些需要处理的任务记录下来.先来看看“任务管理”是怎么使用的吧.下面这个类在Class和name 属性 ...
- C# 语言规范_版本5.0 (第9章 命名空间)
1. 命名空间 C# 程序是利用命名空间组织起来的.命名空间既用作程序的“内部”组织系统,也用作“外部”组织系统(一种向其他程序公开自己拥有的程序元素的方法). using 指令(第 9.4 节)用来 ...
- MongoDB1: 环境安装
1. 环境准备,不支持XP系统,需要在Windows7及以上和windows server 2008 系统上安装. 2. 下载安装包:http://www.mongodb.org/downloads, ...
- android自定义控件,其三个父类构造方法有什么区别
android自定义控件时,通常需要重写父类构造函数.这三个够找函数具体啥时调用? public View (Context context) 是在java代码创建视图的时候被调用,如果是从xml填充 ...
- 2015 QQ最新登录算法
首先还是取得验证码,抓包可得:http://check.ptlogin2.qq.com/check?regmaster=&pt_tea=1&uin=2630366651&app ...
- rhel7.2 yum
redhat 的更新包只对注册的用户生效,所以我们自己手动更改成CentOS 的更新包,CentOS几乎和redhat是一样的,所以无需担心软件包是否可安装,安装之后是否有问题. (前提是wget包已 ...
- 使用OllyDbg从零开始Cracking CHM
需要的小伙伴拿走,百度云链接:https://pan.baidu.com/s/1pLJa5dh.另有一些视频资料,太多,需要的可以留言.