Skip to main content
How To

Edit Config Files in Ubuntu

Got to edit some config file in Ubuntu? Here's what you need to know and do.

Sagar Sharma

"Everything is a file in Linux" and to make things work as per your needs, you have a config file for almost every service out there.

But the question is how do you edit them? Simple. Use a text editor.

And in this tutorial, I'm going to walk you through the following:

  • Create a backup of the important config files (optional)
  • Edit config file using terminal
  • Edit config file using GUI

So let's start with the first one.

Create a backup of the config file (optional)

This is an optional step but highly recommended as if you screw up while making changes in the config file, how would you revert to the previous condition?

Simple. Using the old config file.

This can be very helpful in cases where you are configuring services like SSH or editing the config file of a window manager.

Don't worry, you don't have to use any complex or new tool to create a backup but the cp command that you use to create a copy of a file.

💡
While creating a copy of a file, I would recommend using the .backup extension for the copy so it will be easy for you to identify the backup file.

Also, I would recommend creating a separate directory to store all the backup files. Here, I created a directory for that case named Backup:

mkdir Backup

For your reference, here, I created a backup of the .bashrc file by naming it .bashrc.backup inside the Backup directory:

Create backup of config file

And there you have it!

Edit a config file in the terminal

The nano text editor is the easiest way to edit any config file in the terminal as unlike Vim, you can navigate through the arrow keys, and make changes easily.

While it comes pre-installed in most of the Linux distros, if you went with an extremely minimal install, then it can be installed with the following:

sudo apt install nano

Once done, you can start editing of config file with Nano using the following:

nano Filename.txt

And if the file requires superuser privileges, then you use sudo:

sudo nano Filename.txt

Here's a simple example of how you can edit files with the nano text editor:

edit config files in terminal

To save changes and exit from the nano text editor, you press Ctrl + o and Ctrl + x.

Things are a little different with the sudoers file

It is related to the sudoers file which sure can be edited normally, but with broken syntax, it may break the system and this is the reason why you are advised to use visudo with it.

And when you use the visudo command to edit the sudoers file, it does 2 things:

  • Checks the syntax and if you made any mistakes, it will notify and prevent you from saving the file.
  • While editing, it creates a lock file sodoers.tmp which will prevent other users to make changes while you're editing the file.

By default, it will use the Vim text editor to edit the sudoers file but if you want to open the sudoers file with another editor then, you can use the following:

EDITOR=<Text-editor-name> sudo visudo

For example, if you want to open the sudoers file with the nano text editor, then, you can use the following:

EDITOR=nano sudo visudo

Alternatively, to select the permanent editor, you can execute the following:

sudo update-alternatives --config editor

Once you select the default editor of your choice, you can use the visudo command without any additional flags:

sudo visudo

Edit a config file in GUI

There are various IDEs and text editors available for GUI that lets you easily edit the config file, but here, I will show you how you can use the gedit to edit config files.

First, let's install Gedit in Ubuntu with apt:

sudo apt install gedit

Now, there are two ways to start editing a file with Gedit:

  • From the file manager
  • From the terminal

To start the file editing from the file manager itself, first, go to the directory where the file is located and then, select and right-click on that file and choose the first option Open With Text Editor:

It should open the Gedit by default, but if you see another option, then, you can select the second option Open With Other Application and from there, you can choose the Text editor:

Edit config files in Gedit

Alternatively, if you want to start the config file edit with Gedit in the terminal, then, you can use the gedit command and append the filename to it:

gedit Filename.txt

After using either of the two methods, it will start the Gedit with the specified config file.

And here, you can make changes as per your needs, and once done, hit the Save button located at top right corner:

Save changes in Gedit

But if try to edit a file that needs elevated privileges, then, it won't allow you to save changes by making the Save button unusable:

save button not working with Gedit

In that case, you have to use the sudo while starting the gedit:

sudo gedit Filename.txt

Once you do that, you can configure files that are located in the root directory or needs elevated privileges to make any changes.

I hope you will find this helpful in editing config files in Ubuntu.

Sagar Sharma