How do I edit configuration files?

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.


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.