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

Jorge Castro
  • 73,717
Jim
  • 631

5 Answers5

72

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
Oli
  • 299,380
Joren
  • 5,074
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.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
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.

Oli
  • 299,380
Fahad Yousuf
  • 1,264
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.

Jorge Castro
  • 73,717
Arnold
  • 470
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.