Profiles and RC Files

I love Linux, and if you're reading this, chances are you do too. To be honest though, some aspects of the Linux environment are confusing. Near the top of the list for me is the profile system. Conceptually, it's simple. There are system-wide settings that all users inherit, and then there are individual settings people can set on their own. The problem comes when different distributions handle profiles in different ways, and the concept of login shells versus interactive shells comes into play. Usually, it's not something Linux users worry about. But, when you need to make a change, it can be extremely frustrating to figure out what is loaded in what order, and which is seen by login shells only, and so on.

Login Shells

First, let me clarify what I mean by login shells. You've probably noticed that sometimes in order to get to a terminal shell, you're prompted for a user name and password. Other times, you just click on the terminal icon, and you're presented with a terminal already logged in. You'll most often experience this when using a GUI desktop environment. Basically, if you're already logged in to your Linux desktop, and you open a terminal window, it's an interactive shell.

It doesn't have to be inside a graphical desktop environment, however. If you ssh in to a remote server, you're prompted for a user name and password (thus, a login shell). If you then type bash from inside that SSH session, you're starting a brand-new terminal, but this time, it's an interactive shell (notice you're not prompted for a password). Why it matters is something I'll talk about a little later, but for comprehension sake, just remember that if you're prompted for a user name and password, it's most likely a login shell. If you go directly to a bash prompt, it's most likely an interactive shell. The one fairly common exception to this is if you've set up SSH keys to log in automatically. In that case, even though you aren't prompted for a user name and password, it's still a login shell. It's a pretty safe bet that if you're using SSH to log in, it's a login shell.

The Login Shell Process

The login shell process is far more complicated than interactive shells, so I am going to go over that process first. I'm assuming your users have a bash shell assigned in their /etc/passwd files. It's the most common shell for users to have, so it makes sense to be familiar with its nuances.

Step 1: when you authenticate in to a login shell, the system looks for a file called /etc/profile. That file is a shell script that assigns a few environment variables all users should have set.

Step 2: the /etc/profile script usually ends by calling any shell scripts in the /etc/profile.d folder and executing them as well. Often it will run only shell scripts in /etc/profile.d that end with a .sh extension, so look at the /etc/profile script to see how files should be formatted to run properly. Having a folder to add custom scripts is important, because if you have system-wide changes you'd like added to everyone's login shell, adding commands to the /etc/profile file is dangerous. Any system updates affecting /etc/profile will overwrite your changes. If you simply add a custom file into the /etc/profile.d folder, it will be read by the updated /etc/profile script even if it's updated.

Step 3: the /etc/profile script also executes the user's personal profile. This part is a little messy, as the user profile might be called different things depending on distribution and/or user customization. In general, the system will try loading the profile by name in this order:

  • .bash_profile

  • .bash_login

  • .profile

______________________

Shawn Powers is a Linux Journal Associate Editor. You might find him on IRC, Twitter, or training IT pros at CBT Nuggets.