0

Supposedly the bin/sh shell that is the default root shell is not strictly a "bash" shell, but is a POSIX compliant equivalent. According to this site, when you invoke the shell executable of a "POSIX compliant" shell, it runs any of the "profile files" listed here:

$HOME/.bash_profile
$HOME/.bash_login
$HOME/profile.ksh       ($HOME/.profile on UNIX systems)
$HOME/.profile

I've tried putting them all in /var/root and none of them execute when I run sudo su from my terminal.

Running sudo bash works, by the way. But why doesn't sudo su?

  • Just in case this is an X-Y problem - why have you enabled root. macOS has it disabled for good reasons – Mark Mar 26 at 19:58
  • Is root disabled? I just remember having to set a password... along the lines of sudo passwd... – James M. Lay Mar 27 at 1:01
  • Sorry better wording is "root login is disabled" – Mark Mar 28 at 16:16
1

Your information is incorrect. /bin/sh on macOS is actually a real bash shell. The manual you have found is for a Windows program that "mimics" a Linux environment on Windows. It doesn't have anything to do with macOS.

The profile files for bash are:

  • /etc/profile
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile

Note that ~ and $HOME are the same thing. They both mean that it refers to the user's home folder.

The problem you're seeing is because you're using the command sudo su. This means that you use sudo to start su in order to get an interactive non-login shell. This means that the profile files are not executed - instead only .bashrc is executed.

What you want is really a login shell, which would mean that the profile files are executed. You can do that by using the command sudo su - instead.

  • 1
    sh when invoked as a interactive or non-interactive login shell reads /etc/profile and ~/.profile. When invoked as an interactive shell, sh reads the startup file set in ENV. If ENV is not set then sh does not read any startup files. Paraphrased from the Bash manual under the section- INVOCATION. – fd0 Mar 26 at 20:23
  • @jksoegaard but .bashrc was the first thing I tried... that didn't even run. – James M. Lay Mar 27 at 1:08
  • Why not just use sudo -i? That runs the root profile script, just like sudo su -. – Gordon Davisson Mar 27 at 3:30

You must log in to answer this question.

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