Home Linux User Management How To Change Bash Prompt For Specific User Group In Linux

How To Change Bash Prompt For Specific User Group In Linux

Customize Bash Prompt for User Groups in Linux: A Step-by-Step Guide

By sk
Published: Updated: 931 views

We already looked at how to customize the default BASH prompt in Linux. In this tutorial, we will learn how to change the Bash prompt for specific user group in Linux and Unix-like systems.

Before setting up custom BASH prompts to user groups, it is important to understand the advantages and disadvantages of this approach.

Advantages and Disadvantages of Group-specific Command Prompt

Using a group-specific command prompt in Linux can be advantageous in certain scenarios, but it also comes with some considerations. Here are the advantages and disadvantages:

Advantages

1. Easier Identification of User Roles:

  • By customizing the command prompt based on group membership, it becomes easier to identify the role or environment you are working in. For example, a developer prompt instantly informs the user that they are belong to developers group or environment.

2. Reduction of Errors:

  • Custom prompts can help in reducing errors, especially in environments where users switch between multiple roles or servers. For instance, a distinct prompt for admin users can remind them of the higher level of permissions and encourage caution.

3. Enhanced User Experience:

  • For users who belong to multiple groups, a customized prompt provides a clear, visual indication of their current group, enhancing the user experience by making the terminal more informative and user-friendly.

4. Useful in Multi-User Systems:

  • On systems with many users, such customizations can help in quickly identifying the type of users logged in and their access levels, which is beneficial for system administrators.

Disadvantages

1. Complexity in Management:

  • Implementing and managing custom prompts can add complexity, especially on systems with a large number of users or groups. It requires additional scripting and configuration.

2. Potential for Misconfiguration:

  • If not properly set up, such scripts can lead to misconfigurations. For example, incorrectly appending to .bashrc can lead to unwanted repetitions or even syntax errors that might affect the user's environment.

3. Security Considerations:

  • Automatically appending to configuration files like .bashrc could be a security risk if not handled correctly. It's important to ensure that such scripts do not inadvertently open up security vulnerabilities.

4. Dependency on Group Membership:

  • This method relies on the user's group membership, which might change over time. It requires that group memberships are properly managed and up to date.

5. Overreliance on Visual Cues:

  • Users might become over-reliant on these visual cues and make incorrect assumptions about their privileges or environment based on the prompt alone, which could lead to errors, especially if the prompt configuration has issues.

In summary, customizing the command prompt based on group membership can be useful for enhancing user experience and reducing errors in a multi-user or multi-role environment. However, it requires careful implementation and management to avoid complexity, misconfiguration, and potential security issues.

Let us go ahead and see how to change the command prompt for specific user group in Linux and Unix-like systems.

The following steps were tested on Ubuntu 22.04 LTS system. We hope this method might work on other Linux distributions as well.

Change Bash Prompt For Specific User Group

For demonstration purposes, I will create a new group called 'developers' and a new user named 'senthil'. And then I will add the 'senthil' user to the 'developers' group.

As a result, whenever the 'senthil' user logs in, their prompt will automatically change to 'developer-senthil@ubuntu2204:~$'. Let's see how to do it step-by-step.

Step 1 - Creating a Group in Linux

Create the Group:

Run the following command to create a new group named developers:

$ sudo groupadd developers

This command creates a new group called developers. You might need to enter your password if prompted.

Step 2 - Adding a New User and Assigning to the Group

Create a New User:

To create a new user named senthil, use the command:

$ sudo adduser senthil

You will be prompted to set a password for the new user and fill in some optional user information. Fill these out as required.

Add the User to the Group:

To add senthil to the developers group, use:

$ sudo usermod -aG developers senthil

The -aG option adds the user to the group while keeping their existing group memberships.

Step 3 - Set Custom Bash Prompt for Specific User Group

When you want to change the command prompt for users who are members of a specific group, you have two options for where to place the script that checks the user's group and changes the prompt. The choice depends on whether you want the change to apply to a single user or multiple users:

Individual User's .bashrc File:

  • If you want the change to apply only to a specific user, you should add the lines to that user's .bashrc file located in their home directory (~/.bashrc).
  • This approach is user-specific. Each user for whom you want to apply this change will need the script added to their own .bashrc file.
  • For example, if you want only senthil to have a different prompt when he is part of the developers group, you would add the lines only to senthil's .bashrc file.

Global Configuration File /etc/bash.bashrc:

  • If you want this change to apply to all users on the system, you can edit the global /etc/bash.bashrc file.
  • This method will apply the change to every user's environment, but the script will still only change the prompt for users who are in the specified group.
  • This is useful if you have several users in the group and you want the same behavior for all of them without editing each individual .bashrc file.

Before making any changes in the local ~/.bashrc or global /etc/bash.bashrc file, I strongly recommend you to backup them. This allows you to restore the original settings if something goes wrong.

To backup the user's ~/.bashrc file, run:

$ cp ~/.bashrc ~/.bashrc_backup

To backup global bashrc file, run:

$ sudo cp /etc/bash.bashrc /etc/bash.bashrc_backup

After backing up the appropriate bashrc file, open it using your favorite editor.

Here, I am going to apply this method for all users in the system, so I edit the global /etc/bash.bashrc file.

$ sudo nano /etc/bash.bashrc

Add the following lines at the end:

bashrc_file="/home/$(whoami)/.bashrc"
developer_prompt='PS1="developer-\u@\h:\w\$ "'

