How can I preset aliases for all users?

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

4 Answers

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

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. and Understanding .bashrc and .bash_profile for the distinction between shells.

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:

3

/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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like