I wrote a shell script (this one) to init a new website project. It have to be executed by a user (it isn’t a mater if he got root privileges because this user is me).

As you see, the user have to be able to:

  1. edit /etc/hosts;
  2. Add new files to /etc/apache2/sites-available/;
  3. Create a new directory (with ln) in /var/www/;
  4. Execute a2ensite command;
  5. Execute service apache2 reload command;

All this actions need to be executed as root. But is it possible to give rights to do this to a regular user? And for this, have I to change owner of this files or the user’s group or both and how can I do this?


This is normally done using sudo. On modern systems you can add a file to /etc/sudoers.d adding new privileges.

# Rules for running am apache script.
User_Alias APACHE = yourUser
Cmnd_Alias APACHE = /usr/local/sbin/your_script

Given that you are the user, you may want to add your account to the group defined in /etc/sudoers for administrators. On Debian or Ubuntu systems this is admin. Other distributions use different values. This is the Ubuntu definition for the admin group.

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

sudo comes with detailed man pages. You may want to review the man pages for sudo, sudoers and visudo. If you have the man pages indexed, you can get a list of sudo related man pages with the command man -k sudo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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