Linux创建user, 设置密码,并赋予sudo su root权限

本文参考: 

linux下创建用户,给用户设置密码,给用户授权

xxx is not in the sudoers file.This incident will be reported.的解决方法

给Linux服务器新建一个user, 设置登陆密码,然后赋予sudo su root权限。 介绍两种方式:交互方式,非交互脚本方式

交互方式

新建一个user, 设置登陆密码

1. 登陆putty, change user

sudo su root

 2. add group, group name is usernametest

groupadd usernametest

3. add user and and user to group, user name is usernametest

useradd -d /home/usernametest/ -m -g usernametest usernametest

4. set password  password123456

passwd  password123456

5. change mod

cd /home/
chmod 775 -R usernametest

给user赋予sudo su root权限: 否则 usernametest 登陆后,执行sudo su root 会报:appsvc is not in the sudoers file.  This incident will be reported.

1. 登陆putty, change user

sudo su root

2. 添加sudo文件的写权限

chmod u+w /etc/sudoers

3. 编辑sudoers文件

vim /etc/sudoers

4. root ALL=(ALL) ALL下一行添加

usernametest ALL=(ALL) ALL

5. 撤销sudoers文件写权限

chmod u-w /etc/sudoers

然后user 是 usernametest 时,才可以成功执行命令: sudo su root

非交互方式脚本方式

新建一个user, 设置登陆密码,然后赋予sudo su root权限

#!/bin/sh
# name=username01
# pass=password01

name=$1
pass=$2

# add user
sudo useradd ${name}
if [ $? -eq 0 ];then
   echo "user ${name} is created successfully!!!"
else
   echo "user ${name} is created failly!!!"
   exit 1
fi
#sudo passwd 
echo ${pass} | sudo passwd ${name} --stdin  &>/dev/null
if [ $? -eq 0 ];then
   echo "${name}'s password is set successfully"
else
   echo "${name}'s password is set failly!!!"
fi
# add user to sudoers file 
echo "${name} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
echo "${name} add to sudoers file successfully"

上一篇:学习系统安全及应用


下一篇:移动安全-APP检测ROOT设备的方法