38

I have Ubuntu 14.04.2. I want to make it so all users automatically have a specific set of aliases. I have my aliases set in my personal .bashrc, but I don't want to have to manually copy them into the other users. Ideally it should automatically set these for newly created users as well.

  • 1
    By "preset," do you mean you want all new users to be created with certain aliases, which they can then easily reconfigure? Or do you mean you want to create global aliases, such that all users will have them all the time (and be able to redefine/undefine them... but not see them when they look through their own configuration files, which they might find befuddling)? Or something else? Related: Undertanding .bashrc and .bash_profile (in spite of its odd title, I believe that question and this one are actually quite similar). – Eliah Kagan Apr 17 '15 at 23:48
50

You can create a script in /etc/profile.d/ to make aliases for all users:

  1. Create a file called 00-aliases.sh (or any other fancy name) in /etc/profile.d:

    gksu gedit /etc/profile.d/00-aliases.sh
    
  2. Put you aliases in this file. Example:

    alias foo='bar --baz'
    alias baz='foo --bar'
    
  3. Save the file

  4. Restart any open terminals to apply the changes.
  5. Enjoy!

Some notes:

  • /etc/profile is a global file that gets run before ~/.profile.
  • /etc/profile.d/ is a folder that contains scripts called by /etc/profile
  • When /etc/profile is called (when you start/login a shell), it searches for any files ending in .sh in /etc/profile.d/ and runs them with one of these commands:

    source /etc/profile.d/myfile.sh
    

    . /etc/profile.d/myfile.sh
    
  • I'm putting 00- before the file name to make it execute before the rest of the scripts.
  • You can also add your aliases in /etc/profile, but this isn't recommended.
  • 2
    Works perfectly! Can you give me a brief explanation of what the purpose of /etc/profile.d is? – Brian Sizemore Apr 16 '15 at 15:06
  • 1
    @BrianSizemore: I updated my answer with a few explainations. Do you have any query more? – Helio Apr 16 '15 at 17:47
  • 1
    This explains it very well. The clarification helps me to develop other uses as well. Thanks for all the help! – Brian Sizemore Apr 16 '15 at 18:19
  • 4
    The method in this answer should not be used. Aliases in .sh files in /etc/profile.d/ (or /etc/profile) will be defined only for login shells and they will not work in interactive non-login shells. Unlike environment variables, bash can't export aliases to child processes, not even child bash shells. This method may seem to work properly if it's only tested in login shells, such as the original shell obtained by logging on in a virtual console or via SSH, but it fails in their child shells and also fails in shells started by GUI terminal windows. – Eliah Kagan Apr 18 '15 at 0:56
  • 2
    @Helio My answer to Undertanding .bashrc and .bash_profile gives two other methods, depending on what the goal is, along with detailed explanation. I'm unsure if either this or that question can be considered a duplicate. I've commented on this question to get clarification from the OP about their needs. This question is more narrowly scoped than that one so I suppose it'd be OK for me to post a short answer here covering some of the material in my long one there. (For now, I'm hoping to get a reply by the OP to my comment.) – Eliah Kagan Apr 18 '15 at 7:40
6

As pointed out here, it's probably better to add global aliases in /etc/bash.bashrc:

alias foo='bar --baz'
alias baz='foo --bar'

, because scripts in /etc/profile.d can be ignored for certain (non-login) shells. It took me hours to figure out why /etc/profile.d didn't work.

See e.g. https://askubuntu.com/a/606882/ and Understanding .bashrc and .bash_profile for the distinction between shells.

5

An alias will only work while inside of a shell. If you want something as widely accessible as an executable, you can add a small shortcut script to /usr/bin, e.g.:

#!/bin/sh
ls -l "$@"

The "$@" passes all arguments through to the executable. The name of the script will be the name of the executable.

Source: https://unix.stackexchange.com/a/52509/15954

  • This answer seems unrelated. The accepted answer correctly answers the question while this seems to provide a workaround. – Brian Sizemore Sep 14 '17 at 21:11
  • 3
    @BrianSizemore I had your same question but stumbled upon this solution which was more appropriate in my case; hopefully it will help others in a similar situation. For a lot of people, the alias will be just fine. – jtpereyda Sep 14 '17 at 21:21
  • 1
    @BrianSizemore This answer clarifies a common misconception (which I also had) and is useful and directly related. – a20 Oct 17 '17 at 7:40
0

/etc/bashrc

  • System wide functions and aliases
  • Environment stuff goes in /etc/profile

It's NOT a good idea to change this file unless you know what you are doing. It's much better to create a custom.sh shell script in /etc/profile.d/ to make custom changes to your environment, as this will prevent the need for merging in future updates.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.