63

I'm new at Linux and I just download Ubuntu 12.04 LTS. I found a spot that showed me how join my Windows Active Directory. It has me add couple lines into the 'hosts' but I'm not able to edit the file because I do not have the permission. I'm not the owner. I use chmod with it rwx and still it will not let edit the file. How do you change the permission? Thanks

1
  • Jorge, perhaps I'm going the wrong direction in joining with Windows Active Directory. But I'm still taking the command you are giving me and it's not working. You can review what I'm following and then maybe it can shine some light on the subject. systemadminthings.com/2012/05/…. Thanks
    – Jim
    Commented Sep 9, 2013 at 22:07

5 Answers 5

71

The easiest way (most GUI) would be to use gedit. You could also use any other editor you prefer (e.g. nano or vi). Just run the editor as root using sudo and you're good to go! You need to pass 1 argument: the path to the file you want to edit (in this case /etc/hosts).

The full command:

sudo -i gedit /etc/hosts
4
  • 10
    Or you can use sudo nano /etc/hosts or sudo vim /etc/hosts, because not everybody has the GUI.
    – Thomas Ward
    Commented Sep 9, 2013 at 21:14
  • 1
    That's exactly what I put in my answer, gedit was just meant as an example editor.
    – Joren
    Commented Sep 9, 2013 at 21:15
  • 1
    Be careful with sudo gedit. If @guntbert will see your answer, it will not be ok for you because he doesn't like to see practices like these. Commented Sep 9, 2013 at 21:30
  • sudoedit /etc/hosts
    – kzh
    Commented Nov 3, 2015 at 20:08
11

It is not a good idea to become the owner of a system file/directory! So, if you want to edit a system file, it is not necessary to be the owner of that file. Just use in terminal:

sudo -i            # to grant access as root
gedit file_name    # or /path/to/file/file_name

After you finished to edit your file, press Ctrl+D in terminal to exit from root session.

But to become the owner of a file/directory, use chown. For example:

sudo chown your_username file_or_directory

Check man chown and man chmod for more information.

4
  • 1
    @user68186 No, it is not a good idea. But, as you can see, that problem can be solved. Commented Sep 9, 2013 at 21:02
  • 12
    I don't think chowning /etc/hosts to a user account is a good idea! Commented Sep 9, 2013 at 21:03
  • @JorgeCastro This is exactly what I said in a previous comment. I start to explain about chown because the question was initially tagged with 'ownership'. Commented Sep 9, 2013 at 21:14
  • 2
    I think this answer sort of buries the lede. chowning /etc/hosts has no advantages whatsoever over other approaches to the OP's problem, and although it is possible to read the OP's question as being about how to change file ownership rather than how to edit hosts, this is a case where such literal interpretation is not very helpful. Although it would change the message of your answer, I suggest flipping the order of the information: sudo -i first, and chown as an afterthought. Commented Jan 26, 2017 at 13:45
8

You should use sudo -i (nano for text-based or gedit for graphical) /etc/hosts. By default normal users should not be able to edit the hosts file. Changing the ownership of hosts is not the right thing to do.

2

Open a terminal and try this:

sudo vi file.txt

file.txt is the file you want to edit.

The system will ask you to enter your password so you can execute the command with root privileges.

2
  • Thank you Jorge Castro for the edit. I don't know how to put a gray background. I'll read on that :)
    – Arnold
    Commented Sep 9, 2013 at 21:12
  • If you use a GUI browser (like Firefox, Chromium), see here: i.sstatic.net/BxiYM.png Commented Sep 9, 2013 at 21:21
1

Rare case (but it was mine): The file system may be mounted read-only. This may happen unexpectedly due to a disk / kernel problem.

1
  • If it is mounted as read-only you can use this answer to remount as writable: askubuntu.com/a/175742
    – Atnas
    Commented Apr 18, 2023 at 17:53

Not the answer you're looking for? Browse other questions tagged .