How to Avoid Locking Yourself Out of Linux

Writing bash scripts to automate user account management

Steven Rescigno
Dec 1, 2020 · 3 min read
Image for post
Image for post
Photo by Michael Dziedzic on Unsplash

“I’ve locked myself out of my VM — I can’t access it!”

It can happen. It happens to every developer at least once in their lifetime.

Let’s find a way to avoid having to start setting up all over again, or purging your entire virtual machine because you’ve been locked out.

It’s common when working with Virtualization there will be a need to manage users and accounts manually but this is not very efficient. Let's write a bash script that can handle authentication for a new User.

We begin by initializing the script as a file that can be accessed by Bash on our machine, using the hashbang .

This hashbang will set up the program loader with an instruction to run the program or then pass as the first argument.

Let’s use the following in our own script.

#!/env/bin/bash

This will be at the top of , the file we will need to create.

Next, we will write a small bash statement to handle our authentication — like a champ!

It will allow only with Permissions from the to make additional with the exact same level of access. This will give the new users Permissions.

# CHECK SUDO USER GROUP ASSOCIATION 
if ! [ $(id -u) = 0 ]; then
echo "I am not root! You cannot give sudo power to a new user!"
exit 1
else
echo "You have granted Sudo powers to a New user"
sudo adduser developer
sudo usermod -aG sudo developer
su - developer
fi

Thus, we’ve now allowed the current user to add a new user named .

Only when this Bash script is ran directly on the machine, will we see these affects.

Thanks to this script being written specifically for users with Sudo permissions you won’t be able to run it correctly unless you use with root permissions. Let’s run it with Sudo and give it a try.

With this new user added to the boot loader of the system you will be asked a few questions to verify their permissions:

Enter the new value, or press ENTER for the defaultFull Name []: Room Number []: Work Phone []:Home Phone []:Other []:

These questions can be entered via the command line prompt/terminal and will be for your own reference.

It’s recommended to save this information for your own records but this can also be skipped by using the Enter key to continue.

Once you’ve filled out these values, you’ll be presented with an option to accept these changes into the bootloader of your system.

Is the information correct? [Y/n] 

Type and hit to proceed with finalizing the new user account.

As you can see in the bash script, we’re calling the on this new user account. This is because we need to give permissions to the newly created account. You can leave this command out or modify this line in the script if you don’t want a user to have permissions.

The script should then hand off the new user account to your bash session via .

The SU command allows for your current running user to be switched to another user within the same session. Note: a password must be provided in order to swiftly switch to a different user. Without entering a password, the user session will return to the current user and will not switch.

With that said, you have successfully run a script that can create users for you. It also protects you from locking yourself out because you cannot successfully change your own user permissions or alter the state of other users without Sudo permissions already enabled.

Happy coding!

Better Programming

Advice for programmers.

Sign up for The Best of Better Programming

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store