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.
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.
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
about:config
page.
– user68186
Jul 24 '19 at 16:06
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.
– WinEunuuchs2Unix
Jul 24 '19 at 16:42
sudo gedit
, isn't it better to use gedit admin:///...
nowadays?
– Andrea Lazzarotto
Aug 25 '19 at 00:16
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.
– WinEunuuchs2Unix
Aug 25 '19 at 00:48
sgedt
but it may not be the best place for your answer: https://askubuntu.com/questions/1042344/i-need-an-equivalent-of-gksu-in-18-04
– WinEunuuchs2Unix
Aug 25 '19 at 13:44
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
:
sudo
from the terminal you're in (like sudo
does), an prompts you for your password if you haven't.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.)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.
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:
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.
~/.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.
– Eliah Kagan
Oct 24 '19 at 19:47
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.
visudo
for the sudoers file orcrontab -e
for adding cron entries) – Gordster Jul 24 '19 at 15:26