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.
  • 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 Feb 10 '19 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. – Matthew Wai Feb 10 '19 at 12:13

5 Answers5

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).

Haxiel
  • 8,361
  • 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"? – Matthew Wai Feb 10 '19 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 Feb 10 '19 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? – Matthew Wai Feb 10 '19 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 Feb 10 '19 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
Oscar
  • 121
1

To use a different editor

sudo EDITOR=«my_choice_of_editor» visudo

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
Prvt_Yadav
  • 5,882
0

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