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?

- 29,831
2 Answers
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

- 29,831
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).

- 385
-
-
1Don't use your favourite editor to edit
/etc/sudoers*
files, instead that usesudo visudo
orsudo visudo -f /etc/sudoers.d/<file-name>
. Otherwise any simple typo can lock your system. Reference: https://askubuntu.com/a/159009/566421 – pa4080 Feb 27 '18 at 16:27 -
Thanks, but it still asks for password after reboot. How can i make this to a specific user and not all of the users in the system? I tried this: username TEST=(TEST:TEST) EDITFSTAB – Feb 27 '18 at 21:43
-
Add the nopasswd option : "username ALL=NOPASSWD: EDITFSTAB". And as pa4080 mentionned, use visuel – DrGorilla.eth Feb 28 '18 at 07:08
-
-
-
On what line did you add it? Sudoers works in a sequential way, any line with that user/usergroup without the nopaswd option coming after will overwrite the option. – DrGorilla.eth Feb 28 '18 at 07:30
man sudoers
- it will show you how to allowsudo
access only to listed commands. BUT all the editors I use have the capability for a "shell escape" (e.g.vim
and:!
) that would give access to aroot
shell. In the security biz, that's Game Over. Also, readman sudoers
aboutsudoedit
and the-e
option. – waltinator Feb 27 '18 at 16:07