Score:2

`sudo visudo /etc/hosts` gives syntax errors

mx flag

Really dumb question, Ubuntu 20, and I have apache2 installed, not much else. I go sudo visudo /etc/hosts and want to add an alias "dummy.com" domain alias as a hack to point to the server for local only testing a learning site and I get this unusual

>> /etc/hosts: syntax error near line 1 <<<
>>> /etc/hosts: syntax error near line 2 <<<
>>> /etc/hosts: syntax error near line 5 <<<
>>> /etc/hosts: syntax error near line 6 <<<
>>> /etc/hosts: syntax error near line 7 <<<
>>> /etc/hosts: syntax error near line 8 <<<
>>> /etc/hosts: syntax error near line 9 <<<
What now? Q

I removed the 127.0.0.1 localhost notmydomain.com line , so this is my file now, why does it have such syntax errors as above whenever I edit it?

conrad@conrad-VirtualBox:~$ cat /etc/hosts
127.0.0.1   localhost
127.0.1.1   conrad-VirtualBox

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Confused because the browser does work when I visit fakedomain.com (curiously the alias still carries on working in browser even after I removed the fakedomain.com alias and ran sudo service network-manager restart.) I've wasted so much time on this I'm going to re-install, but would love to know where I might have gone wrong.

N0rbert avatar
zw flag
[`man visudo`](http://manpages.ubuntu.com/manpages/focal/en/man8/visudo.8.html) first will help.
mx flag
Oh, yeah, me confusing the commands and the arg it takes I knew I had gone way "off pieste". Apache virtual host was adding the changes, not the hosts file since I was not actually editing that file correctly all along. Good.
guiverc avatar
cn flag
Be aware that Ubuntu Core 20 is a different server product to Ubuntu 20.04 LTS Server. Ubuntu has used the *year* format products to highlight that since 2016 (20 is *snap* only, longer support life, *confined* security model for apps, etc and is thus different to 20.04 for example)
Score:26
vn flag

visudo is a command designed to edit the /etc/sudoers file (and only that file), and perform an integrity check that ensures the file is valid.

If you give it a file as an argument, it will look for another sudoers file, and check that syntax. Since /etc/hosts is a different file with another syntax, this gives errors.

Just edit /etc/hosts with your favorite editor, like this:

sudo vim /etc/hosts

Or you can use the sudoedit (or sudo -e) command, which works the same way as running crontab -e (where you edit a temporary file that are copied to the destination when you're done):

sudoedit /etc/hosts
jp flag
Dan
@ArturMeinild to be nitpicky, `sudo -e ` is even 8 keystrokes (with the extra space). `sudoe` `[Tab]` is still 6 as the `Tab` key will add the space as well. xD
Artur Meinild avatar
vn flag
@Dan I stand corrected!
Tilman avatar
cn flag
Don't run an editor as root with `sudo`. The `sudoedit` command, which is designed for that kind of work, is a much better choice.
Artur Meinild avatar
vn flag
For me, I believe it depends on the actual usecase - but for completeness sake, I've included this example as well.
mx flag
Ah, thanks, that is helpful. I think part of my distress about the hosts file not working was down to the apache web configuration also adds my fake domain to the system at one point. And wondered if I had overridden something while setting up virtual hosts in apache without understanding.
br flag
Do run an editor as root with sudo if it's your machine and that's what you want to do. Don't give *people you don't trust* access to run editors as root; any given editor has enough facilities to let them take over anything they want :)
Persistence avatar
sc flag
@Tilman - That's just bad advice... As long as it's a terminal editor, and not a graphical one, you'll be absolutely fine just using `sudo`... There's no need to overcomplicate things for beginners
in flag
Not that your last line of `sudoedit` can be shortened as `sudo -e`, which is shorter than most commonly used editors
Artur Meinild avatar
vn flag
`sudo -e` is 7 keystrokes (and can't be autocompleted), whereas `sudoe` `[Tab]` is only 6.
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.