1

I cannot open my /etc/hosts file. I would very much like to, I have tried logging in as root and changing the ownership and changing the permissions, please help.

When I try to open the file I get:

-bash: /etc/hosts: permission denied
Eliah Kagan
  • 117,780
Seb
  • 11
  • 1
  • 1
  • 2
  • 2
    How are you trying to open it? Aren't you trying to run it? – choroba Jul 29 '13 at 21:56
  • 2
    I agree with choroba - it sounds like you are trying to run (execute) the file i.e. just typing the filename at the command prompt. You need to specify an editor program e.g. 'nano /etc/hosts' not just '/etc/hosts' – steeldriver Jul 29 '13 at 22:00
  • Are you trying to run it or edit it. – mojo706 Jul 29 '13 at 22:00
  • 1
    /etc/hosts cannot be executed. It is not a program or script. On Windows, you can open a file by issuing the filename by itself as a command. On Ubuntu (and other Unix-like systems), you cannot -- issuing a filename as a command attempts to run a command of that name. – Eliah Kagan Jul 29 '13 at 22:01

2 Answers2

3

Open a terminal and run the following command...

gksudo gedit /etc/hosts

This will open the file in gedit with root privileges allowing you to make your changes.

Cheesemill
  • 987
  • 6
  • 8
  • (gksudo:4878): Gtk-WARNING **: cannot open display: That's what I got in return, with nothing after that colon :( – Seb Jul 29 '13 at 22:01
  • @Seb You can use sudo nano -w /etc/hosts. If you want to edit it with a graphical application, then we can troubleshoot the "cannot open display problem"; to do that, you should tell us how you are logged in? Are you logged in with a normal graphical session, or are you, for example, SSHing in remotely? – Eliah Kagan Jul 29 '13 at 22:02
  • @EliahKagan I don't really understand most of that, but the first command worked a little bit. I'm no pro at this at all, I was just hoping to type into the hosts file, but this has some weird graphical interface thing that I don't understand and it doesn't have a list of my hosts in it so I'm scared! – Seb Jul 29 '13 at 22:21
  • @Seb When you say "some weird graphical interface thing" are you referring to what happens when you run nano, or something else? Also, what do you mean when you say it doesn't have a list of your hosts? What are you expecting? The contents of a hosts file usually look something like this. (By the way, if you just want to look at a file on the command line, you can use cat filename, in this case cat /etc/hosts.) You're typing commands into a terminal. How did you open that terminal? – Eliah Kagan Jul 29 '13 at 22:27
  • @EliahKagan Ok, thank you so much for your help so far, you're being very patient. I think I misunderstood what I should be doing. I was trying to fix an error in connecting to minecraft servers (I know, the shame) and there was a fix in windows that read: "open the "hosts" file using notepad read the paragraph to help you understand the file then enter this on one line by its self: "184.73.223.40 session.minecraft.net"" I'm really sorry to have wasted your help so far, you were really kind, thank you very much, but if you could help me fix this? – Seb Jul 31 '13 at 18:12
  • @Seb In Ubuntu you should be able to do the same thing (you just can't open the hosts file the same way). You might want to back up the file first with the command sudo cp /etc/hosts /etc/hosts.old. If you're not comfortable using a command-line text editor (nano -w), and gksudo gedit /etc/hosts does not work, then you can try sudo -H gedit /etc/hosts. If that fails you can try sudo -i gedit /etc/hosts. You should avoid running just sudo gedit /etc/hosts; it's not likely to work any better and can cause problems. – Eliah Kagan Jul 31 '13 at 18:46
  • @Eliah Kagan

    # 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 Is this what it's supposed to look like? When I did the gksudo one, the text editor came up with 2 tabs, one that looked like the stuff above, and another that never stopped loading. The sudo-H and sudo-i one just brought up 1 tab like the text above (which is hopefully in that grey box if I figured that out). I'm not sure if I type that line into the text above or the other tab that gksudo brought

    – Seb Jul 31 '13 at 19:18
  • @Seb It sounds like gksudo, sudo -H gedit, and sudo -i gedit are all working okay. When you use gksudo gedit, you may get an extra tab in Gedit that doesn't have any document open it. That's normal; it's a known bug in Gedit. You can just use the tab that has your hosts file in it, and ignore the empty one. It's normal for a hosts file in Ubuntu to say "The following lines are desirable..." followed by those 5 lines. You should not change or remove those lines, but you can add more. – Eliah Kagan Jul 31 '13 at 20:40
  • @Seb If you still need help, please edit your question with details of what you're trying to do and what you need help with (or create a new question if what you're asking is very different from your original question); going back-and-forth in comments like this rapidly becomes confusing once the conversation becomes long. Thanks. – Eliah Kagan Jul 31 '13 at 20:47
-1

In the terminal type:

$  sudo su

Type your password...

And you will get the root privileges. Then open the file via any text editor:

By GEdit

#  gedit /etc/hosts

By mousepad ( sudo apt-get install mousepad )

#  mousepad /etc/hosts