Die wichtigsten Erkenntnisse
- Man kann die Erstellung neuer Admin-Benutzer unter Linux automatisieren: Optimiert den Prozess des Hinzufügens neuer Admin-Benutzer in Linux-Umgebungen.
- Erhöht Effizienz und Genauigkeit: Reduziert den manuellen Aufwand und minimiert das Risiko menschlichen Versagens.
- Skript-Flexibilität: Anpassbar an verschiedene Bedürfnisse, mit Unterstützung unterschiedlicher Benutzer-, Passwort- und Gruppenoptionen.
- Sicherheit im Mittelpunkt: Enthält Passwortverschlüsselung und erfordert Root-Rechte, um die Sicherheit zu erhöhen.
- Vielseitig einsetzbar: Geeignet sowohl für Einzelpersonen als auch für MSP, die mehrere Linux-Systeme verwalten.
- Vergleich mit traditionellen Methoden: Bietet im Vergleich zu manuellen Befehlen oder GUI-Tools eine kohärentere und skalierbarere Lösung.
- Häufig gestellte Fragen: Beantwortet Fragen zur Sicherheit, Kompatibilität und zum Massenbetrieb.
- Auswirkungen auf Sicherheit: Betont die Bedeutung einer angemessenen Verwaltung von Berechtigungen und der Überprüfung der Skriptverwendung.
- Best Practices: Tests, Protokollierung und regelmäßige Überprüfung der Konten wird empfohlen.
- Integration in NinjaOne: Solche Skripte ergänzen umfassendere IT-Management-Plattformen wie NinjaOne.
Die effektive Verwaltung von Linux-Systemen erfordert häufig die Erstellung und Verwaltung von Benutzerkonten, insbesondere von solchen mit Administratorrechten. Für Systemadministrator:innen und IT-Expert:innen ist es wichtig, die Feinheiten dieses Prozesses zu verstehen. Das heute vorgestellte Skript erfüllt diese Anforderung, indem es eine effiziente Methode zum Hinzufügen neuer Administrator:innen in Linux-Umgebungen bietet.
Kontext
In der Welt der IT ist die Verwaltung von Benutzerkonten eine grundlegende Aufgabe. Dieses Skript ist besonders für IT-Expert:innen und Managed Service Provider (MSPs) relevant, die häufig mit mehreren Linux-Servern oder -Workstations arbeiten. Dass man die Erstellung von Admin-Benutzern unter Linux automatisieren kann, spart nicht nur Zeit, sondern verringert auch die Wahrscheinlichkeit menschlichen Versagens. Ihre Bedeutung kann in Szenarien, in denen eine schnelle und sichere Benutzerverwaltung entscheidend ist, nicht unterschätzt werden.
Das Skript
#!/usr/bin/env bash # # Description: Create a new admin user for Linux, by adding the user to the sudo group. # By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use. # Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. # Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. # Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. # Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. # Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. # Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. # EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA). # Usage: [-u|--user <arg>] [-p|--pass <arg>] [-g|--group <arg>] [-d|--disable <arg>] [-h|--help] # -u, --user: User Name for new user account. (no default) # -p, --pass: Password for new user account. (no default) # -g, --group: Name of group to add the new user account to. (default 'sudo') # -d, --disable: Date to disable account. (no default) # -h, --help: Prints help # # When called, the process ends. # Args: # $1: The exit message (print to stderr) # $2: The exit code (default is 1) # if env var _PRINT_HELP is set to 'yes', the usage is print to stderr (prior to $1) # Example: # test -f "$_arg_infile" || _PRINT_HELP=yes die "Can't continue, have to supply file as an argument, got '$_arg_infile'" 4 die() { local _ret="${2:-1}" test "${_PRINT_HELP:-no}" = yes && print_help >&2 echo "$1" >&2 exit "${_ret}" } # Function that evaluates whether a value passed to it begins by a character # that is a short option of an argument the script knows about. # This is required in order to support getopts-like short options grouping. begins_with_short_option() { local first_option all_short_options='uph' first_option="${1:0:1}" test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0 } _arg_user= _arg_pass= _arg_group="sudo" _arg_disable= if [[ -n "${username}" ]]; then _arg_user=$username fi if [[ -n "${password}" ]]; then _arg_pass=$password fi if [[ -n "${group}" ]] && [[ "${group}" != "null" ]]; then _arg_group=$group fi if [[ -n "${disableAfterDate}" ]] && [[ "${disableAfterDate}" != "null" ]]; then _arg_disable=$(date -d "$disableAfterDate" +'%Y-%m-%d') fi # Function that prints general usage of the script. # This is useful if users asks for it, or if there is an argument parsing error (unexpected / spurious arguments) # and it makes sense to remind the user how the script is supposed to be called. print_help() { printf '%s\n' "Create a new admin user." printf 'Usage: %s [-u|--user <arg>] [-p|--pass <arg>] [-g|--group <arg>] [-e|--enable <arg>] [-d|--disable <arg>] [-h|--help]\n' "$0" printf '\t%s\n' "-u, --user: User Name for new user account. (no default)" printf '\t%s\n' "-p, --pass: Password for new user account. (no default)" printf '\t%s\n' "-g, --group: Name of group to add the new user account to. (default 'sudo')" printf '\t%s\n' "-d, --disable: Date to disable account. (no default)" printf '\t%s\n' "-h, --help: Prints help" } # The parsing of the command-line parse_commandline() { while test $# -gt 0; do _key="$1" case "$_key" in # We support whitespace as a delimiter between option argument and its value. # Therefore, we expect the --user or -u value. # so we watch for --user and -u. # Since we know that we got the long or short option, # we just reach out for the next argument to get the value. -u | --user) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_user="$2" shift ;; # We support the = as a delimiter between option argument and its value. # Therefore, we expect --user=value, so we watch for --user=* # For whatever we get, we strip '--user=' using the ${var##--user=} notation # to get the argument value --user=*) _arg_user="${_key##--user=}" ;; # We support getopts-style short arguments grouping, # so as -u accepts value, we allow it to be appended to it, so we watch for -u* # and we strip the leading -u from the argument string using the ${var##-u} notation. -u*) _arg_user="${_key##-u}" ;; # See the comment of option '--user' to see what's going on here - principle is the same. -p | --pass) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_pass="$2" shift ;; # See the comment of option '--user=' to see what's going on here - principle is the same. --pass=*) _arg_pass="${_key##--pass=}" ;; # See the comment of option '-u' to see what's going on here - principle is the same. -p*) _arg_pass="${_key##-p}" ;; # See the comment of option '--user' to see what's going on here - principle is the same. -g | --group) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_group="$2" shift ;; # See the comment of option '--user=' to see what's going on here - principle is the same. --group=*) _arg_group="${_key##--group=}" ;; # See the comment of option '-u' to see what's going on here - principle is the same. -g*) _arg_group="${_key##-g}" ;; # See the comment of option '--user' to see what's going on here - principle is the same. -d | --disable) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_disable="$2" shift ;; # See the comment of option '--user=' to see what's going on here - principle is the same. --disable=*) _arg_disable="${_key##--disable=}" ;; # See the comment of option '-u' to see what's going on here - principle is the same. -d*) _arg_disable="${_key##-d}" ;; # The help argument doesn't accept a value, # we expect the --help or -h, so we watch for them. -h | --help) print_help exit 0 ;; # We support getopts-style short arguments clustering, # so as -h doesn't accept value, other short options may be appended to it, so we watch for -h*. # After stripping the leading -h from the argument, we have to make sure # that the first character that follows corresponds to a short option. -h*) print_help exit 0 ;; *) _PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1 ;; esac shift done } parse_commandline "$@" if [[ -z "${_arg_user}" ]]; then die "FATAL ERROR: User Name is required. '$_arg_user'" 1 fi if [[ -z "${_arg_pass}" ]]; then die "FATAL ERROR: Password is required. '$_arg_pass'" 1 fi if [ "$(id -u)" -eq 0 ]; then if grep -E "^$_arg_user" /etc/passwd >/dev/null; then # User already exists, add them to the group echo "$_arg_user exists!" if usermod -aG "$_arg_group" "$_arg_user"; then echo "User($_arg_user) has been added to $_arg_group group!" else echo "Failed to add a user to $_arg_group group!" exit 1 fi else pass=$(perl -e 'print crypt($ARGV[0], "password")' "$_arg_pass") if useradd -m -p "$pass" "$_arg_user"; then echo "User($_arg_user) has been added to system!" else echo "Failed to add a user!" exit 1 fi if usermod -aG "$_arg_group" "$_arg_user"; then echo "User($_arg_user) has been added to $_arg_group group!" else echo "Failed to add a user to $_arg_group group!" exit 1 fi fi if [[ -n "${_arg_disable}" ]]; then # Expire the user after date usermod -e "$_arg_disable" "$_arg_user" fi else echo "Only root may add a user to the system." exit 2 fi
Greifen Sie auf über 300 Skripte im NinjaOne Dojo zu.
Detaillierte Aufschlüsselung
Skript-Funktionalität:
- Einrichtung der Umgebung: Das Skript beginnt mit einer Shebang-Zeile #!/usr/bin/env bash, die die Umgebung für die Verwendung von Bash als Skriptsprache einrichtet.
- Beschreibung und Verwendung: Es enthält Kommentare, die seinen Zweck erklären: Einen neuen Administrator-Benutzer zu erstellen, indem er ihn einer Gruppe hinzufügt, typischerweise ‘sudo’.
- Funktionsdefinitionen:
- die(): Behandelt Exit-Szenarien und zeigt bei Bedarf Fehlermeldungen an.
- begins_with_short_option(): Hilft beim Parsen von Befehlszeilenoptionen. \
- print_help(): Zeigt die Benutzungsanweisungen an.
- Initialisierung der Variablen: Variablen wie _arg_user, _arg_pass, _arg_group und _arg_disable werden initialisiert.
- Parsen von Befehlszeilenargumenten: Die Funktion parse_commandline() iteriert über die Argumente, um Benutzer, Passwort, Gruppe und Deaktivierungsdatum festzulegen.
- Logik der Benutzererstellung:
- Prüft, ob der Benutzer existiert.
- Wenn nicht, wird ein neuer Benutzer erstellt und der angegebenen Gruppe hinzugefügt.
- Legt optional ein Deaktivierungsdatum für das Benutzerkonto fest.
- Überprüfung der Root-Rechte: Stellt sicher, dass das Skript von einem Benutzer mit Root-Rechten ausgeführt wird, ein notwendiger Schritt zum Erstellen und Ändern von Benutzerkonten.
Mögliche Anwendungsfälle
Nehmen wir an, ein MSP verwaltet eine Vielfalt von Linux-Servern für einen Kunden. Er erhält eine Anfrage zur schnellen Einbindung eines neuen Entwickler-Teams, das Administratorrechte benötigt. Mit diesem Skript kann der MSP auf effiziente Weise individuelle Administratorkonten einrichten und so sicherstellen, dass jedes Teammitglied ohne manuelle Eingriffe den richtigen Zugang erhält, was Zeit spart und Fehler minimiert.
Vergleiche
Die automatisierte Erstellung neuer Admin-Benutzer unter Linux erfordert in der Regel mehrere manuelle Befehle (useradd, passwd, usermod). Dieses Skript automatisiert diese Schritte in einem einzigen, zusammenhängenden Prozess. Im Gegensatz zu GUI-basierten Benutzerverwaltungs-Tools kann dieses Skript in größere Automatisierungs-Workflows integriert werden, was es flexibler und skalierbarer macht.
FAQs
- Wie sichert dieses Skript das Passwort?
- Es verwendet perl, um das Passwort vor der Erstellung des Benutzers zu verschlüsseln, was die Sicherheit erhöht.
- Kann das Skript die Massenerstellung von Benutzern verarbeiten?
- So wie es geschrieben ist, ist es für die Erstellung eines einzelnen Benutzers gedacht. Es kann jedoch für Massenoperationen geändert werden.
- Ist es mit allen Linux-Distributionen kompatibel?
- Das Skript sollte auf den meisten Linux-Distributionen funktionieren, die über die Bash-Shell und Standard-Dienstprogramme für die Benutzerverwaltung verfügen.
Folgen
Dieses Skript ist zwar ein leistungsfähiges Tool für Systemadministrator:innen, aber es ist wichtig, sich über seine Auswirkungen auf die Sicherheit im Klaren zu sein. Die ordnungsgemäße Verwaltung von Benutzerrechten ist für die Systemsicherheit entscheidend. Durch Missbrauch oder Fehler im Skript könnten versehentlich zu viele Berechtigungen erteilt werden.
Empfehlungen
- Testen Sie das Skript immer zuerst in einer nicht produktiven Umgebung.
- Implementieren Sie Protokollierung, um die Verwendung und Ausgabe des Skripts zu verfolgen.
- Überprüfen Sie regelmäßig die vom Skript erstellten Benutzerkonten und kontrollieren Sie diese.
Abschließende Überlegungen
Abschließend zeigt dieses Skript beispielhaft, wie Automatisierung administrative Aufgaben in Linux-Umgebungen rationalisieren kann. Für eine umfassende IT-Verwaltung und -Automatisierung stellen Lösungen wie NinjaOne die notwendigen Tools und die Infrastruktur zur Verfügung. Sie bieten robuste Unterstützung für Aufgaben wie diese und gewährleisten Effizienz und Sicherheit bei der Verwaltung von IT-Umgebungen.