简体   繁体   中英

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, 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.

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:

  1. 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.local is the right shell script I should edit. By "permanent", it means that this update will affect all users using the system.
  2. when a user logs in ; This stage is most appropriate if a user only wants to change his personal Linux environment. Therefore, files under ~/ (or HOME ) should be the right place to look for. For example, ~/.profile (historically referred to as ./bash_profile or ~/bash_login ) is a shell script run at login time. ~/pam_environment is not a shell script, but useful for setting environmental variables (see Ubunte-official-wiki-environmental_variables for more information).
  3. 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 variables PATH can be changed here.

Hopefully this summary is more intuitive than technical.

The difference is in when they are run and who they're running as when run ie 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...

https://superuser.com/questions/49289/what-is-the-bashrc-file

.bashrc runs for each bash session started (ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM