-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.sh
executable file
·89 lines (76 loc) · 1.76 KB
/
user.sh
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
#! /bin/bash -e
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2024-2025 Red Hat, Inc.
set -euo pipefail
username=""
userid=""
usage() {
cat <<EOF >&2
Usage: $0
-u | --user <username>
-g | --gid <userid>
EOF
exit 1
}
# Parse command-line arguments
args=$(getopt -o u:g: --long user:,gid: -n "$0" -- "$@") || usage
eval set -- "$args"
while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
usage
;;
-u | --user)
username="$2"
shift 2
;;
-g | --gid)
userid="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Unsupported option: $1" >&2
usage
;;
esac
done
# Validate required parameters
if [ -z "$username" ] || [ -z "$userid" ]; then
echo "Error: --user and --gid are required." >&2
usage
fi
USER_NAME="$username"
USER_UID="$userid"
USER_GID="$USER_UID"
HOME_DIR="/home/$USER_NAME"
# Exit if the user is root
if [ "$USER_NAME" = "root" ]; then
exit 0
fi
if ! getent group "$USER_NAME" >/dev/null; then
groupadd --gid "$USER_GID" "$USER_NAME"
fi
if ! getent passwd "$USER_NAME" >/dev/null; then
useradd --uid "$USER_UID" --gid "$USER_GID" -m "$USER_NAME"
fi
# Ensure $HOME exists when starting
if [ ! -d "${HOME_DIR}" ]; then
mkdir -p "${HOME_DIR}"
fi
# Add current (arbitrary) user to /etc/passwd and /etc/group
if ! whoami > /dev/null 2>&1; then
if [ -w /etc/passwd ]; then
echo "update passwd file"
echo "${USER_NAME:-user}:x:$(id -u):0:${USER_NAME:-user} user:${HOME}:/bin/bash" >> /etc/passwd
echo "${USER_NAME:-user}:x:$(id -u):" >> /etc/group
fi
fi
# Fix up permissions
chown "$USER_NAME:$USER_GID" -R "/home/$USER_NAME"
chown "$USER_NAME:$USER_GID" -R /opt
mkdir -p "/run/user/$USER_UID"
chown "$USER_NAME:$USER_GID" "/run/user/$USER_UID"