2

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.

3
  • 2
    man visudo first will help.
    – N0rbert
    Jan 17 at 15:59
  • 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.
    – Conrad B
    Jan 17 at 16:37
  • 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)
    – guiverc
    Jan 17 at 22:44

1 Answer 1

26

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
7
  • 5
    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.
    – Tilman
    Jan 17 at 15:32
  • 3
    For me, I believe it depends on the actual usecase - but for completeness sake, I've included this example as well. Jan 17 at 16:14
  • 5
    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 :)
    – hobbs
    Jan 18 at 5:26
  • 1
    @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 Jan 18 at 11:29
  • 1
    sudo -e is 7 keystrokes (and can't be autocompleted), whereas sudoe [Tab] is only 6. 😉 Jan 18 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.