Skip to content
Home » Automating User Management with Bash Scripts

Automating User Management with Bash Scripts

Creating User Accounts

Creating user accounts with Bash Scripts can be done quickly and easily. An example:

#!/bin/bash

username="[username]"
useradd -m $username
passwd $username

This code block creates a user account with the specified username. The useradd command adds the user while the passwd command sets the password.

The user can then be added to existing groups with the usermod command. To do this, simply list the group names separated by spaces, like this:

#!/bin/bash

username="[username]"
usermod -a -G [group1] [group2] $username

This code block adds the specified user to the specified groups. The usermod command modifies the user account while the -a flag adds the user to the specified groups.

Deleting User Accounts

Deleting user accounts with Bash Scripts can also be done quickly and easily. An example:

#!/bin/bash

username="[username]"
userdel -r $username

This code block deletes a user account with the specified username. The userdel command deletes the user while the -r flag removes the user’s home directory, as well.

The userdel command can also be used to delete multiple user accounts at once. To do this, simply list the usernames separated by spaces, like this:

#!/bin/bash

userdel -r [username1] [username2] [username3]

Modifying User Accounts

Modifying user accounts with Bash Scripts can be done with a few simple commands. An example code block is shown below.

#!/bin/bash

username="[username]"
usermod -a -G [group] $username

This code block adds the specified user to the specified group. The usermod command modifies the user account while the -a flag adds the user to the specified group.

The usermod command has many other useful flags and options. For example, the -L flag can be used to lock a user account and the -U flag can be used to unlock a user account. Additionally, the -e flag can be used to set an expiration date for a user account.

Leave a Reply

Your email address will not be published. Required fields are marked *

four × 1 =