How do I edit configuration files?

8,422

Solution 1

Configuration files are usually owned by root. For example:

$ ll /etc/default/grub

-rw-r--r-- 1 root root 6801 Jul 18 13:26 /etc/default/grub

 ^^ ^  ^
 || |  +-- Users can only read
 || +----- Members of the group can only read
 |+------- The owner can write
 +-------- The owner can read

In order for a user (yourself) to edit /etc/grub/default you need to use sudo powers. So instead of using:

gedit /etc/default/grub

You must use:

sudo -H gedit /etc/default/grub

At which point you will be prompted for your password.

Note: Related question today: Grub file can't be saved after modification

Solution 2

System-wide configuration files, which are often in /etc, are owned by root and you need to elevate your privileges to edit them. It's common to run a text editor as root with sudo, and other answers here show how. But just because only root can make changes the files does not mean you have to run your text editor as root. You can instead use sudoedit, which is documented in the same manpage as sudo.

For example, to edit /etc/hosts:

sudoedit /etc/hosts

The way this works is that sudoedit:

  1. Checks if you've recently authenticated with sudo from the terminal you're in (like sudo does), an prompts you for your password if you haven't.
  2. Checks if it is clear that could just edit the file normally, without sudoedit or sudo. If so, it gives you an error message and quits. (In this situation, you should just edit the file normally as DK Bose says, assuming it makes sense to do so.)
  3. Makes a temporary copy of the file, opens that file in a text editor, and waits for the editor to quit.
  4. Checks if the file has changed, and updates the original if it has.

sudoedit works with your choice of text editor. To edit with nano:

VISUAL=nano sudoedit /etc/hosts

It even works with GUI editors like Gedit:

VISUAL=gedit sudoedit /etc/hosts

Furthermore, because the editor is run as you, it uses your configuration, so if you've customized the behavior of your editor, those customizations are used. Other ways of attempting this tend not to work very well.

You can set the VISUAL environment variable persistently for your user to make sudoedit, and various other commands that need to pick an editor, use your preferred editor. If you want to set an editor just for sudoedit (and visudo) but not other programs, set SUDO_EDITOR instead of VISUAL.

Because the logic of checking if two files are the same and copying one onto another is simpler than the full logic of a text editor--and especially simpler than the full operation of a graphical program like Gedit--running sudoedit might be considered more secure in the specific sense that it is associated with a smaller attack surface. Running less stuff as root is good. But the main reason to use sudoedit is convenience.

Because sudoedit is so useful and versatile, its benefits are sometimes overstated. It's a good tool, but you don't have to use it, and using it instead of other methods doesn't affect security in any decisive way. If you change system-wide configuration files in a way that breaks things, they will still break! That seems obvious when stated that way, but sometimes people try to give users the right to edit files owned by root with sudoedit while still trying to restrain those users in some way through technical measures. This is not usually practical.

Solution 3

There are also configuration files in your home folder and its subfolders such as the .config folder. The preceding "." signifies the folder (or file) is hidden normally. Some config files may also be directly in your home folder. If you're using a file manager, Control+H will hide/reveal such files/folders.

Such files can be edited by a plain text editor with convenient GUIs (graphical user interfaces) similar to notepad in Windows. Editing such files should not require the use of sudo since you are the owner of these files. Indeed using sudo on files in your home folder is rarely, if ever, needed.

Some examples of plain text editors, most of them offering syntax highlighting, bracket matching, etc:

  • gedit in Ubuntu
  • mousepad in Xubuntu
  • leafpad in Lubuntu 18.04
  • featherpad in Lubuntu 19.04+
  • kate or kwrite in Kubuntu ...

You could also use LibreOffice Writer but make sure you save your file as plain text and not in LibreOffice Writer's default Open Document Format.

Mostly though, you needn't edit config files directly. (That's why these files are mostly in hidden folders.) The preferred way is to use the GUI of whichever application your using. That way, you'll avoid making any "syntax" errors.

Solution 4

As mentioned by WinEunuuchs2Unix, most of the configuration files are owned by root so typing "sudo" before the editor of your choice is necessary. I use "vim" to edit files so from the command line I type

sudo vim (filename).

or if you use nano which comes with Ubuntu by default

sudo nano (filename)

There are many editors you can choose from; vim, emacs, gedit, nano, and many more.

Also, as I mentioned in my comment above it is recommended to run sudo visudo by itself if editing the sudoers file and also crontab -e when adding cron entries. Both of these commands will essentially open up the vim editor for editing files by default so I recommend getting familiar with vim but also exploring the other editors out there. It is very important to read the comments or notes at the top of configuration files to see if the system recommends against editing the files directly.

Share:
8,422

Related videos on Youtube

ashwinnraj
Author by

ashwinnraj

Updated on September 18, 2022

Comments

  • ashwinnraj
    ashwinnraj over 1 year

    I'm a beginner to Ubuntu. Can anyone tell me how to edit a configuration file?

    This is on Ubuntu 18.04 LTS. Thanks in advance.

    • Admin
      Admin almost 5 years
      Welcome to Ask Ubuntu, please edit your post and provide more details, what do you exactly want to do?
    • Gordster
      Gordster almost 5 years
      Which files did you want to edit? I am asking because some files require root privileges and some highly recommend that you run a command specifically for editing them (example visudo for the sudoers file or crontab -e for adding cron entries)
  • user68186
    user68186 almost 5 years
    The answer depends on which configuration file you want to edit. Most are text files that can be edited with admin privileges. Some are not. For example Firefox configuration should be done from inside Firefox using about:config page.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 5 years
    @user68186 Yes there are all kinds of specialized solutions like you mention. However generically most of the default packages in Ubuntu require sudo to edit configuration files like grub, plymouth, systemd, etc. These are just examples of booting the system. There are many more applications after system is booted that also require sudo. I believe this to be the spirit of the question. Adding all the exceptions would make all the answers a textbook.
  • user68186
    user68186 almost 5 years
    Fair enough. :-)
  • Gordster
    Gordster almost 5 years
    @user68186 added nano to my answer :)
  • Andrea Lazzarotto
    Andrea Lazzarotto over 4 years
    Rather than sudo gedit, isn't it better to use gedit admin:///... nowadays?
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 4 years
    @AndreaLazzarotto In the old days I created sgedit script which copies users profile for gedit to root so I actually don't even use sudo -H gedit except buried within that script. i think there is a gksu gedit Q&A or two here in Ask Ubuntu where gedit admin:/// would be a great answer for you to post. Please let me know when you do and I'll check it out.
  • Andrea Lazzarotto
    Andrea Lazzarotto over 4 years
    I think I learnt about the admin prefix here on AU. Is there any specific place where you suggest I should post that as an answer? Do you mean this question or other questions?
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 4 years
    @AndreaLazzarotto There are many questions about what to replace gksu with. Here's the one with my answer sgedt but it may not be the best place for your answer: askubuntu.com/questions/1042344/…
  • Eliah Kagan
    Eliah Kagan over 4 years
    User-specific configuration files are common and this is a good answer. But while it is sometimes best to avoid editing configuration files directly, in some cases (e.g., ~/.vimrc) manual editing is reasonable and recommended in official documentation. Configuration files are often named starting with . (or put in directories that are) because we don't usually want to be distracted by their presence (before ~/.config/ was in widespread use, home directories tended to have many dotfiles) and because operations we intuitively think of as "do X on all files here" should often skip them.