-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_user.bash
More file actions
161 lines (136 loc) · 4.59 KB
/
create_user.bash
File metadata and controls
161 lines (136 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
# Set bash execution flags:
# - Treat unset variables as an error when substituting
# - Exit immediately if a command exits with a non-zero status
# - Print each command to stdout before executing it (useful for debugging)
set -u
# set -e
# set -x
# ANSI Colors
RED='\033[0;31m' # Error
GREEN='\033[0;32m' # Success
BLUE='\033[0;34m' # Info
YELLOW='\033[0;93m' # Warning/Useful info
NC='\033[0m' # No Color
# Show usage instructions
# Usage example: show_usage
show_usage() {
echo "= = Usage = ="
echo " Directly in CLI:"
echo " $0 username [--add-to-sudo] [password]"
echo " - username: Name of the user to be created."
echo " - --add-to-sudo: (Optional)"
echo " - password: (Optional) Password for the new user.If not provided, a random password will be generated."
echo " From WEB:"
echo " To run the script from the internet use:"
echo " curl:"
echo " $ SCRIPT_URL='https://raw.githubusercontent.com/voiduin/linux-host-setup/main/create_user.bash';\\"
echo " curl -Ls \"\${SCRIPT_URL}\" | sudo bash -s username [--add-to-sudo] [password]"
echo " wget:"
echo " $ SCRIPT_URL='https://raw.githubusercontent.com/voiduin/linux-host-setup/main/create_user.bash';\\"
echo " wget -qO - \"\${SCRIPT_URL}\" | sudo bash -s username [--add-to-sudo] [password]"
echo -e "\n"
echo "This script creates a new user with the specified username and password."
echo "If the password is not provided, it generates a random password for the user."
}
# Function to ensure a user does not already exist
assert_user_not_exists() {
local username="$1"
local user_exists=$(id "$username" &>/dev/null && echo "yes" || echo "no")
if [[ $user_exists == "yes" ]]; then
exit_with_err "The user already exists: $username"
fi
}
# Exit with an error message and show usage
# Usage example: exit_with_err "Error message"
exit_with_err() {
local message="$1"
echo -e "${RED}Error: $message${NC}"
echo -e "\n"
show_usage
exit 1
}
# Ensure the script is run as root
# Usage example: assert_run_as_root
assert_run_as_root() {
if [[ $EUID -ne 0 ]]; then
exit_with_err "This script must be run as root"
fi
}
# Function to generate a random password
generate_random_password() {
local password_length=12
echo "$(openssl rand -base64 $password_length)"
}
# Function to create a new user with a password
create_user() {
local username="$1"
local password="$2"
assert_user_not_exists "$username"
if [[ -z $password ]]; then
password=$(generate_random_password)
local password_generated="yes"
else
local password_generated="no"
fi
local hashed_password="$(openssl passwd -1 "$password")"
sudo useradd -m -p "$hashed_password" "$username"
echo -en "${YELLOW}"
echo " REMEMBER: User creation successful:"
echo " - Username: ${username}"
echo -n " - Password: ${password}"
if [[ ${password_generated} == "yes" ]]; then
echo " (randomly generated)"
else
echo " (set by user)"
fi
echo -en "${NC}"
}
# Function to add user to a group
add_user_to_group() {
local user_name="$1"
local group_name="$2"
usermod -aG "${group_name}" "${user_name}"
echo " - User ${user_name} has been added to the ${group_name} group"
}
# Main function to handle script logic
main() {
assert_run_as_root
if [[ $# -lt 1 ]]; then
exit_with_err "ERR: Invalid number of arguments"
fi
local username="$1"
shift
# Default to "no" if the third argument is not provided
local need_add_to_sudo="no"
local need_add_to_docker="no"
local password=""
while [[ $# -gt 0 ]]; do
case "$1" in
--add-to-sudo)
need_add_to_sudo="yes"
;;
--add-to-docker)
need_add_to_docker="yes"
;;
*)
if [[ -z "${password}" ]]; then
password="$1"
else
exit_with_err "ERR: Unexpected argument: $1"
fi
;;
esac
shift
done
create_user "${username}" "${password}"
# Set default user shell - BASH (by default set minimalistic '/bash/sh')
usermod --shell '/bin/bash' "${username}"
if [[ "${need_add_to_sudo}" == "yes" ]]; then
add_user_to_group "${username}" "sudo"
fi
if [[ "${need_add_to_docker}" == "yes" ]]; then
add_user_to_group "${username}" "docker"
fi
}
main "$@"