Score:0

How to enable a non root user to substitute the file /etc/hosts by cp command, without the sudo command?

pg flag

On my lubuntu 22.04 I need to change the file /etc/hosts. To do that I would to execute the command:

> /usr/bin/cp /path/to/new_hosts /etc/hosts

by a non root user without the sudo command. To do this I have add the following line to the file /etc/sudoers:

<username> ALL = (root) NOPASSWD: /usr/bin/cp /path/to/new_hosts /etc/hosts

I have checked the correct syntax of /etc/sudoers by the command:

> sudo visudo -c /etc/sudoers

The output of the command is:

/etc/sudoers: parsed OK
/etc/sudoers.d/10-installer: parsed OK
/etc/sudoers.d/README: parsed OK
/etc/sudoers.d/kdesu-sudoers: parsed OK

The message /etc/sudoers: parsed OK assures me that the syntax of /etc/sudoers is correct.

When I execute the cp command by a terminal the result is Permission denied.

Where is the mistake?

guiverc avatar
cn flag
You need to use `sudo` to use the `sudoedit` or modify any files in `/etc` (such as `sudoers` though I'd recommend using `visudo` so changes are validated before saving! as don't forget any error in the file is treated as a EOF thus subsequent lines are ignored; meaning you risk losing `sudo` access on the system if you make a mistake)
frankfalse avatar
pg flag
@guiverc I have edited the question according to to your comment.
cn flag
You can't get `cp` to work without `sudo` by editing `sudo` configs.
Score:4
in flag

You will need to run your command with sudo:

sudo /usr/bin/cp /path/to/new_hosts /etc/hosts

Otherwise, the sudoers file won't be considered.

frankfalse avatar
pg flag
Perfect, It was 1 day that I was looking for this answer. Thank you very much
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.