How can I enable sudo to edit /etc/hosts?

In the lifecycle before_install, i added the following line of command.

sudo echo " db.test.mysql" >> /etc/hosts

however, i ran into the error saying “Permission denied”. So is there anyone knowing how to fix it?

btw, i also added sudo: required, and there were not any other special configs but a few mysql scripts.

appreciate all the help. tks

is there anyone?:joy:

In general, I suggest testing commands in the most basic form before you put it in your configuration.

On your machine, you will know that sudo would not work this way; sudo only invokes the command echo, but the I/O redirection with >> is used by the shell, which only has the user privilege. You need to write to the file with the root privilege; e.g.,

echo " db.test.mysql" | sudo tee -a /etc/hosts

Or, you can use the “hosts” addon.

1 Like