5
sudo sh -c 'echo "$(logname) ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/$(logname)' & sudo chmod 440 /etc/sudoers.d/$(logname)

I use the above one-liner to allow the current user to execute sudo without a password, on remotely connected hosts.

But what exactly is $(logname)?

On a local machine, echo $(logname) returns nothing, while echo $LOGNAME does, so it's not the environmental variable, although I have never seen the value to be any different on remote machines.

Also, are there any other similar $(foo) variables (or whatever they are called)?

paradroid
  • 1,203
  • 2
    It appears that echo $(logname) is in fact different to echo $LOGNAME on remote hosts when su'd into another account. – paradroid Aug 02 '19 at 14:10
  • This look strange to me, you need sudo right (at least with password prompt) to execute sudo in first place. – Archemar Aug 02 '19 at 14:51
  • Dupe https://unix.stackexchange.com/questions/268378/difference-between-logname-and-logname . If command logname doesn't work on your 'local machine' it isn't POSIX-compliant. – dave_thompson_085 Aug 03 '19 at 02:15

1 Answers1

10

logname(1) is a command that will return the login name of the current user.
$( ... ) is the syntax for command substitution which is saying "substitute the output of the command here"

So if your user is foo you are executing:

sudo sh -c 'echo "foo ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/foo' & sudo chmod 440 /etc/sudoers.d/foo
jesse_b
  • 37,005