🖥️ Creating a New Linux User with Bash as Default Shell 🎯

Saurabh NamdeoSaurabh Namdeo
3 min read

Here’s a blog post draft on how to create a user in Linux and ensure they are added to the Bash shell instead of the default sh:


Welcome back! Today, we're tackling a common scenario you might encounter when adding new users to a Linux system. By default, when you create a user using the useradd command, they might be assigned the sh shell. However, if you want them to use bash, the more feature-rich and widely-used shell, you’ll need to specify this. Let’s dive into how to do it! 🚀

Understanding the Issue 🤔

When you create a user with the sudo useradd command, Linux assigns them a default shell, often sh (the Bourne shell). While sh is functional, bash (Bourne Again Shell) offers more features like command history, auto-completion, and better scripting capabilities. Most users prefer bash, so let’s make sure your new users start with it right away.

Example Scenario

You create a user with the command:

However, when the user logs in, they find themselves in the sh shell, not bash.

Creating a User with Bash as the Default Shell 🛠️

To ensure a user is created with bash as their default shell, you can use the -s option with the useradd command, specifying the path to the bash shell.

Step-by-Step Guide:

  1. Create a New User with Bash as Default:

    Use the -s flag followed by the path to the bash shell (/bin/bash) when creating the user:

     sudo useradd -s /bin/bash newusername
    
    • -s /bin/bash: This sets /bin/bash as the default shell for the new user.

    • newusername: Replace this with the desired username.

  2. Set a Password for the User:

    After creating the user, set a password so they can log in:

     sudo passwd newusername
    

    You’ll be prompted to enter and confirm the password.

  3. Verify the Default Shell:

    To ensure that the user is using bash, you can check the /etc/passwd file, which stores user information:

     grep newusername /etc/passwd
    

    You should see an entry similar to this:

     newusername:x:1001:1001::/home/newusername:/bin/bash
    

    The last part of the line (/bin/bash) confirms that bash is the default shell for this user.

Changing an Existing User’s Shell to Bash 🔄

If you’ve already created a user and they’re assigned to sh, you can change their default shell to bash using the chsh (change shell) command.

Step-by-Step Guide:

  1. Change the User’s Shell:

    Run the following command:

     sudo chsh -s /bin/bash existingusername
    
    • -s /bin/bash: This specifies the new shell path.

    • existingusername: Replace this with the username of the user whose shell you want to change.

  2. Verify the Change:

    Check the /etc/passwd file to confirm the change:

     grep existingusername /etc/passwd
    

    You should now see /bin/bash as the default shell for this user.

Conclusion 🎯

Setting bash as the default shell for new and existing users is a small but important step to ensure a better user experience on Linux systems. Whether you’re creating new users or modifying existing ones, the -s option with useradd and the chsh command make it easy to assign bash as the default shell.

Next time you’re managing users, remember these commands to ensure they start with the best shell for their needs. Stay tuned for more tips and tricks as we continue our DevOps journey! 🚀

0
Subscribe to my newsletter

Read articles from Saurabh Namdeo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurabh Namdeo
Saurabh Namdeo