Injecting sudo su with aliases

0

I'm wondering if there is a way to inject alias when you switch to root profile with sudo.

What I have in mind is this:

sshLogin@linuxVM /home/sshLoginUser/> sudo su -x "alias goHome='cd /home/sshLoginUser/'"
root@linuxVM /home/OtherDir/> goHome
root@linuxVM /home/sshLoginUser/>

I am using a VM box on cloud to setup and run some operations, and I want to eventually add more alias for repetitive tasks.

Ideally, I would like to sudo su and source on a file which contains my collection of aliases and functions.

Fadiel

Posted 2019-09-26T09:35:46.580

Reputation: 1

Answers

0

It is important to understand, that sudo does not do shell expansion; it runs a command. There are probably two solutions for what you would want to do, each with there own advantages/disadvantages:

1 alias hop='sudo id': let the sudo be part of the alias or

2 sudo -s (without command), which starts an interactive shell and reads /root/.bashrc.

Ljm Dullaart

Posted 2019-09-26T09:35:46.580

Reputation: 922

0

[tom@alarm ~]$ sudo -i
[root@alarm ~]# tail -n 1 .bashrc
alias goHome="cd /home/$SUDO_USER"
[root@alarm ~]# goHome
[root@alarm tom]# pwd
/home/tom
[root@alarm tom]#

If the "built-in" env vars are not sufficient for your cases, you can also make sudo preserve some custom ones. For example:

CERTAINFILE="/home/$USER/different_name" sudo -i --preserve-env=CERTAINFILE

Tom Yan

Posted 2019-09-26T09:35:46.580

Reputation: 4 744