5

I have read this answer but don't know how to add the following line into my sudoers file.

matthew ALL=(ALL) NOPASSWD: /usr/sbin/service fancontrol start

I ran "sudo visudo", and a "/etc/sudoers.tmp" window popped up. Is "/etc/sudoers.tmp" the correct file into which the line should be added? If so, under which line should I add the lines? How can I save it? I cannot find a "Save" option there.

I aim to run "sudo service fancontrol start" without a password.

   GNU nano 2.9.3                   /etc/sudoers.tmp                             
#
# This file MUST be edited with the 'visudo' command as root.
2
  • Is this basically a question about how to use the nano editor? You can change editor by setting VISUAL and/or EDITOR to your preferred editor.
    – Kusalananda
    Commented Feb 10, 2019 at 12:04
  • This is a question about how to add a line into my sudoers file, and Haxiel has answered my question. I even did not know "^G" denotes "Ctrl + G". I am a beginner in Linux. Commented Feb 10, 2019 at 12:13

5 Answers 5

14

visudo is a command provided for editing the sudoers file in a safe way. To quote its manual page:

visudo edits the sudoers file in a safe fashion, analogous to vipw(8). visudo locks the sudoers file against multiple simultaneous edits, provides basic sanity checks, and checks for parse errors.

The /etc/sudoers.tmp file is lock file used by visudo. Your changes are written to this temporary file so that visudo can carry out its checks. If everything checks out okay, the main /etc/sudoers file will be modified accordingly.

So when you run sudo visudo, a command line editor pops up so that you can edit the file. In your case, this editor appears to be GNU nano. In nano, you can navigate to the bottom of the file using arrow keys (or the Page Down key), and then paste the lines you want to include. Once your changes are done, you can exit the editor with Ctrl + X and choose the 'Y' option to save the file (you'll be asked to confirm the filename - just hit Enter).

Your sudoers file should now be updated. You can use a pager like less to read the file and confirm that for yourself (the command to do that is sudo less /etc/sudoers).

4
  • Many thanks to Haxiel. Now I can start Fancontrol without a password. I don't understand what you mean by "use a pager like less to read the file"? What is the command to bring up a pager like "less"? Commented Feb 10, 2019 at 12:04
  • @MatthewWai A "pager" is a program that shows you a text file, page by page, in a terminal. less is a pager, as is more. He means you can read the file by using less /etc/sudoers (as root).
    – Kusalananda
    Commented Feb 10, 2019 at 12:27
  • Thanks! After pressing "Enter" as a response to "File Name to Write: /etc/sudoers.tmp", if "visudo" finds something wrong, what will happen? Will an error message be given? Commented Feb 10, 2019 at 13:22
  • @MatthewWai That's correct. In the manual page I have linked to, there is a section named 'DIAGNOSTICS' that shows the error messages that visudo will display, based on the problem encountered.
    – Haxiel
    Commented Feb 10, 2019 at 13:26
2

You can easily add any line to sudoers by using:

echo 'whatever text you want to add' | sudo EDITOR='tee -a' visudo

In your case it would be:

echo 'matthew ALL=(ALL) NOPASSWD: /usr/sbin/service fancontrol start' | sudo EDITOR='tee -a' visudo
1
  • But make sure you have a backup or alternate mechanism in case you mess up sudoers.
    – U. Windl
    Commented Feb 8, 2022 at 14:08
1

To use a different editor

sudo EDITOR=«my_choice_of_editor» visudo

1
  • I would pass EDITOR to sudo as an environment variable, not hope that it's allowed in the existing rules.
    – Jeff Schaller
    Commented Feb 10, 2019 at 16:13
0

You can either use vi or nano to edit /etc/sudoers file and you can also use sudo visudo to edit /etc/sudoers file.

After that, you can add your line with proper format.

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
YOUR_ENTRY ALL=(ALL)    ALL     ## enter here
2
  • 4
    sudo visudo is recommended.
    – Prvt_Yadav
    Commented Feb 10, 2019 at 8:39
  • It is the first time I have edited my sudoers file, and "sudo visudo" has done the job. I know nothing about "VISUAL and/or EDITOR", which Kusalananda mentioned above. Commented Feb 10, 2019 at 12:20
0

Use the command visudo -f /etc/sudoers add the content and save it

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .