Set aliases globally for all users

24.4k Views Asked by At

I have certain paths, which are too long to type, so I need to wrap all those in one script as alias and source that script to my existing package code.

These should set alias permanently over that server or over the package, so every user can use the alias instead typing the whole path.

alias bc='bc -l'
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i' 
alias grep='grep --color'
alias update='yum update'
alias update='yum -y update'

I have tried this but it isn't working.

2

There are 2 best solutions below

1
On

Old thread, but I figured out what to do using the great post from Shivams. The following is step by step of what I did on Raspberry Pi:

sudo nano /etc/bash.bashrc

Add the aliases to the bottom, you can keep comments to be useful, such as:

alias ll="ls -al"    #Adds alias for ll to show all files and folders

alias lll="ls -alh"  #Adds alias for lll to show all files and folders with size in human readable format

alias cls="clear"    #Adds alias for cls to clear screen

#Specifically for Raspberry Pi, I also have:

alias temp="vcgencmd measure_temp"    #Get the system temperature

watch_temp() { tMax=0.1; tMin=999.1; while true; do t="$(vcgencmd measure_temp)"; t="${t:5: -2}"; if [[ $t  > $tMax ]]; then tMax=$t; fi; if [[ $tMin > $t ]]; then tMin=$t; fi; echo "$(hostname) - $(date +'%Y-%m-%d (%a) %T') - $t (Max $tMax Min $tMin)"; sleep 5; done; unset tMax; unset tMin; }

alias watchtemp="watch_temp"          #Get reading of temp every 5s with watch_temp function, includes max/min

Ctrl+s to save, then then Ctrl+x to exit

They won't work for your current session, so log off the terminal and back in.

1
On

This is not the way to do this. Instead you need to set alias in your .bashrc file.

If you want these alias for one tty only then put them in a bash script and source that script on that tty. You only need to change shebang header in your script from

#!/usr/bin/perl to #!/usr/bin/bash

Instead if you want these changes in alias to be permanent then you need to put these entries in your .bashrc file and they will be permanent.

If you want to make those changes for all users you can put that in /etc/bashrc or /etc/bash.bashrc whichever is present on your system and then make /etc/profile to source it.

For example my machine is running ubuntu and it's /etc/profile entry is this way

if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
fi

So if i put alias in /etc/bash.bashrc then they will be available to all users.