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.
Table of Contents
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:
1. Easier Identification of User Roles:
2. Reduction of Errors:
3. Enhanced User Experience:
4. Useful in Multi-User Systems:
1. Complexity in Management:
2. Potential for Misconfiguration:
3. Security Considerations:
4. Dependency on Group Membership:
5. Overreliance on Visual Cues:
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.
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.
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.
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.
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:
Global Configuration File /etc/bash.bashrc:
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:
Function add_or_update_ps1:
Check Group Membership and Apply 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
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:
If your system doesn't have GUI, you can verify it by SSH into the system from other systems.
developer-senthil@ubuntu2204:~$
See? The user's Bash prompt has been changed.
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-
Whether this setup is recommended depends on the context and needs of the system:
Advantages:
Disadvantages:
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.
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 <strong><mark>.bashrc</mark></strong> -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
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:
The above is the detailed content of How To Change Bash Prompt For Specific User Group In Linux. For more information, please follow other related articles on the PHP Chinese website!