目标
- 查看user
- 给Linux服务器新建一个user, 设置登陆密码,然后赋予sudo su root权限。 介绍两种方式:交互方式,非交互脚本方式
- 删除user
给Linux服务器新建一个user, 设置登陆密码,然后赋予sudo su root权限。 介绍两种方式:交互方式,非交互脚本方式
1.查看Linux服务器的user
# 查看所有用户的列表
cat /etc/passwd
#查看当前活跃的用户列表
w
# 查看用户组
cat /etc/group
#查看当前登录用户名
whoami
#查看当前登录用户的组内成员
groups
#查看gliethttp用户所在的组,以及组内成员
groups gliethttp
# 查看所有用户 一个简明的layout命令
cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F":" '{ print $1"|"$3"|"$4 }'|more
2.创建user, 设置密码,并赋予sudo su root权限
创建user后,需要给user赋予sudo su root权限,否则 usernametest 登陆后,执行sudo su root 会报:appsvc is not in the sudoers file. This incident will be reported.
交互方式
#登陆putty, change user
sudo su root
#add group, group name is usernametest
groupadd usernametest
#add user and and user to group, user name is usernametest
useradd -d /home/usernametest/ -m -g usernametest usernametest
#set password password123456
passwd password123456
#change mod
cd /home/
chmod 775 -R usernametest
#登陆putty, change user
sudo su root
#添加sudo文件的写权限
chmod u+w /etc/sudoers
#编辑sudoers文件
vim /etc/sudoers
#root ALL=(ALL) ALL下一行添加
usernametest ALL=(ALL) ALL
#撤销sudoers文件写权限
chmod u-w /etc/sudoers
非交互方式脚本方式
#!/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 "Fail to create user ${name}"
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 "Fail to set ${name}'s password"
fi
# add user to sudoers file
echo "${name} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
echo "${name} add to sudoers file successfully"
3.删除user
userdel -r username
本文参考:
xxx is not in the sudoers file.This incident will be reported.的解决方法