Script creates sudo user in Linux

by Daniel Pham
Published: Updated:

There are times when you work with linux systems, you have to repeat some boring work. For me, that is creating a sudo account for the user.

Script to do

It does not matter if it’s just one or two servers, but what if you have to create 100 or even 1000 servers? You can not just sit repeatedly typing on 1000 servers.

Of course, if you master automatic support tools such as ansible or chef, you will not need this script.

Script content

#!/bin/bash
# Script by: WriteBash.com
# Script date: 20-12-2017
# Script version: 1.0
# Script is used to create sudo users on Linux systems

# Declare a password for each user
USER_A="usera_pass"
USER_B="userb_pass"

# Function use to create users
create_user () {
    adduser $1
    echo $1:$2 | chpasswd
    echo "$1 ALL=(ALL) ALL" >> /etc/sudoers
}

# Normally, when you create a sudo user, you will disable login root for extra security
disable_root () {
    RECENT=`cat /etc/ssh/sshd_config | grep "PermitRootLogin" | head -n 1`
    sed -i 's|'"$RECENT"'|PermitRootLogin no|g' /etc/ssh/sshd_config
    service sshd restart
}

# Main function
main () {
    # The following command calls "create_user" function and passes the parameter is username and password
    create_user usera $USER_A
    create_user userb $USER_B
    disable_root
}
main

exit

You can download the script here.

Use script

1. Create a new script file and set it’s name is create_sudo_user or any name that you wish. Copy content above and paste to this file.

nano create_sudo_user

2. You declare the user right inside the script as the demo content above, the script content above declared 2 user is usera and userb. You just need to edit the username and password in the script.

3. Grant permission to execute script.

chmod 700 create_sudo_user
demo run script create sudo user
Demo run script create sudo user

4. Execute the script. As you can see in the image above, you see that the usera and userb directories were created in the /home directory.

./create_sudo_user

Conclusion

Hope this script will help you do not have to repeat the task of creating sudo users in a boring way. The reason I love bash scripts is because I do not like repetitive work too many times.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.