2024-09-24 17:59:47 +08:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
declare user_choice
|
2024-09-25 00:51:25 +08:00
|
|
|
|
echo "========$(basename $0 .sh)========"
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "1. 新建用户"
|
|
|
|
|
echo "2. 查看所有用户"
|
|
|
|
|
echo "3. 删除用户"
|
|
|
|
|
echo "4. 修改用户密码"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
echo "任意输入返回主菜单"
|
2024-11-18 23:12:52 +08:00
|
|
|
|
read -p "请输入要使用的功能:" user_choice
|
2024-09-24 17:59:47 +08:00
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
case $user_choice in
|
2024-09-24 17:59:47 +08:00
|
|
|
|
'1')
|
2024-11-18 23:12:52 +08:00
|
|
|
|
declare new_user_name
|
|
|
|
|
read -p "请输入要创建的用户名:" new_user_name
|
2024-09-24 17:59:47 +08:00
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if id "$new_user_name" &>/dev/null; then
|
|
|
|
|
echo "用户 $new_user_name 已存在。"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
useradd -m -s /bin/bash "$new_user_name"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if grep -q "^$new_user_name " /etc/sudoers;then
|
|
|
|
|
sed -i "s/^#\?$new_user_name.*/$new_user_name ALL=(ALL) NOPASSWD: ALL/g" /etc/sudoers
|
2024-09-24 17:59:47 +08:00
|
|
|
|
else
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "$new_user_name ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
|
2024-09-24 17:59:47 +08:00
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
declare login_method_choice
|
2024-09-24 17:59:47 +08:00
|
|
|
|
echo "用户登录方式"
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "y. 密码登录"
|
|
|
|
|
echo "n. 使用 root 用户公钥"
|
|
|
|
|
read -p "默认 y,请输入:" login_method_choice
|
|
|
|
|
if [[ ! $login_method_choice =~ [Nn] ]];then
|
|
|
|
|
declare user_password
|
|
|
|
|
read -p "请输入密码:" user_password
|
|
|
|
|
echo "$new_user_name:$user_password" |chpasswd
|
2024-09-24 17:59:47 +08:00
|
|
|
|
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config;
|
|
|
|
|
echo "创建成功"
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "账号:$new_user_name"
|
|
|
|
|
echo "密码:$user_password"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
else
|
|
|
|
|
sed -i 's/^#\?PubkeyAuthentication.*/PubkeyAuthentication yes/g' /etc/ssh/sshd_config
|
2024-11-18 23:12:52 +08:00
|
|
|
|
su "$new_user_name" -c "mkdir -p '/home/$new_user_name/.ssh'"
|
|
|
|
|
cp "/root/.ssh/authorized_keys" "/home/$new_user_name/.ssh/authorized_keys"
|
|
|
|
|
chown "$new_user_name:$new_user_name" "/home/$new_user_name/.ssh/authorized_keys"
|
|
|
|
|
su "$new_user_name" -c "chmod 600 '/home/$new_user_name/.ssh/authorized_keys'"
|
|
|
|
|
su "$new_user_name" -c "chmod 700 '/home/$new_user_name/.ssh/'"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
|
|
|
|
|
echo "创建成功"
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "账号:$new_user_name"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
echo "密钥登录"
|
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
declare root_login_choice
|
|
|
|
|
echo "是否关闭 root 用户登录"
|
|
|
|
|
read -p "输入 n 取消关闭:" root_login_choice
|
2024-09-24 17:59:47 +08:00
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if [[ ! $root_login_choice =~ [Nn] ]];then
|
2024-09-24 17:59:47 +08:00
|
|
|
|
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin no/g' /etc/ssh/sshd_config
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "root 用户登录已关闭"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
systemctl restart sshd.service
|
|
|
|
|
;;
|
|
|
|
|
'2')
|
|
|
|
|
echo "当前系统有以下用户"
|
|
|
|
|
cut -d: -f1 /etc/passwd
|
|
|
|
|
;;
|
|
|
|
|
'3')
|
2024-11-18 23:12:52 +08:00
|
|
|
|
declare user_to_delete
|
|
|
|
|
read -p "请输入需要删除的用户名:" user_to_delete
|
|
|
|
|
if ! id $user_to_delete &> /dev/null ;then
|
2024-09-24 17:59:47 +08:00
|
|
|
|
echo "系统内没有该用户"
|
|
|
|
|
exit
|
|
|
|
|
fi
|
2024-11-18 23:12:52 +08:00
|
|
|
|
sed -i "/^#\?$user_to_delete.*/d" /etc/sudoers &> /dev/null
|
|
|
|
|
pkill -u $user_to_delete
|
|
|
|
|
userdel -r $user_to_delete &> /dev/null
|
|
|
|
|
rm -rf "/home/${user_to_delete}"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
echo "用户删除成功"
|
|
|
|
|
;;
|
|
|
|
|
'4')
|
2024-11-18 23:12:52 +08:00
|
|
|
|
declare new_password
|
2024-09-24 17:59:47 +08:00
|
|
|
|
declare user_name
|
2024-11-18 23:12:52 +08:00
|
|
|
|
read -p "请输入需要修改密码的用户名:" user_name
|
2024-09-25 00:51:25 +08:00
|
|
|
|
if ! id $user_name &> /dev/null;then
|
2024-09-24 17:59:47 +08:00
|
|
|
|
echo "系统内没有该用户"
|
|
|
|
|
exit
|
|
|
|
|
fi
|
2024-11-18 23:12:52 +08:00
|
|
|
|
read -p "请输入${user_name}的新密码:" new_password
|
|
|
|
|
echo "${user_name}:${new_password}" |chpasswd
|
2024-09-24 17:59:47 +08:00
|
|
|
|
if [[ ${user_name} == "root" ]]; then
|
|
|
|
|
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
|
|
|
|
|
fi
|
|
|
|
|
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config
|
|
|
|
|
systemctl restart sshd.service
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "修改成功, 用户 ${user_name} 的新密码为:${new_password}"
|
2024-09-24 17:59:47 +08:00
|
|
|
|
;;
|
|
|
|
|
esac
|