77

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.

Brian Sizemore
  • 1,363
  • 3
  • 11
  • 19

4 Answers4

100

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.
chesedo
  • 1,749
  • 1
  • 14
  • 25
0x2b3bfa0
  • 9,110
  • 7
  • 38
  • 55
34

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.

tinlyx
  • 3,328
17

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

jtpereyda
  • 2,115
  • 3
  • 20
  • 21
2

/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.

xenoid
  • 5,759
CentoS
  • 21
  • 1