k8s-wk/passwd/useradd.sh
2025-04-27 15:41:01 +08:00

40 lines
955 B
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
username=$1
homedir="/data/"
num=1
check_command() {
if [ $? -ne 0 ]; then
let num = $num + 1
exit 1 # 退出脚本并返回错误状态
fi
}
mkdir -p $homedir/$usernames
check_command
chown -R $username:$username $homedir/$username
check_command
useradd -m -d $homedir/$username -s /bin/bash $username
check_command
password= $(openssl rand -base64 48 | cut -c1-6)
echo "$password" | passwd --stdin $username
check_command
chown -R $username:$username $homedir/$username
check_command
usermod -aG docker $username
check_command
newgrp docker
check_command
read -p "是否需要sudo权限(y/n):" yn
if [ $yn = "是" ]; then
usermod -aG sudo $username
fi
echo "用户创建成功!"
echo "================================================================"
IP=$(curl ifconfig.me)
echo "公网IP$IP"
ip=$(hostname -I)
echo "内网IP(默认使用第一个)$ip"
echo "用户名:$username"
echo "密码:$password"