1. Adding new users: The useradd command adds a new user account. This is useful when adding many users at once. For example, to add 500 users named “user_” followed by the next sequential number (starting from 1), you can use this script:
#!/bin/bash
for i in {1..500}; do useradd -m -s /sbin/nologin user_; done
This will add a new user named “user_” followed by the next sequential number. The “-m” option creates a home directory for the new user, and the “-s /sbin/nologin” option sets the shell to nologin (which prevents users from logging in via SSH). 2. Modifying existing users: To modify an existing user account, you can use the chpasswd command. This is useful when modifying many user accounts at once. For example, to change the password for 500 users named “user_” followed by the next sequential number (starting from 1), you can use this script:
#!/bin/bash
for i in {1..500}; do echo -e “$i\nnewpassword\nnewpassword” | chpasswd user_; done
This will change the password for each of the users named “user_” followed by the next sequential number. The first line is the new password, and the second line is a confirmation of the new password. 3. Deleting existing users: To delete an existing user account, you can use the deluser command. This is useful when deleting many user accounts at once. For example, to delete 500 users named “user_” followed by the next sequential number (starting from 1), you can use this script:
#!/bin/bash
for i in {1..500}; do deluser -f user_; done
This will delete each of the users named “user_” followed by the next sequential number. The “-f” option forces the deletion, even if there are files owned by the user that need to be deleted as well. 4. Listing existing users: To list all existing user accounts, you can use the getent command. This is useful when listing many user accounts at once. For example, to list 500 users named “user_” followed by the next sequential number (starting from 1), you can use this script:
#!/bin/bash
for i in {1..500}; do getent passwd | grep user_; done
This will list each of the users named “user_” followed by the next sequential number. The output is formatted as follows: username:password:uid:gid:fullname:homedir:shell
Note that the password field is not displayed, but it can be included if desired using the -s option with getent passwd. 5. Searching for existing users: To search for a specific user account, you can use the grep command in combination with the getent passwd command. This is useful when searching for many user accounts at once. For example, to search for 100 users named “user_” followed by any number (starting from 1), you can use this script:
#!/bin/bash
for i in {1..100}; do grep -i user_[[:digit:]]+ getent passwd | awk ‘{print $1}’; done
This will search for each of the users named “user_” followed by any number (starting from 1). The output is formatted as follows: username
Note that this script uses grep with the -i option to perform a case-insensitive search, and awk to extract only the username.