Files
for-servers/server/create_user.sh

35 lines
1.3 KiB
Bash
Raw 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
# Цвета
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
RESET='\033[0m'
# Генерируем имя пользователя (10 символов; большие/маленькие буквы)
USERNAME=$(tr -dc 'a-zA-Z' < /dev/urandom | head -c 10)
# Перечень безопасных спецсимволов для пароля
SPECIAL='!@#$%^_-+='
# Генерируем пароль (15 символов; большие/маленькие буквы и спецсимволы)
PASSWORD=$(cat /dev/urandom | tr -dc "a-zA-Z${SPECIAL}" | head -c 15)
# Создаём пользователя с домашней директорией
sudo useradd -m "$USERNAME"
# Устанавливаем пароль пользователю
echo "$USERNAME:$PASSWORD" | sudo chpasswd
# Добавляем пользователя в группу sudo (Ubuntu/Debian)
if grep -q '^sudo:' /etc/group; then
sudo usermod -aG sudo "$USERNAME"
elif grep -q '^wheel:' /etc/group; then
sudo usermod -aG wheel "$USERNAME"
fi
# Цветной вывод
echo -e "${YELLOW}Пользователь создан!${RESET}"
echo -e "${CYAN}Имя пользователя:${RESET} ${GREEN}$USERNAME${RESET}"
echo -e "${CYAN}Пароль: ${RESET} ${RED}$PASSWORD${RESET}"