.bashrc systemwide

General support questions
wos
Posts: 5
Joined: 2015/03/17 14:18:27

.bashrc systemwide

Postby wos » 2015/03/23 07:39:18

Hi,

not a *nix geek, coming from debian and had some modifications which I'd put in debian in bash.bashrc there ... but this file seems not to exist in CentOS! I'd read some forum posts, but none shifts me in the right direction:

Where do i have to put my modification for bash (some aliases / modification for prompt). I found a file /etc/bashrc, where many lines with if/fi inside. Don't know if it's the right place, and if ... where to put my modifications in? Some forum posts pointing in direction /etc/profile.d but i don't understand what and how to modify there?!

Some saying put it there, its better then other location, others say, better put other place ... and there X possibilities ... so I'm ending up a bit confused and don't know what to do. Please can someone tell me which file is the best (i dont need 10 different possibilities) to achieve what i want.

Thx

User avatar
TrevorH
Forum Moderator
Posts: 21383
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: .bashrc systemwide

Postby TrevorH » 2015/03/23 08:49:33

Create a new file in /etc/profile.d called something.sh and put your content in there. They're executed in alphabetical order.
CentOS 5 died in March 2017 - migrate NOW!
Full time Geek, part time moderator. Use the FAQ Luke

wos
Posts: 5
Joined: 2015/03/17 14:18:27

Re: .bashrc systemwide

Postby wos » 2015/03/23 16:06:52

Thx for quick reply. Working wonderful!

koullislp
Posts: 4
Joined: 2016/03/22 11:41:37

Re: .bashrc systemwide

Postby koullislp » 2016/03/22 11:45:50

Captain, how on earth can we do that for non interactive shells? I am struggling here.

Need to export some variables and run a few scripts on every user login/bash shell opened.

Any way to do systemwide without the need to do that in ~/.bashrc for every single user?

Tried /etc/profile.d/ but only works for ssh.

User avatar
dmunk
Posts: 37
Joined: 2016/03/16 03:33:49

Re: .bashrc systemwide

Postby dmunk » 2016/03/23 18:22:51

Hello,

If your talking about a system wide bashrc on centos you'll need to look at :

Code: Select all

/etc/bashrc


That will be global. It will not stop someone from having a /home/SOME_USER/.bashrc for their account though. So, a user could set up their own bashrc file. Any way, "/etc/bashrc" would be a global though. To see how it works just do the following :

Code: Select all

#:printf "alias df='df -Thk'\n" >> /etc/bashrc
( exit shell and re-open. You can also just use source /etc/bashrc; however, you want to make sure your changes will take effect on a login.
#:df


Hope that helps.

koullislp
Posts: 4
Joined: 2016/03/22 11:41:37

Re: .bashrc systemwide

Postby koullislp » 2016/04/20 09:00:40

Thanks, but it wouldn't work for a terminal launched from MATE.

I had to enable it through the terminal settings to launch this as an interactive shell to get this to work.

Otherwise it wasn't going through bashrc at all.

pjsr2
Posts: 233
Joined: 2014/03/27 20:11:07

Re: .bashrc systemwide

Postby pjsr2 » 2016/04/20 15:44:20

A bash login shell first reads /etc/profile (if it exists) and then the first one it finds of ~/.bash_profile , ~/.bash_login or ~/.profile .
For a bash interactive and non-login shell, the shell reads ~/.bashrc .
A non-interactive shell reads the file ${BASH_ENV} , if that environment variable exists and the file it points to can be read.

When a user's home directory is created, the files /etc/skel/{.bash_profile,.bashrc,.bash_logout} are copied to the new home directory.
By default, the skeleton file for .bash_profile reads ~/.bashrc and the skeleton file for .bashrc reads /etc/bashrc .
So when the user has not changed the ~/.bash_profile or ~/.bashrc from what was copied from /etc/skel, both login and non-login interactive shells will read /etc/bashrc.

/etc/profile and /etc/bashrc execute the commands found in the files /etc/profile.d/*.sh (/etc/bashrc does this only for non-login shells, so it is not needlessly done twice).
When you want to add your own system wide customization for bash, create a new file in /etc/profile.d with .sh file name suffix.

@koullislp: when you launch a terminal you always launch an interactive shell in it. A terminal launched from a desktop session (like MATE) is a interactive non-login shell. When this shell has not read your customization in /etc/profile.d/*.sh , it is likely that the user does not have a ~/.bashrc or that ~/bashrc is modified and no longer reads /etc/bashrc .

Note 1: bash for security reasons skips reading ~/.bashrc if it is not owned by the user or root, or when it has write permissions for group or other.

Note 2: (syntax) errors in ~/.bashrc or ~/.bashrc_profile or other files read on start of the shell may make that only part of these files is read. So check and double check these files for errors!