30

I'm a brand new user so I'm not sure of my way around the system yet. I want to change my computer's name, so I tried to edit the name in /etc/hostname, but it is a read-only file. Adding the ! character didn't help. I'm using vim to edit the file.

Zanna
  • 70,465
  • 1
    How is tagMatcher's answer any better than http://askubuntu.com/a/92382/158442? There is literally nothing new. You can accept it if you want, but merging it is not needed. – muru Dec 17 '14 at 02:36
  • Set the default output in the config file sudo nano /etc/ansible/ansible.cfg and paste the following line

    [defaults] # Human-readable output stdout_callback = yaml

    https://www.shellhacks.com/ansible-human-readable-output-format/

    – AZ_ Nov 06 '22 at 13:07

6 Answers6

38

Changing system settings requires superuser permissions. From a terminal, do

sudo <editor> <filename>

where could be vim or nano or any other editor command, and is the one you need to edit.

You could also usegksudo gedit <filename>.

In either case you will be prompted for password of a user with superuser permissions - in a normal Ubuntu installation this would be the first user created during installation.

tagMacher
  • 650
  • 8
  • 14
19

If you are editing a file without sudo, and you need sudo in order to save, simply use this vim command:

:w !sudo tee %

Credit to Dr Beco. Note that vim will notice the file change and ask you if you want to (L)oad the changes, press L.

  • thanks! this works even when the file's permissions are 400 or 440, with no one having write permission! – Yoga Nov 11 '17 at 03:32
  • This doesn't work for me, running as sudo or even logged in as root. I get a message "Read-only file system" when I try this, and "Shel returned 1", with no change and no ability to save the file. -----EDIT: indeed, this can't be done when you're in a read-only filesystem (my example was an app installed with snap; I was trying to modify the .desktop file). However, there are alternative locations for the .desktop files, e.g: /var/lib/snap/desktop/applications – Marses Apr 10 '20 at 13:41
8

Very Short Answer:

You can modify a file (even if it's read-only) if you own it.

Short Answer:

Even if the user which you are logged in as (in this case navid) has administrative privileges, you won't be able to modify /etc/hostname, because the root user owns that file. Consequently, you should log in as the root user.

Long Answer

Assuming that you want to change your computer name from oldName to newName, here are the steps that you should follow:

  1. Log in as the root user:

    navid@oldName:~$ sudo su -
    
  2. Open hostname:

    root@oldName:~# vi /etc/hostname
    
  3. You will see oldName. Press i to go to the insert mode then change it to newName. Then press Esc + : + w + q + Enter to save and exit.

  4. Open hosts:

    root@oldName:~# vi /etc/hosts
    

    The top 2 lines look like this:

    127.0.0.1       localhost
    127.0.1.1       oldName
    
  5. Similarly to what you did in step 3, change the computer name from oldName to newName. Then save and exit.

  6. Exit the root user:

    root@oldName:~# exit
    
  7. Save all of your unsaved work and restart your computer:

    navid@oldName:~$ reboot
    
  8. Open your terminal, and you will see that your computer name has successfully been changed! :-)

    navid@newName:~$ 
    

Note: Although you may achieve what you want by skipping steps 4 and 5, I strongly recommend doing them as well, to avoid potential errors in the future.

See also:

Positive Navid
  • 925
  • 3
  • 12
  • 19
0
sudo -H gedit <path to file>

eg :

sudo -H gedit /etc/environment

or sudo nano /etc/environment Ctrl + X and Ctrl + Y and hit Enter(for nano)

wittich
  • 1,174
  • 13
  • 26
ArUn
  • 357
  • 1
  • 5
  • 13
-1

You might not have access to the file to change it.

In order to get access you need to get super user privileges and that is sudo.

Open terminal, type:

sudo -H gedit "your file name "

If you use any other text editor type that in.

muru
  • 197,895
  • 55
  • 485
  • 740
-3

By default you have wight permission to hosts file to root user only, as shown below:

-rw-r--r-- 1 root root 252 Dec 16 08:40 /etc/hosts

Hence you have to use sudo to edit this or else you can use below command to become root

sudo su -

however this is not recommended at all.

safest and recommended it you use below command to edit the file

sudo vim /etc/hosts

then press "Esc" on keyboard and then type ":w!" to write the changes to the hosts file then agin type ":q" or ":q!", this should shall do the trick for you.

Hrish
  • 2,343