sudo with user id
I was trying to use the Nextcloud utility script occ, but it refused to run as I invoked it:
$ ./occ
Console has to be executed with the user that owns the file config/config.php
Current user id: 1000
Owner id of config.php: 33
Try adding 'sudo -u #33' to the beginning of the command (without the single quotes)
I tried to just do as they said, so I changed my call to use the user id that owns config.php:
sudo -u #33 ./occ
But I got a different error this time:
sudo: option requires an argument -- 'u'
usage: sudo -h | -K | -k | -V
usage: sudo -v [-ABkNnS] [-g group] [-h host] [-p prompt] [-u user]
usage: sudo -l [-ABkNnS] [-g group] [-h host] [-p prompt] [-U user] [-u user]
[command [arg ...]]
usage: sudo [-ABbEHkNnPS] [-r role] [-t type] [-C num] [-D directory] [-g
group] [-h host] [-p prompt] [-R directory] [-T timeout] [-u user]
[VAR=value] [-i | -s] [command [arg ...]]
usage: sudo -e [-ABkNnS] [-r role] [-t type] [-C num] [-D directory] [-g group]
[-h host] [-p prompt] [-R directory] [-T timeout] [-u user] file ...
I literally did as it indicated and it didn't work. Hmm...
I tried without the #. It also didn't work:
$ sudo -u 33 ./occ
sudo: unknown user 33
I did some random internet searches that did not produce any fruitful result.
So I did what I should have done instead: I read the manual.
$ man sudo
Then in the section for the -u option, I saw that I needed to use the hash character to indicate I want to use a user ID (which explains why it interpreted 33 as a user name when used without the #), and also I noticed this extremely enlightening sentence:
When running commands as a UID, many shells require that the
‘#’ be escaped with a backslash (‘\’)
Could it just be that? I tried adding the backslash:
$ sudo -u \#33 ./occ
Surprise, surprise... It worked!
Thus, learning of the day: if you need to execute using another user id, remember to escape the #. Otherwise, it acts as the opening of a comment, and the rest of the line is ignored! Which is why, of course, it was complaining about not having the argument for -u.
It of course makes a lot of sense, once you stop and think about that. Since I was more focused on fixing the issue than on how bash was parsing the command, I didn't think more of the #, although I did have a nagging feeling in the background of my thoughts. But of course it was not going to make a parsing exception just because I am using sudo!