Script creates sudo user in Linux

Share this post to your friends !

WriteBash - 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
script-creates-sudo-user-in-linux-01 Script creates sudo user in Linux
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.

If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.