What Is the Difference Between /Etc/Rc. Local and ~/. Bashrc?
This Is a Linux Related Problem. I Have Searched Around but Did Not Get a Good Explanation. It Seems to Me That Both File Configure the Setup When I Log In...
This is a linux related problem. I have searched around but did not get a good explanation.
It seems to me that both file configure the setup when I log in, but is there any difference? I notice that there seems to be "some rule" in deciding what should go into two different files. For example, if I need to add a specific search path to $PATH, I should do it in ~/.bashrc. But if I decide to change some system setting, like
/sys/class/backlight
or
/sys/devices/cpu/cpu#/online
then I have to do this in /etc/rc.local, otherwise it will not work.
Is it because these configurations can not differ between users?
Thanks.
3 Answers
The difference is in when they are run and who they're running as when run i.e. rc.local is run on a change of run level and it runs as root. bashrc is bash specific and run on a non login shell as a particular user.
You can find a good explanation of rc.local here
The script /etc/rc.local is for use by the system administrator. It is traditionally executed after all the normal system services are started, at the end of the process of switching to a multiuser runlevel. You might use it to start a custom service, for example a server that's installed in /usr/local. Most installations don't need /etc/rc.local, it's provided for the minority of cases where it's needed.
and you can find what you need about bashrc
man bash
When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
There's more info on bashrc in this question...
This question was asked by me a month ago, though later I realized that stack overflow is not the best site for this Linux question. Thanks for people who answered this question earlier, but I would like to add some more explanation here.
Basically there are (at least) three stages where a user may change system environment in Linux:
- when the system boots; This stage is most appropriate if we fancy permanent system setting, and should be made via
/etc/.... For example, in my original question, the backlight, as well as on-line/off-line management of some CPUs can be set in this way, and/etc/rc.localis the right shell script I should edit. By "permanent", it means that this update will affect all users using the system. - when a user logs in; This stage is most appropriate if a user only wants to change his personal Linux environment. Therefore, files under
~/(orHOME) should be the right place to look for. For example,~/.profile(historically referred to as./bash_profileor~/bash_login) is a shell script run at login time.~/pam_environmentis not a shell script, but useful for setting environmental variables (see Ubunte-official-wiki-environmental_variables for more information). - when a user starts a bash shell; This stage is more restricted, as it only has effects inside a bash shell (as well as its child processes), hence does not affect GUI environment. So if a user is doing most of his job from a shell, then this is an appropriate stage to go for. The shell script related to this stage is
~/.bashrc. For example, environmental variablesPATHcan be changed here.
Hopefully this summary is more intuitive than technical.
.bashrc runs for each bash session started (i.e. every time you open a shell). It sounds as though you're talking of .bashrc as if it were .bash_profile which is run once per login.
Depending on what kind of setup you're running the rc.local is a legacy construct but, traditionally it was run on the last run level during start up. You can see this link for other related posts talking about rc.local.
If you're on a system running systemd this is usually included by default in the systemd package systemd-backlight.service.