I've been trying to grant www-data to use sudo service apache2 restart without a password but no matter what I try, nothing works. I have read a few hundred posts regarding this issue on different forums but nothing in them helps.

I know that the permissions are added in order, so the specific command should go at the end of the list. Here is my file:

# This file MUST be edited with the 'visudo' command as root.
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
# See the man page for details on how to write a sudoers file.
Defaults        env_reset
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL) ALL

# See sudoers(5) for more information on "#include" directives:

# includedir /etc/sudoers.d

%www-data ALL=(ALL) NOPASSWD: /usr/sbin/service apache2 restart

From what I gather, anyone in www-data should now be able to run sudo service apache2 restart and not require a password. It still asks for a password. I have tried to add:


And that works as expected. I can give them no password for all commands but not for one. Of course that would be really bad idea.

Any help would be appreciated. This is the latest version of Ubuntu


Turns out I had sudo aliased in my .zshrc. Removing the alias fixed the issue. Now it works as it should.

This link was useful: Sudo arguments not working

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.