Working with any Linux distro means you’ll be spending a lot of your time at the prompt. So, I’m sure you’ve made it your own over time. Maybe added some colours, the time, important information like the current path, etc. Plus, I’m sure you’ve created a few command aliases that make your life easier. But, where do you put this stuff so it’s applied even when you quickly switch to a different user, say root, for some admin work? When I first started out, I found that everyone seems to recommend putting these preferences in a different place. Most people suggested /etc/profile while others staunchly advocated the ~/.bashrc file. I’m going to tell you why I chose option #3, /etc/bash.bashrc.
If you google anything about customizing your prompt, you will find people telling you to put the PS1 line in /etc/profile so that it becomes the default for all users. This works great and does indeed become the default unless overridden by the user’s local ~/.bashrc. That’s as it should be, users should always have control over stuff like that. Same deal with command aliases. So I did this for a few months until I got irritated…
Switching users – what happened to my aliases???
The vast majority of systems I work on are servers that provide one or more services. These are devices such as web servers, database providers, file sharing hosts, etc. This means they have a root user, several service accounts and one or more user accounts that have SUDO rights. Well, I’m sure you’ve guessed where I’m going with this. My command aliases, in particular, worked perfectly if I logged in as me or as the root user. But, every time I switched users I lost all my aliases and had to remember the “real commands”. Annoying! Working as me, then su -s for quick root access – nope, goodbye aliases! Logged in as root and need to switch to a user account to maintain permissions? SU - user… oops, no more aliases and a different prompt! There must be a better way…
What’s going on?
Well, I got angry enough that I carried out a little experiment to see what files are read in what circumstances when switching users. I’ll spare you the process (it’s not very interesting) but instead, I’ll summarize the results in a quick table:
|method||/etc/profile (1)||/etc/bash.bashrc (2)||~/.bashrc (3)||priority|
|su - root||Y||Y||Y||3,1,2|
Well, well, well… enlightening. This cleared up a lot. Obviously, I could maintain my preferences across users if they all had the same ~/.bashrc since that is always read and applied with a top priority. But, that means I have to remember to change the ~/.bashrc for each service account, user, etc. I’ll never remember to do that. The generic advice of using /etc/profile also fails here since that file is not read when switching users via su or sudo -s (my preference). However, it seems that one file is always read upon invocation of any bash session, right?
Regardless of how a bash session is started, it reads this particular file. That means that any prompts specified here and any command aliases are always read and applied regardless of whether it’s a local interactive login or just switching users via sudo or su. Yes! Finally, one place I can add my preferences and have them stick across users! Plus, it’s still overridden by the local ~/.bashrc so individual users aren’t forced into anything they don’t want. Well, that’s how I dealt with this annoying issue. Now I can use my familiar aliases in my account and use them when I need to quickly switch to a service account or hang out as root for a bit. Plus, I learned a little something about which files are read and in what order, so I thought I’d pass that nugget of knowledge on to you too!
Thanks for reading my techie-thoughts on this issue. Have any comments or suggestions? Want to add your tips? Things you want me to cover in a future article? Comment below!