0

I have an alias I use to run sudo -s called ssu. I want it to also source my root session from the .profile in my home directory.

Other answers seem to say that this is the solution:

alias ssu='sudo -s && sudo -Hu root source /my_home/.profile'

But executing source as root user doesn't make it the profile for my shell. It runs the command after I log out of my root session.

Is there a way to seamlessly execute sudo -s and source (as root) in a row without having to run two commands?

Sam Bishop
  • 25
  • 1
  • 5
  • 1
    Probable duplicate of: https://unix.stackexchange.com/questions/30925/in-bash-when-to-alias-when-to-script-and-when-to-write-a-function – jesse_b Aug 05 '20 at 16:36
  • @jesse_b I did read that post, but I'm still unaware if it's possible to seamlessly execute the source as root using a function or a script – Sam Bishop Aug 05 '20 at 16:40

1 Answers1

1

The session running in sudo has all its variables scoped for the lifetime of the invocation. What you have in your alias is a command, sudo -s which will launch a shell as root. Once you exit that shell, it will run another command, sudo -Hu root source /my_home/.profile. Even if that did what you thought it did (it doesn’t), it would be for a second invocation of sudo and this a separate shell session.

The source command in bash is a builtin, not an executable, so sudo can’t launch it as root. You could run it in a bash command, though.

Perhaps you would be best served with something like

sudo -H bash —rcfile /my_home/.profile ?

jsbillings
  • 24,406
  • Thanks! That one line worked. For anyone else trying to do the same, it has to be done as a function, -rcfile doesn't work in the alias for some reason. Cheers! – Sam Bishop Aug 06 '20 at 15:48