What's the preferred way to set environment variables for all users (on Ubuntu) such that the new variables are read whenever a new shell/terminal is started? Presently, we're using a file in /etc/profile.d/ but

  1. Changes aren't picked up when a user opens a new shell, and
  2. Only programs started through a login shell (e.g., we have to set terminals to run /bin/bash -l instead of /bin/bash) actually read this file.

I guess one could demand a source line in all users' .bashrcs, but is there a way to keep everything in /etc?

  • The /etc/profile should be read by all login shells. If it is not setup to user /etc/profile.d you won't get anywhere. There may be some requirements for file extension or permissions you need to tweak to get it to work. Getting /etc/profile.d working is a good idea because it will make it easier to install future boxes with ansible or scripts. – chicks Oct 20 '15 at 20:25

I no longer use debian all that much, but I used to set those in /etc/profile, and according to this it still is the same. As ubuntu is a debian derivative it should do the same.

  • According to the wiki page you linked, a shell in a terminal will read only the user's ~/.bashrc. If correct, that answers my question in the negative. – user317715 Oct 20 '15 at 17:53
  • have you tried? It did work in debian when I had to do something like that – natxo asenjo Oct 20 '15 at 18:21
  • To be clear, I'm looking for a solution which works upon starting a new terminal without having to log in again. – user317715 Oct 20 '15 at 18:22
  • I understand your question, do you understand mine? – natxo asenjo Oct 20 '15 at 18:27
  • I have just tested in an oldish debian (squeeze) and it works perfectly when starting screen sessions, starting another bash process inside my shell, the variable is available to all new shells. – natxo asenjo Oct 20 '15 at 18:32

You should add these definitions to the .bashrc file in the home folder of the user that owns that shell session

You can edit the template used to create new users. Put here the .bashrc file customized as per your needs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy