How can I preset aliases for all users?

99,308

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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

Share:
99,308

Related videos on Youtube

Brian Sizemore
Author by

Brian Sizemore

Computer Science and Computer Engineering Dual Major at West Virginia University. Member of CyberWVU. http://cyberwvu.lcsee.wvu.edu/

Updated on September 18, 2022

Comments

  • Brian Sizemore
    Brian Sizemore almost 2 years

    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.

    • Admin
      Admin about 9 years
      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).
  • Brian Sizemore
    Brian Sizemore about 9 years
    Works perfectly! Can you give me a brief explanation of what the purpose of /etc/profile.d is?
  • 0x2b3bfa0
    0x2b3bfa0 about 9 years
    @BrianSizemore: I updated my answer with a few explainations. Do you have any query more?
  • Brian Sizemore
    Brian Sizemore about 9 years
    This explains it very well. The clarification helps me to develop other uses as well. Thanks for all the help!
  • 0x2b3bfa0
    0x2b3bfa0 about 9 years
    You're welcome! English is not my mother tongue, if you find any typo or spell failure, please fix it.
  • Eliah Kagan
    Eliah Kagan about 9 years
    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.
  • 0x2b3bfa0
    0x2b3bfa0 about 9 years
    @EliahKagan: Did you know another method? If true, please make another answer. I don't know more than this.
  • Eliah Kagan
    Eliah Kagan about 9 years
    @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.)
  • 0x2b3bfa0
    0x2b3bfa0 about 9 years
    @EliahKagan: Your answer is very good and well explained (mine has nothing to do with it). You can mark the question as duplicate if the OP says that is a duplicate.
  • Brian Sizemore
    Brian Sizemore almost 7 years
    This answer seems unrelated. The accepted answer correctly answers the question while this seems to provide a workaround.
  • jtpereyda
    jtpereyda almost 7 years
    @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.
  • a20
    a20 over 6 years
    @BrianSizemore This answer clarifies a common misconception (which I also had) and is useful and directly related.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 4 years
    @EliahKagan FYI I referenced your comment above in this answer: Alias for all users