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?

2
  • 1
    Probable duplicate of: unix.stackexchange.com/questions/30925/…
    – jesse_b
    Commented Aug 5, 2020 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
    Commented Aug 5, 2020 at 16:40

1 Answer 1

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 ?

1
  • 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
    Commented Aug 6, 2020 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .