The script creates a new user with the specified username, disables the password, and sets up SSH access for the user. It is designed to be run in a terminal on a Linux system with sudo privileges.
- A Linux system with
sudoprivileges. - Bash shell.
To use the script, follow these steps:
-
Open a terminal.
-
Copy the script into a file, for example,
create_user.sh.#!/bin/bash # Check if a username was provided as an argument if [ -z "$1" ]; then echo "Usage: $0 <username>" exit 1 fi # Assign the first argument to the USERNAME variable USERNAME=$1 # Create the new user sudo adduser --disabled-password --gecos "" "$USERNAME" # Remove the password sudo passwd -d "$USERNAME" # Set password expiry and other properties sudo chage -I -1 -m 0 -M 99999 -E -1 "$USERNAME" # Switch to the new user to set up SSH sudo su - "$USERNAME" -c " mkdir -p ~/.ssh chmod 700 ~/.ssh touch ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys " echo "User '$USERNAME' has been created and configured."
-
Make the script executable:
chmod +x create_user.sh
-
Run the script with the desired username:
./create_user.sh <username>
Replace
<username>with the desired username for the new user.
- Checks for Username: The script checks if a username is provided as an argument. If not, it displays usage instructions and exits.
- Creates User: It creates a new user with the specified username, disabling the password and setting the user information to empty.
- Removes Password: The password for the new user is removed, allowing for passwordless login.
- Sets Password Expiry: Configures the password expiry settings for the user.
- Sets Up SSH Access: Creates the
.sshdirectory andauthorized_keysfile for the new user, setting appropriate permissions.
- Ensure that you have the necessary permissions to run the script with
sudo. - The script does not set a password for the new user, which may not be suitable for all environments. Consider security implications before using passwordless accounts.
- You may need to manually add SSH keys to the
~/.ssh/authorized_keysfile for the new user to enable SSH access.