1

I know that, by default, bash will source /etc/profile and ~/.bash_profile.

Now, suppose I also have machine-specific profile commands in /usr/local/etc/profile (involving stuff that's in /usr/local, hence I don't want to sully /etc with it).

My question: Whose responsibility is it to source /usr/local/etc/profile ? Should it be sourced within ~/.bash_profile, or perhaps the lines:

if [ -f /usr/local/etc/profile ]; then
    . /usr/local/etc/profile
fi

should actually be within /etc/profile, since they're valid for any installation of the distribution regardless of what's on the actual machine?

3
  • 1
    huh? ", and not also /etc/profile unless that's explicitly sourced from within the home directory's ~/.bash_profile." that seems wrong. /etc/profile is always sourced for login shells! Commented Dec 9, 2021 at 9:53
  • A couple of things come to mind: 1) are your users supposed to choose whether/how the content of /usr/local/etc/profile may affect them? 2) Existing users will need to edit their shell initialization files if you go for .bash_profile (and won't otherwise). Commented Dec 9, 2021 at 10:22
  • @MarcusMüller: You're right; see edit. Commented Dec 9, 2021 at 11:38

2 Answers 2

1

Conventionally, the right place to put machine-specific login-time initialization is /etc/profile. Many Unix variants don't bother much with /usr/local/etc if at all, anyway.

Note that for GUI logins, no profile file is directly read. However many distributions of X11 arrange to read source /etc/profile from the X session initialization. /usr/local/etc/profile is not a classic location and would not be sourced.

Many distributions have a directory /etc/profile.d, and put code in /etc/profile to source the files in /etc/profile.d. If your distribution does that, the best place for your machine-specific content would be a machine-specific file in /etc/profile.d.

On modern *BSD, /usr/local is for packaged software, not for local installation, and there /usr/local/etc is for files shipped with a package or their configuration. There the distinction between /etc and /usr/local/etc is whether the configuration is meant for the base system or for a specific program. /etc/profile is a system-wide configuration — you could make a case that if the login shell is a third-party package then its configuration should be in /usr/local/etc, but profile is not specific to a given shell and applies to /bin/sh which is part of the base system. (FreeBSD appears to be inconsistent though: the bash package reads /usr/local/etc/profile whereas the zsh package reads /etc/zprofile.)

If you (as the administrator) leave it to users to source another file from their .profile, that indicates that the customizations in that file are not meant for everyone, only for users who have an unusual need.

3
  • 1. I did not mention the .d directories to simplify the question. In fact I'm asking about /etc/profile.d files vs. /usr/local/etc/profile.d files with a mechanism in` /usr/local/etc/profile` which sources all of the latter. 2. Are you saying that if I've installed a package under /usr/local, I should place it's etc content, or at least its etc/profile.d files, under /etc rather than /usr/local/etc? How does that make sense? Commented Dec 9, 2021 at 11:41
  • @einpoklum *BSD uses /usr/local/etc for configuration files for programs in /usr/local/bin. On systems where /usr/local is for unpackaged software, the tradition is to use /etc. Commented Dec 9, 2021 at 12:17
  • @einpoklum on your run-of-the-mill Linux distros where /etc/profile.d is present, /etc is the admin's domain. Packages may put files there, but package management systems do not willy-nilly override modifications. It makes no sense to use something else when that's the case. Commented Dec 9, 2021 at 14:23
1

Whose responsibility is it to source /usr/local/etc/profile?

If this is supposed to be available by all users then source it from /etc/profile. Add those line as you already mentioned into /etc/profile

[ -f /usr/local/etc/profile ] && source /usr/local/etc/profile
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.