- 10 Oct 2024
- 1 読む分
- 印刷する
- PDF
Lock User account
- 更新日 10 Oct 2024
- 1 読む分
- 印刷する
- PDF
Use this script to lock a user account on Linux machines.
Copy and save the contents below to a UTF-8 editor like notepad++ OR Sublime Text in Windows or gedit in Ubuntu.
If you are using notepad++ then use the bottom right panel to change the type to Unix (LF).
Or click here to download the file.
In the script, replace the following placeholder:
Provide the username and hostname of the user whose account you want to lock.
# Define variablesusername="ENTER_USER_NAME"
hostname="ENTER_USER_HOSTNAME"
#!/bin/bash # Define variables username="ENTER_USER_NAME" hostname="ENTER_USER_HOSTNAME" # Check if hostname is present if grep -q "$hostname" /etc/passwd; then # Check if user is present if id "$username" &>/dev/null; then # Lock user account sudo passwd -l "$username" echo "User $username has been locked." # Note: Unsaved data may be lost or may result in data corruption # Log out the user if in an active session sudo pkill -KILL -u "$username" echo "User $username has been logged out." else echo "User $username does not exist." fi else echo "Hostname $hostname not found. Exiting..." exit 1 fi
If Hostname is invalid the script will not execute.
If Hostname is valid, it will next check Username and if Username is invalid or not present, script will not execute.
If both Hostname and Username are valid then only script will be executed.
Follow our guide to upload & publish the script using Scalefusion Dashboard.
Note:
Some of the scripts and their contents are sourced from internet and yes, our new friend ChatGPT.
Please validate the scripts on a test machine before deploying them on all your managed devices.
Scalefusion has tested these scripts, however Scalefusion will not be responsible for any loss of data or system malfunction that may arise due to the usage of these scripts.