-3

I am trying to prevent bash to ask for sudo permission to run the port command (OSX packet manager) adding it to visudo. I basically tried to follow this SO thread but I still get an error

manfredo@cave05:~$ port selfupdate
--->  Updating MacPorts base sources using rsync
Error: Error synchronizing MacPorts sources: command execution failed
Please run `port -v selfupdate' for details.
Error: /opt/local/bin/port: port selfupdate failed: Error synchronizing MacPorts sources: command execution failed

The same selfupdate command runs smoothly if I sudo it.

My visudo has the following lines

# User privilege specification
  root     ALL=(ALL) ALL
  %admin   ALL=(ALL) ALL
  %admin ALL=(ALL) NOPASSWD: /opt/local/bin/port

and I also tried susbtituitng the last line with

<user_name> ALL=(ALL) NOPASSWD: /opt/local/bin/port

What am I missing? Also can someone explain the syntax of the last line of the visudo file.

Community
  • 1
  • 1
Manfredo
  • 1,760
  • 4
  • 25
  • 53
  • You simply don't execute the `port` command with `sudo`. What do you expect? It looks like you don't understand what `sudo` is doing. – hek2mgl Oct 24 '16 at 13:03
  • Well that's exactly what I want to do. To run a command that needs `sudo` without having to type `sudo` in front of it. It seems quite clear to me. Also calm down, your tone is irritating. – Manfredo Oct 24 '16 at 13:06
  • Two downvotes and as usual no comment. Reading on the -1 arrow I read "not show any research effort, unclear or not useful" I don't know for the latter but the I researched my question and tried to write it as clear as possible. – Manfredo Oct 24 '16 at 13:08
  • I'm sorry, I was just saying that you don't understand `sudo`, or how to call programs from the commandline - which is obviously the case. And I've asked you what you expect, because that is hard to get for somebody who reads your question. If you want to run `port` always with `sudo` without typing the `sudo` command explicitly, you can create a bash alias, like this `alias port='sudo port'` – hek2mgl Oct 24 '16 at 13:14
  • No it is not hard to get, and no it is not obviously the case. I often use `sudo` and often run programs from the command line. Does this mean I understand perfectly all about `sudo` and command line, surely not. Does it mean I don't understand them? Neither. But is your tone still irritating? Surely yes. If I alias `port` with `sudo port` I will still be asked for my password when I run the `port` command. What I want is to be able to run a certain command which usually requires `sudo` without the `sudo` in front of the command. – Manfredo Oct 24 '16 at 13:23
  • You need both a) change the sudo settings to enable running `sudo port` without need to enter the password (you did already) b) create the alias to be able to run `sudo port` without need to type `sudo`. That's it. – hek2mgl Oct 24 '16 at 14:44
  • I see, I thought `NOPASSWD` meant also no need to `sudo` it, hence the confusion about you first comment. This solves my problem so if you want to post it as an answer I can accept it, thanks. – Manfredo Oct 24 '16 at 14:53

1 Answers1

1

You actually want to achieve two things:

1. Making it possible to run sudo port without the need to enter a password.

You already solved that by adding the following line to /etc/sudoers:

%admin ALL=(ALL) NOPASSWD: /opt/local/bin/port

2. Making it possible to run sudo port without the need to explicitely type sudo

This can be done by an shell alias. Since the question is tagged bash I assume your shell is bash. In that case modify your .bashrc and add the following line:

alias port='sudo port'

Start a new shell and it should work.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266