1

How can I set up sudo so that a particular common user can edit /etc/fstab? I've thought to edit /etc/sudoers.d file to do this, but how do we edit /etc/fstab in this file?

pa4080
  • 30,621

2 Answers2

1

Create simple script, called editfstab and located in /usr/local/bin (to be accessible as shell command), and make it executable:

echo -e '#!/bin/sh\nnano /etc/fstab' | sudo tee /usr/local/bin/editfstab && sudo chmod +x /usr/local/bin/editfstab

Run the command sudo visudo -f /etc/sudoers.d/editfstab and add the following rule as content of the newly created file:

ALL ALL=NOPASSWD: /usr/local/bin/editfstab

At this point, each system user will be able to edit /etc/fstab, without password, by the command:

sudo editfstab

You can extend the functionality of /usr/local/bin/editfstab by adding a feature to make backup copy before edit:

#!/bin/sh
cp /etc/fstab /etc/fstab.bak
nano /etc/fstab
pa4080
  • 30,621
1

Adding a line in sudoers.d with you favorite editing software in a cmd alias should do the trick :

Cmnd_Alias EDITFSTAB = /etc/bin/vim /etc/fstab
username    ALL = (user) EDITFSTAB

Be careful, there is a huge risk of escape privilege, maybe you should write a basic shell script to restrict/control fstab modifications WITHOUT using editor (ie "for that modification, press 1" and echo-ing right in fstab).

DrGorilla.eth
  • 385
  • 1
  • 13