{"id":386412,"date":"2024-11-26T15:43:19","date_gmt":"2024-11-26T15:43:19","guid":{"rendered":"https:\/\/www.ninjaone.com\/?post_type=script_hub&#038;p=386412"},"modified":"2024-11-26T15:43:19","modified_gmt":"2024-11-26T15:43:19","slug":"deaktivieren-von-benutzerkonten-unter-linux","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/de\/script-hub\/deaktivieren-von-benutzerkonten-unter-linux\/","title":{"rendered":"Guide f\u00fcr IT-Experten: Wie man Benutzerkonten in Linux deaktivieren kann"},"content":{"rendered":"<p>Die Verwaltung von Benutzerkonten ist ein wichtiger Aspekt des System-Managements, insbesondere wenn es darum geht, die Sicherheit und Integrit\u00e4t einer Linux-Umgebung zu gew\u00e4hrleisten. <strong>Das Deaktivieren von Benutzerkonten<\/strong>, die nicht mehr verwendet werden oder m\u00f6glicherweise kompromittiert sind, ist ein Standardverfahren unter IT-Experten und <a href=\"https:\/\/www.ninjaone.com\/de\/was-ist-ein-msp\/\" target=\"_blank\" rel=\"noopener\">Managed Service Providern (MSPs)<\/a>.<\/p>\n<p>In diesem Blogbeitrag wird ein umfassendes Skript zum Deaktivieren von Benutzerkonten unter Linux vorgestellt. Wir befassen uns mit einer detaillierten Erkl\u00e4rung seiner Funktionalit\u00e4t, Anwendungsf\u00e4llen und Best Practices.<\/p>\n<h2>Die Bedeutung der Benutzerkontenverwaltung verstehen<\/h2>\n<p>In jeder IT-Infrastruktur ist die Verwaltung von Benutzerkonten essenziell f\u00fcr die <a href=\"https:\/\/www.ninjaone.com\/de\/blog\/checkliste-fuer-mehr-it-sicherheit-in-ihrem-unternehmen-ninjaone\/\" target=\"_blank\" rel=\"noopener\">Aufrechterhaltung der Sicherheit<\/a> und der betrieblichen <a href=\"https:\/\/www.ninjaone.com\/de\/effizienz\/\" target=\"_blank\" rel=\"noopener\">Effizienz<\/a>. Unbefugter Zugriff oder Missbrauch von Benutzerkonten kann zu Datenschutzverletzungen, dem Verlust vertraulicher Informationen und m\u00f6glichen Systemausf\u00e4llen f\u00fchren. Daher ist die Deaktivierung inaktiver oder gef\u00e4hrdeter Benutzerkonten eine proaktive Ma\u00dfnahme zum Schutz des Systems.<\/p>\n<h2>Kontext<\/h2>\n<p>Das Skript, das wir untersuchen, soll ein bestimmtes Benutzerkonto deaktivieren, indem es seine Anmelde-Shell in &#8218;\/sbin\/nologin&#8216; \u00e4ndert und das Konto sperrt. Dieser Vorgang verhindert, dass sich die Benutzer:innen anmelden, w\u00e4hrend das Konto f\u00fcr eine m\u00f6gliche Reaktivierung oder zu Pr\u00fcfungszwecken erhalten bleibt. Dieser Ansatz wird gegen\u00fcber einer vollst\u00e4ndigen L\u00f6schung bevorzugt, da der Kontoverlauf und die zugeh\u00f6rigen Dateien intakt bleiben.<\/p>\n<h2>Das Skript zum Deaktivieren von Benutzerkonten unter Linux<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">#!\/usr\/bin\/env bash\r\n\r\n# Description: Disables a user account by changing its shell to \/sbin\/nologin and locking the account.\r\n#\r\n# Release Notes: Initial Release\r\n# 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.\r\n# 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. \r\n# 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. \r\n# 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. \r\n# Warranty Disclaimer: The script is provided \u201cas is\u201d and \u201cas available\u201d, 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. \r\n# 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. \r\n# 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. \r\n# EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).\r\n#\r\n# Below are all the valid parameters for this script.\r\n#\r\n# Preset Parameter: \"ReplaceMeWithUsernameToDisable\"\r\n#   Username of the user you would like to disable.\r\n#\r\n\r\n# Help text function for when invalid input is encountered\r\nprint_help() {\r\n  printf '\\n### Below are all the valid parameters for this script. ###\\n'\r\n  printf '\\nPreset Parameter: \"ReplaceMeWithUsernameToDisable\" \\n'\r\n  printf '\\t%s\\n' \"Username of the user you would like to disable.\"\r\n}\r\n\r\n# Determines whether or not help text is nessessary and routes the output to stderr\r\ndie() {\r\n  local _ret=\"${2:-1}\"\r\n  echo \"$1\" &gt;&amp;2\r\n  test \"${_PRINT_HELP:-no}\" = yes &amp;&amp; print_help &gt;&amp;2\r\n  exit \"${_ret}\"\r\n}\r\n\r\n_arg_userToDisable=\r\n\r\n# Grabbing the parameters and parsing through them.\r\nparse_commandline() {\r\n  while test $# -gt 0; do\r\n    _key=\"$1\"\r\n    case \"$_key\" in\r\n    --help | -h)\r\n      _PRINT_HELP=yes die 0\r\n      ;;\r\n    --*)\r\n      _PRINT_HELP=yes die \"FATAL ERROR: Got an unexpected argument '$1'\" 1\r\n      ;;\r\n    *)\r\n      if [[ -z $_arg_userToDisable ]]; then\r\n        _arg_userToDisable=$1\r\n      else\r\n        _PRINT_HELP=yes die \"FATAL ERROR: Got an unexpected argument '$1' but user '$_arg_userToDisable' was already specified!\" 1\r\n      fi\r\n      ;;\r\n    esac\r\n    shift\r\n  done\r\n}\r\n\r\n# Parse the command-line arguments passed to the script.\r\nparse_commandline \"$@\"\r\n\r\nif [[ -n $usernameToDisable ]]; then\r\n  _arg_userToDisable=\"$usernameToDisable\"\r\nfi\r\n\r\n# Check if the username to disable is empty and display an error if it is.\r\nif [[ -z $_arg_userToDisable ]]; then\r\n  _PRINT_HELP=yes die \"[Error] The username of the user you would like to disable is required!'\" 1\r\nfi\r\n\r\n# Validate the username to ensure it only contains lowercase letters, digits, hyphens, and underscores.\r\nif [[ ! $_arg_userToDisable =~ ^[a-z0-9_-]+$ ]]; then\r\n  _PRINT_HELP=no die \"[Error] Invalid characters detected in '$_arg_userToDisable' usernames can only have a-z, 0-9 or -, _ characters!\" 1\r\nfi\r\n\r\n# Search for the user in the \/etc\/passwd file and ensure the user account is not already set to 'nologin'.\r\npasswdEntry=$(grep -w \"$_arg_userToDisable\" \/etc\/passwd)\r\nif [[ -z $passwdEntry ]]; then\r\n  _PRINT_HELP=no die \"[Error] User '$_arg_userToDisable' does not exist.\" 1\r\nfi\r\n\r\nunlockedaccount=$(passwd -S \"$_arg_userToDisable\" | cut -f2 -d \" \" | grep -v \"L\")\r\nnologin=$(grep -w \"$_arg_userToDisable\" \/etc\/passwd | cut -d \":\" -f7 | grep \"nologin\")\r\nif [[ -z $unlockedaccount &amp;&amp; -n $nologin ]]; then\r\n  _PRINT_HELP=no die \"[Error] User '$_arg_userToDisable' is already disabled. $nologin\" 1\r\nfi\r\n\r\n# Check if the 'sudo' command is available on the system.\r\nsudoAvailable=$(command -v sudo)\r\n\r\n# If 'sudo' is available, check if the specified user has sudo privileges and is not explicitly forbidden from using sudo.\r\nif [[ -n $sudoAvailable ]]; then\r\n  sudoAccess=$(sudo -l -U \"$_arg_userToDisable\" | grep -v \"is not allowed to run sudo\")\r\nfi\r\n\r\n# Initialize a flag to check for the availability of another administrative user.\r\nanotherAdminAvaliable=false\r\n\r\n# If the user to disable is 'root' or if they have sudo access, proceed to check for other admin users.\r\nif [[ \"$_arg_userToDisable\" == \"root\" || -n $sudoAccess ]]; then\r\n # Fetch all user accounts with UID &gt;= 1000 (typically regular users) and exclude the specified user and 'nobody'.\r\n  allAccounts=$(cut -d \":\" -f1,3 \/etc\/passwd | grep -v -w \"$_arg_userToDisable\" | grep -v \"nobody\" | awk -F ':' '$2 &gt;= 1000 {print $1}')\r\n\r\n  # If the user to disable is not 'root', add 'root' to the list of all accounts if it is enabled and not set to 'nologin'.\r\n  if [[ ! \"$_arg_userToDisable\" == \"root\" ]]; then\r\n    enabled=$(grep -w \"root\" \/etc\/passwd | grep -v \"nologin\")\r\n    if [[ -n $enabled ]]; then\r\n      allAccounts=$(echo \"$allAccounts\"; echo \"root\")\r\n    fi\r\n  fi\r\n\r\n  # Iterate over each account to check if there are other admin users available.\r\n  for account in $allAccounts; do\r\n    # Skip checking accounts if 'sudo' is not available.\r\n    if [[ -z $sudoAvailable ]]; then\r\n      continue\r\n    fi\r\n\r\n    # Check if the current account has sudo access.\r\n    sudoAccess=$(sudo -l -U \"$account\" | grep -v \"is not allowed to run sudo\")\r\n    if [[ -z $sudoAccess ]]; then\r\n      continue\r\n    fi\r\n\r\n    # Check if the current account is enabled (i.e., not set to 'nologin').\r\n    accountEnabled=$(grep -w \"$account\" \/etc\/passwd | grep -v \"nologin\")\r\n    if [[ -z $accountEnabled ]]; then\r\n      continue\r\n    fi\r\n\r\n    # If an admin account is available and enabled, set the flag to true.\r\n    anotherAdminAvaliable=\"true\"\r\n  done\r\n\r\n  # If no other admin users are available, output an error and suggest creating another admin account.\r\n  if [[ $anotherAdminAvaliable == \"false\" ]]; then\r\n    _PRINT_HELP=no die \"[Error] No other admins available. Please create another account to administer the system.\" 1\r\n  fi\r\nfi\r\n\r\n# Attempt to change the shell of the user to \/sbin\/nologin to disable login capabilities.\r\nif ! usermod \"$_arg_userToDisable\" -s \/sbin\/nologin; then\r\n  _PRINT_HELP=no die \"[Error] Failed to change the shell for '$_arg_userToDisable' to \/sbin\/nologin.\" 1\r\nfi\r\n\r\n# Attempt to lock the user account using usermod.\r\nif ! usermod -L \"$_arg_userToDisable\"; then\r\n  _PRINT_HELP=no die \"[Error] Failed to lock '$_arg_userToDisable' using usermod.\" 1\r\nfi\r\n\r\n# Check if the user has been successfully disabled by confirming 'nologin' is set.\r\ndisabledUser=$(grep -w \"$_arg_userToDisable\" \/etc\/passwd | grep \"nologin\")\r\nif [[ -n $disabledUser ]]; then\r\n  echo \"Successfully disabled '$_arg_userToDisable'.\"\r\nelse\r\n  _PRINT_HELP=no die \"[Error] Failed to disable '$_arg_userToDisable'.\" 1\r\nfi\r\n<\/pre>\n<p>&nbsp;<\/p>\n\n<div class=\"in-context-cta\"><p>Greifen Sie auf \u00fcber 300 Skripte im NinjaOne Dojo zu.<\/p>\n<p><a href=\"https:\/\/www.ninjaone.com\/freetrialform\/\">Zugang erhalten<\/a><\/p>\n<\/div>\n<h2>Detailansicht<\/h2>\n<h3><em>Ersteinrichtung und Parameter-Parsen<\/em><\/h3>\n<p>Das Skript beginnt mit der Definition einer Hilfetextfunktion (print_help) und einer Stop-Funktion (die) zur Behandlung von Fehlern und zur Anzeige der entsprechenden Warnmeldungen. Die Funktion &#8218;parse_commandline&#8216; verarbeitet Befehlszeilenargumente, um den Benutzernamen des zu deaktivierenden Kontos zu ermitteln.<\/p>\n<h3><em>Validierung des Benutzerkontos<\/em><\/h3>\n<p>Das Skript pr\u00fcft dann, ob der angegebene Benutzername g\u00fcltig ist und im System existiert. Es stellt sicher, dass der Benutzername nur erlaubte Zeichen enth\u00e4lt (Kleinbuchstaben, Ziffern, Bindestriche und Unterstriche) und \u00fcberpr\u00fcft seine Existenz in der Datei &#8218;\/etc\/passwd&#8216;.<\/p>\n<h3><em>Es wird sichergestellt, dass keine Konflikte mit Admin-Konten auftreten.<\/em><\/h3>\n<p>Das Skript \u00fcberpr\u00fcft au\u00dferdem, ob die zu deaktivierenden Benutzer:innen \u00fcber Administratorrechte verf\u00fcgen. Wenn es der Fall ist, sucht das Skript nach anderen Administratorkonten, um zu verhindern, dass wichtige administrative Zugriffe gesperrt werden.<\/p>\n<h3><em>Deaktivieren des Benutzerkontos<\/em><\/h3>\n<p>Schlie\u00dflich deaktiviert das Skript das Benutzerkonto, indem es die Shell in &#8218;\/sbin\/nologin&#8216; \u00e4ndert und das Konto sperrt. Es pr\u00fcft dann den Erfolg dieser Vorg\u00e4nge und gibt Feedback.<\/p>\n<h2>Potenzielle Anwendungsf\u00e4lle<\/h2>\n<p>Stellen Sie sich ein Szenario vor, in dem ein IT-Experte das Konto eines Mitarbeiters deaktivieren muss, der das Unternehmen verlassen hat. Das Skript bietet eine optimierte und effiziente Methode, um zu gew\u00e4hrleisten, dass das Konto deaktiviert wird, ohne dass man es auch l\u00f6scht. Auf diese Weise kann das IT-Team die Daten und den Kontoverlauf der Benutzer:innen f\u00fcr Compliance- und Audit-Zwecke aufbewahren.<\/p>\n<h2>Vergleiche mit anderen Methoden<\/h2>\n<p>Der herk\u00f6mmliche Ansatz zur Deaktivierung eines Benutzerkontos kann die manuelle Bearbeitung der Datei &#8218;\/etc\/passwd&#8216; oder die Ausf\u00fchrung mehrerer Befehle beinhalten. Dieses Skript automatisiert den Prozess, verringert das Risiko menschlichen Versagens und gew\u00e4hrleistet die Konsistenz zwischen verschiedenen Systemen.<\/p>\n<h2>FAQs<\/h2>\n<h3>F: Kann dieses Skript ein Benutzerkonto l\u00f6schen?<\/h3>\n<p>A: Nein, das Skript deaktiviert das Konto nur, indem es die Shell \u00e4ndert und sperrt. Das L\u00f6schen eines Kontos w\u00fcrde zus\u00e4tzliche Befehle erfordern.<\/p>\n<h3>F: Ist es sicher, das Root-Konto zu deaktivieren?<\/h3>\n<p>A: Das Deaktivieren des Root-Kontos kann dazu f\u00fchren, dass das System nicht mehr verwaltbar ist. Das Skript enth\u00e4lt \u00dcberpr\u00fcfungen, um daf\u00fcr zu sorgen, dass ein anderes Administratorkonto verf\u00fcgbar ist, bevor es fortf\u00e4hrt.<\/p>\n<h3>F: Was ist, wenn das Benutzerkonto bereits deaktiviert ist?<\/h3>\n<p>A: Das Skript pr\u00fcft den Kontostatus und meldet, wenn das Konto bereits deaktiviert ist, um redundante Vorg\u00e4nge zu vermeiden.<\/p>\n<h2>Auswirkungen auf die IT-Sicherheit<\/h2>\n<p>Die Deaktivierung inaktiver oder kompromittierter Benutzerkonten ist eine wichtige Sicherheitsma\u00dfnahme. Es reduziert die Angriffsfl\u00e4che und verhindert unbefugten Zugriff, wodurch die allgemeine Sicherheitslage des Unternehmens verbessert wird.<\/p>\n<h2>Empfehlungen<\/h2>\n<p>Wenn Sie dieses Skript verwenden, stellen Sie sicher:<\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"1\" data-aria-level=\"1\">Dass Sie ein anderes Administratorkonto zur Verf\u00fcgung haben.<\/li>\n<\/ul>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"2\" data-aria-level=\"1\">Dass Sie Ihre Richtlinien zur Verwaltung von Benutzerkonten regelm\u00e4\u00dfig \u00fcberpr\u00fcfen und aktualisieren.<\/li>\n<\/ul>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"3\" data-aria-level=\"1\">Dass Sie das Skript in einer Staging-Umgebung testen, bevor Sie es in einer Live-Umgebung einsetzen.<\/li>\n<\/ul>\n<h2>Abschlie\u00dfende \u00dcberlegungen<\/h2>\n<p><a href=\"https:\/\/www.ninjaone.com\/de\/script-hub\/\" target=\"_blank\" rel=\"noopener\">Die Verwaltung von Benutzerkonten<\/a> ist ein grundlegender Aspekt des System-Managements. Dieses Skript vereinfacht das Deaktivieren von Benutzerkonten unter Linux und bietet eine leistungsstarke und automatisierte L\u00f6sung. Tools wie <a href=\"https:\/\/www.ninjaone.com\/de\/\" target=\"_blank\" rel=\"noopener\">NinjaOne<\/a> k\u00f6nnen den IT-Betrieb weiter optimieren und bieten umfassende Verwaltungs- und \u00dcberwachungsfunktionen, um daf\u00fcr zu sorgen, dass Ihre Systeme sicher und effizient bleiben.<\/p>\n<p>Durch die Einhaltung von Best Practices und den Einsatz automatisierter Skripte k\u00f6nnen IT-Experten eine sichere und gut verwaltete Linux-Umgebung aufrechterhalten, die ihre Infrastruktur vor potenziellen Bedrohungen sch\u00fctzt und eine reibungslose Betriebskontinuit\u00e4t gew\u00e4hrleistet.<\/p>\n","protected":false},"author":35,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","_lmt_disableupdate":"no","_lmt_disable":""},"operating_system":[4211],"use_cases":[4309],"class_list":["post-386412","script_hub","type-script_hub","status-publish","hentry","script_hub_category-linux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/de\/wp-json\/wp\/v2\/script_hub\/386412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/de\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/de\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/de\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/de\/wp-json\/wp\/v2\/comments?post=386412"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/de\/wp-json\/wp\/v2\/media?parent=386412"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/de\/wp-json\/wp\/v2\/operating_system?post=386412"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/de\/wp-json\/wp\/v2\/use_cases?post=386412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}