I have heard it to be adviced that, with the general idea of least privilege, Unix-like users should create another account without sudo privilege, in addition to the built-in account with sudo privilege. In Mac, this translates to be: one should create a staff account for daily use, in addition to the admin account that comes with the computer, being reserved only when necessary.
For concreteness, say bruce
is a staff, which I use daily, and batman
is an admin, which I use only when necessary.
But how can I, while logging in batman
, perform sudo tasks, on behalf of bruce
, that is to say with bruce
's own ~
and $(whoami)
just as if I were logging in as bruce
with privilege?
To illustrate this, define /User/bruce/test.sh
:
#!/usr/bin/env bash
whoami
cd ~ && pwd
ls ~/Library/Preferences/Preferences/
(Yes, I have done chmod 744 test.sh
) I chose the last line for artificial illustration purpose that, normally it seems, ~/Library/Preferences/Preferences/
is not accessible to a staff (i.e., a non-admin).
What I intend to receive, is bruce
, /Users/bruce
, and successful ls
output.
The first attemp is that, indeed, if I open bruce
's terminal, but login
the bash of batman
(in order to get sudo privilege), and run
sudo /Users/bruce/test.sh
I get root
, /Users/batman
, and successful ls
output of ~/Library/Preferences/Preferences/
, showing that the home is batman's; this is no wonder.
Or, a possible duplicate (a) asserts that sudo -i -u bruce
enables me to execute commands as bruce
.
But no! If I (as above) open bruce
's terminal, login
the bash of batman
, and run
sudo -i -u bruce /Users/bruce/test.sh
I get bruce
, /Users/bruce
, and ls: : Permission denied
.
Yet another possible duplicate (b) states that sudo -H -u bruce
uses bruce's home.
Well, again when I open bruce
's terminal, login
the bash of batman
, and run
sudo -H -u bruce bash /Users/bruce/test.sh
I too get bruce
, /Users/bruce
, and ls: : Permission denied
.
The reader might think my request is unreasonable; but I just want to know whether it is possible to temporarily grant sudo privilege to a staff -- which may be convenient at times.
For example, if this can be done, when I am doing some short script I need no longer avoid $(whoami)
or ~
, making it more flexible.
Or, when I run a short script what site I googled suggests, nor do I have to replace $(whoami)
by bruce
and/or ~
by /Users/bruce
.
The last time, as far as I recall, I was irritated by the restriction of privilege, is probably when I ran a layperson-made script I googled, in order to install a LaTeX font.
However, at present I can recollect one instance only.
That is, after I, while logging in batman, brew
-ed something using homebrew
(for it requires sudo privilege), and logout
-ed back as bruce and brew
-ed something other.
Homebrew complained a large number of incorrect permissions of certain folders under /usr/local
, and urged me to run sudo chown -R $(whoami) blablabla
.
It seemed to me that brew
had set certain permission to batman
's own, and it drove me crazy to fix them one by one.
What do you do to get around the moment, for example, that brew
requires sudo, while using a staff account?
Or, after all, is the practice that daily-used account should contain least privilege, simply unrealistic?
sudo
I have only tosu
, but as you saidhomebrew
is a single-user designed. – Violapterin Nov 27 '16 at 13:17chown /Users/bruce/Library/Preferences/Preferences/
and it is alright! – Violapterin Nov 28 '16 at 6:06