0
root@puppetclient-ubuntu:/home/azureuser#cat /etc/.bash_aliases
  alias extend_shutdown_15='bash extend_shutdown.sh 15 ; bash /bin/max_timetrack.sh'
  alias extend_shutdown_30='bash extend_shutdown.sh 30 ; bash /bin/max_timetrack.sh'
  alias extend_shutdown_60='bash extend_shutdown.sh 60 ; bash /bin/max_timetrack.sh'

#try to automate and execute the "source /etc/.bash_aliases" through shell script but it is not working means changes are not effecting.

root@puppetclient-ubuntu:/home/azureuser# cat alias.sh
#!/bin/bash
source  ~/.bash_aliases

when I do source /etc/.bash_aliases the alias custom commands are available only for root user and it is not available for all others expect "root" if I do execute one time it should always available for the all users. Please help me to fix.Thanks.

3 Answers3

1

The standard approach in Ubuntu is that aliases are defined and fully controlled by the user, not by the administrator. Yes, some default aliases are defined upon creation of the account, but these definitions are added to the private .bashrc file and thus can be deleted/changed if the user prefers so.

You can customize the default aliases that come with a new account by editing /etc/skel/.bashrc. That file is the default .bashrc file for new accounts.

Control alias definitions as administrator

If, instead, you wish to take control, as administrator, of the aliases of current users, you can define these in a script that you add in /etc/profile.d. All of the scripts present there are executed by /etc/profile, the systemwide profile script executed before the user's private ~/.profile script for logon shells. Prefer this over directly editing /etc/profile, because the latter file is controlled by your package manager and may be overwritten during a future update.

Be aware that some users might not like you if you take such control. Also know that users can negate the aliases you impose to them with unalias commands in their private .bashrc file.

vanadium
  • 88,010
0

First, aliases are temporary and valid only for the currently running shell. When you run the alias.sh file, it executes in its own shell, not in the shell it was run from. So the script sources ~/.bash_aliases (why this file and not /etc/.bash_aliases? I thought you want the latter?), but once the script ends, the shell also exits and whatever aliases could have been defined in the ~/.bash_aliases file, they are gone.

That's the difference between running a shell script and sourcing it. If you run a shell script, it runs in it's own shell, so any changes to environment, aliases and the similar done in the script do not affect the parent shell (the shell the script was started from). But when you source the same script, it executes in the current shell, so all changes it makes stay after the script finishes (of course they stay as long as the shell lives - if you exit the shell, all changes are gone again).

Taking this into account, if you want aliases from the file /etc/.bash_aliases to be available to any user in any shell they run, the file must be sourced from /etc/bash.bashrc file. This file is executed whenever any shell (from any user) starts and is commonly used to set the initial environment.

So if you add the following line at the end of /etc/bash.bashrc (of course you must do it as root, to be able to edit the file):

source /etc/.bash_aliases

you should get what you want.

The alias.sh file isn't needed at all.

raj
  • 10,353
  • azureuser@puppetclient-ubuntu:~$ cat /etc/bash.bashrc source /etc/.bash_aliases automatically changes are not effecting in system but when I executed manually changes are effected "source /etc/bash.bashrc " but I don't want execute manually. Please help me.. – hariraj Sep 15 '21 at 08:35
  • @hariraj /etc/bash.bashrc should be executed automatically on shell startup, I don't know why it isn't. Probably your system is configured to execute some other file instead. You must find that file and insert source /etc/.bash_aliases into it. If you have a nonstandard system configuration, nobody can know which file is actually executing but you. – raj Sep 15 '21 at 15:52
0

I think you just need to put you alias.sh file in /etc/profile.d

in my case /etc/profile.d/99-alias.sh it contains:

#Added by JP
alias du='du -h'
alias dfh='df -h | grep -v snap'

works for every login.

jpbrain
  • 483
  • I have added same as you suggested but still not working "cat /etc/profile.d/alias.sh alias extend_shutdown_15='bash extend_shutdown.sh 15;bash /bin/max_timetrack.sh' alias extend_shutdown_30='bash extend_shutdown.sh 30;bash /bin/max_timetrack.sh' alias extend_shutdown_60='bash extend_shutdown.sh 60;bash /bin/max_timetrack.sh' – hariraj Sep 15 '21 at 08:24
  • automatically changes are not effected but when I execute manually like "bash /etc/profile.d/alias.sh" changes are effected but I don't want to execute manually ... – hariraj Sep 15 '21 at 08:41
  • Please tell me what are you trying to achieve? Also, what does "alias" command shows after you put the file in /etc/profile.d – jpbrain Sep 15 '21 at 14:27
  • I have added the "alias.sh" file into "/etc/profile.d/" , so I want aliases to be created for all the users in the linux system automatically. Now when I do the "alias" command in root user- there are no aliases created which I mentioned in alias.sh file. – hariraj Sep 16 '21 at 05:20
  • Hello. how do you login to root user? what command do you use to test root user? – jpbrain Sep 16 '21 at 14:08