Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1791  / 1 Year ago, wed, may 17, 2023, 1:55:56

I'm used to use sudo -i and don't have a root password set, i.e. su - doesn't work. I'm looking for a way to share bash aliases between a user shell (e.g. the one opened when I start gnome-terminal started as the user who logged into the display manager) and the shell opened by sudo -i.



According to man sudo it's a login shell and afaik it should source ~/.profile, i.e. /root/.profile, but putting aliases in /root/.profile doesn't work. Only if I run bash in the shell opened by sudo -i the alias is available. According to /etc/passwd root's shell is bash and SHELL variable confirms that (in the shell started by sudo -i).



I'd prefer to not put the aliases in /etc/profile because that would expose them to any user and I only want to have them for the display manager user and root.


More From » bash

 Answers
6

The related Serverfault post suggests using /etc/bashrc, but I personally would recommend creating /etc/shell_aliases file ( apparently /etc/aliases by sendmail package is already reserved ) , and source it from your user's and root users .bashrc file. That way you can re-define aliases globally.



Alternative approach as shown in this AskUbuntu post is to use /etc/profile.d/00-aliases.sh.



$ sudo bash -c 'echo alias print_hello="echo 'HELLO ASKUBUNTU'" > /etc/profile.d/00-aliases.sh  ' 
$ sudo -i
$ print_hello
HELLO ASKUBUNTU


Side note: for GUI shells , you will have to log out and log in, because .profile is sourced each time you log in, and once you sing into the system via graphical login screen, it will be sourced , but only once. All terminal emulators opened are considered interactive shells after that.


[#14147] Wednesday, May 17, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nalystcul

Total Points: 390
Total Questions: 106
Total Answers: 115

Location: Tokelau
Member since Sun, May 7, 2023
1 Year ago
;