How to apply settings (.bash_profile, .bashrc, .inputrc) to all users?
I've applied custom bash settings in files like .bashrc, .bash_profile, and .inputrc. However, these only apply to the current user because they're placed in the current user's home directory. I'd like apply my custom settings to all users so that I don't have to modify those files for each user. Is there any simple way to do this?
|
Do you have a directory, /etc/profile.d? If so, there's where you can add profile stuff that's system-wide (look at the individual shell programs in there).
Don't have /etc/profile.d? Well, you can do the old-fashioned thing and edit /etc/profile -- it runs for every log in irrespective of what shell the user is using. Don't want to edit /etc/profile? OK, make up a shell program (and stick it /etc and make it executable) that sets things the way want them and execute it from the tail end of /etc/profile. You can also fiddle around in the /etc/rcx.d directories, in your equivalent of rc.local (somewhere in /etc, or in the /etc/init.d directories) and do things there. The basic thing to know is that /etc/profile is executed by every log in, always, and you can modify that file or add function to it in a separate file (invoked from /etc/profile). Hope this helps some. |
You can try to put a script into /etc/profile.d directory. All the *.sh files inside it are sourced from /etc/profile. Here is the relevant code on OpenSuSE:
Code:
# |
Read section in the bash man page on INVOCATION. It will detail which start-up files your version of bash uses, and the order they are loaded. Modify one of those files.
In general, bashrc is for bash-specific stuff, profile is for things that could apply to all shells. The /etc directory is for system-wide configurations, and ~/ holds per-user things. |
When I look at the contents of /etc/profile and /etc/bashrc, both say that rather than making changes to those files I should add scripts to /etc/profile.d/. But I thought /etc/profile (analogous to .bash_profile) is run on initial login only and /etc/bashrc (analogous to .bashrc) is run everytime a new terminal is opened. So how can both files be suggesting that I put scripts is /etc/profile.d/... When exactly will the scripts in that directory get run? Should I put things in that directory from .bash_profile or .bashrc?
Also, I see there's an /etc/inputrc - if I don't want to create a .inputrc for each user, can I simply change that file and will it be picked up by all users? Finally, what's the point of .bashrc at all? Can't I just put everything in .bash_profile, whether it is setting environment variables, aliases, or functions? If it's set upon initial login, won't it for my whole session and all terminals opened in it? Maybe I'm just not seeing this correctly... |
The file /etc/profile is executed at log in irrespective of what shell program you're using; down at the bottom of /etc/profile there should be a section that looks something like this:
Code:
# Append any additional sh scripts found in /etc/profile.d/: In my own case, I do not use BASH in favor of KornShell, so I have a /etc/profile.d/ksh.sh file that sets all KornShell user environments; however, there is not a specific BASH file in /etc/profile.d as BASH-specific settings are handled in sections of /etc/profile (at least in Slackware). Again, all of that gets done at log in. A user can have a .profile that overrides or augments the settings in /etc/profile and /etc/profile.d at log in. A user can also have a .bash_profile or a .bashrc file to override or augment system-wide settings. I believe that .bashrc is executed every time you hit the enter key (could be wrong about that but that's what a .cshrc file in C-shell does and system administrators sometimes use that "feature" to make sure users don't fiddle too much with their environments). A .profile covers every shell a user might use at log in, a .bash_profile is useful for user-specific aliases and the like as is a .bashrc. Overall, if you have settings that affect every user, they really belong in /etc/profile.d so that you only have one point to maintain rather than trying to spew settings to every user account (think in terms of hundreds of users even though it may only be "you" on the box). If individual users need to have special PATH or other environment variables and other users do not, well, that's what a .profile is for in their home directories and you let them fiddle with whatever aliases they like in a .bash_profile or a .bashrc. It's really easy to overdo this stuff and you want to keep it as simple as possible with as few maintenance points as you can. Set system-wide variables in /etc/profile.d and everybody gets 'em at log in and you don't have to deal with individual user environment problems. Where you run into goofy stuff is with the type of terminal being used and how it is set. KDE has a terminal emulator and a Konsole (two separate utilities). You set the terminal emulator to "run command as a login shell" in the terminal emulator's preferences to get all the log in settings; Konsole just does that by default (I don't remember having to fiddle with Konsole, could be wrong). Anyway, try to keep it as centralized and simple as possible and life will be good. Hope this helps some. |
You say to set system-wide variables in /etc/profile.d/ - what about aliases and functions? Those are supposed to go in .bashrc which is executed on every new terminal as opposed to just logins, but if I put them in /etc/profile.d/ then they'll just get executed on logins...
What about setting iptables rules on startup? If I do them in /etc/profile.d/ then these rules will get reapplied on every login. But this is a system-wide thing that should be applied once upon system startup rather than per every user login. |
I would think you'd want to put individual user aliases and functions in ~/.bashrc rather than in /etc/profile.d/something.sh.
You would probably want to start up IPTABLES in /etc/rc.d/rc.local (or in whatever directory in /etc where you start daemons at system boot if not /etc/rc.d/rc.local; e.g., /etc/rcx.d). Things like IPTABLES should only start at system boot, not at user log in. Hope this helps some. |
I'm clearly getting something wrong here, because I thought /etc/profile.d/ scripts are run at user login, as you also mentioned in your responses. However, when I make a change to my script and open a new terminal window within my current login session, the changes are reflected. Meaning that my script got read! I was not expecting this, because I thought those scripts only get called on login (as for .bash_profile, but these scripts seem to be getting called at the same time as .bashrc which is for new non-login shells like opening a terminal). What's going on here then?
|
When you say, "open a terminal," what terminal do you mean: an xterm, konsole, terminal emulator or what? Most of those -- the exception being terminal emulator -- will open "as if you logged in." And, if you're using the terminal emulator from the system application button in the Kickoff Application Launcher (in KDE), and if the "Run application as a login shell" is selected in the preferences for terminal emulator, you'll get a log in terminal -- if that is not selected you'll get a $ prompt (no profile stuff).
Most of those "run as a login shell" by default. That's why you get all the environment settings in the newly-opened terminal. Keep in mind that all you're doing with "extras" that you place in /etc/profile.d or in individual user .profile or .bashrc or whatever are environment settings, colors, prompt format, window size, path variables, search paths and the like. You do system-wide in /etc/profile.d/whatever and you do user-specific in ~/.profile, ~/.bashrc and the like. You do not kick off applications in those, such as IPTABLES (except in rare cases); those should get launched at boot from /etc/rc.d, /etc/rcx.d or wherever your system does those things (in other words, never from one of the profile files). Hope this helps some. |
Hi,
I agree with tronayne. For my user(s) the '~#.bash_profile' sources '~#.bashrc'; Code:
sample .bash_profile; Code:
sample .bashrc; HTH! |
Quote:
|
Hi,
Quote:
HTH! |
All times are GMT -5. The time now is 06:41 PM. |