# Function to add or update PS1 in .bashrc
add_or_update_ps1() {
    prompt_line=$1
    grep -qF -- "$prompt_line" "$bashrc_file" || echo "$prompt_line" >> "$bashrc_file"
}

if id -nG "$(whoami)" | grep -qw "developers"; then
    add_or_update_ps1 "$developer_prompt"
fi

Let us break down the above code and see what each option does.

Define Variables:

  • bashrc_file holds the path to the user's .bashrc file.
  • developer_prompt hold the PS1 strings for users belong to developers group.

Function add_or_update_ps1:

  • This function takes a prompt line as an argument.
  • It uses grep -qF to check if the exact prompt line already exists in .bashrc.
  • The -- ensures that the subsequent string is treated as a literal and not as a command option.
  • If it doesn’t exist (||), the prompt line is appended to .bashrc.

Check Group Membership and Apply Prompt:

  • The script checks if the user is in the developers group.
  • If the user belong to developers group, it calls add_or_update_ps1 with developer_prompt.

In summary, this script changes the command prompt for users who belong to the developers group by appending a custom prompt definition to their .bashrc file. It ensures that the custom prompt is only added once to avoid duplication.

Press CTRL+O followed by CTRL+X to save the file and exit.

Remember, after editing either file, the changes will only take effect when a new shell session is started. Users can either log out and back in, or they can run source ~/.bashrc in their current session to apply the changes immediately.

Apply the changes using command:

$ source /etc/bash.bashrc

Step 4 - Verify Bash Prompt

Now log out and log back in as user 'senthil'. Open your Terminal and you will see the user's prompt has changed to something like this:

Change Bash Prompt For Specific User Group In Linux
Change Bash Prompt For Specific User Group In Linux

If your system doesn't have GUI, you can verify it by SSH into the system from other systems.

developer-senthil@ubuntu2204:~$
Change Bash Prompt For Specific User Group
Change Bash Prompt For Specific User Group

See? The user's Bash prompt has been changed.

Change the Bash Prompt Based on Sudo Group Membership

You can further modify this script to distinguish between different types of users.

Say for example, you can distinguish normal users and administrative users who belong to the sudo group in Linux. This involves modifying the command prompt based on whether the user has sudo privileges.

Add the following lines to the global /etc/bash.bashrc file or to an individual user's .bashrc file:

bashrc_file="/home/$(whoami)/.bashrc"
sudo_prompt='PS1="sudouser-\u@\h:\w\$ "'
normal_prompt='PS1="normaluser-\u@\h:\w\$ "'

# Function to add or update PS1 in .bashrc
add_or_update_ps1() {
    prompt_line=$1
    grep -qF -- "$prompt_line" "$bashrc_file" || echo "$prompt_line" >> "$bashrc_file"
}

if id -nG "$(whoami)" | grep -qw "sudo"; then
    add_or_update_ps1 "$sudo_prompt"
else
    add_or_update_ps1 "$normal_prompt"
fi

This script will change the prompt to sudouser-<username>@hostname: ~$ for users in the sudo group and to normaluser-<username>@hostname: ~$ for users not in the sudo group.

Is this a Recommended Setup?

Whether this setup is recommended depends on the context and needs of the system:

Advantages:

  • Clear Role Identification: Similar to group-specific prompts, this setup helps in immediately identifying whether the current user session has sudo privileges, which can be particularly useful in multi-user environments.
  • Error Prevention: It acts as a constant reminder of the user's privileges, potentially preventing accidental commands with elevated privileges.

Disadvantages:

  • Complexity and Maintenance: Like any customization, it adds a layer of complexity to the system configuration and requires proper maintenance.
  • False Sense of Security: Users might develop a false sense of security or complacency, thinking that the prompt change fully reflects their privileges. However, the actual permissions are more nuanced and context-dependent.
  • Potential for Misconfiguration: Incorrectly implemented, it could lead to confusion or misconfiguration.

In summary, customizing the command prompt to distinguish between normal users and sudo users can be useful in certain environments, especially where quick identification of user privileges is important.

However, it's not universally recommended, as it adds complexity and depends on the specific needs and management capabilities of the system administrators.

Restore .bashrc File to Default Settings

If you encounter problems, you can revert the changes by restoring the .bashrc file from your backup. If you didn't make a backup, you can either manually edit the file again and remove or comment out the custom script that you added in the previous steps.

Also, there is a default version of the .bashrc file in the /etc/skel/ directory in Debian and Ubuntu systems.

$ ls -al /etc/skel/
total 32
drwxr-xr-x   2 root root  4096 Jan  8 18:02 .
drwxr-xr-x 138 root root 12288 Jan  8 17:55 ..
-rw-r--r--   1 root root   220 Jan  6  2022 .bash_logout
-rw-r--r--   1 root root  4116 Jan  8 18:00 .bashrc
-rw-r--r--   1 root root   807 Jan  6  2022 .profile

Copy the default version of ~/.bashrc file to your current version like below:

$ cp /etc/skel/.bashrc ~/

Finally, run the following command to update the changes.

$ source ~/.bashrc

For more details check the following link:

How To Restore .bashrc File To Default Settings In Ubuntu

Conclusion

In this tutorial, we discussed how to set custom bash prompt for users of a certain group, and the advantages and disadvantages of altering the command prompt in Linux with example scripts.

While modifying bash prompts can be useful for specific needs in certain environments, it's generally not recommended for beginners.

It is always a good practice to test this approach in a Virtual machine and weigh the benefits against the potential risks and complexities before implementing these changes.

Related Read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More