Linux users normally edit configuration files with terminal-based tools like
vim . If you want to edit a file graphically—even a system file—the
gedit text editor makes it painless and easy.
Files, Files Everywhere
An oft-repeated phrase concerning Linux and other Unix-based operating systems such as macOS is “everything is a file.”
While that’s not strictly accurate, text files are often used for system logs and configuration. You can read these files to learn more about the inner workings of your operating system, and you can edit them to change its behavior.
The default GNOME text editor is
gedit, so you should find it on any system with a GNOME desktop environment. That includes Ubuntu, Fedora, Debian, CentOS, and Red Hat. It’s a handy tool for editing files when all you need is just enough editor to get the job done—without the learning curve of some of the power-house editors like
gedit from the command line, type
gedit and hit Enter.
gedit text editor will appear shortly.
It’s an uncluttered and clean application window. You can get on with the task of typing up whatever you’re working on with no distractions.
Of course, you can also launch gedit from your Linux desktop’s application menu. It’s often named “Text Editor.” Just search the applications menu for “gedit.”
Launching gedit as a Background Task
The terminal window will wait for
gedit to close before it returns you to the command prompt. If you want to use the terminal window while
gedit is still open, launch
gedit with this command instead. This opens
gedit as a background task. You get the command line prompt back straight away and you can carry on using the terminal window even when
gedit is running.
gedit, a space, an ampersand
&, then press Enter—like this:
Opening an Existing File
To open an existing text file click the “Open” button in the
gedit toolbar. You can also press Ctrl+O to open a file.
This opens the recent files menu. If you want to re-open one of the listed files click on the name of the file. If you wish to open a different file, click the “Other documents…” button at the bottom of the menu.
This opens up a standard file open dialog. You can use this to browse to the location of the file you want to edit.
Click the green “Open” button when you have highlighted the file you wish to edit.
Opening a File from the Command Line
You can ask
gedit to open a file as soon as it launches by providing the filename on the command line. This makes
gedit load the file so that it is ready to be edited as soon as
The syntax highlighting feature of
gedit makes it especially nice to edit program source code files and shell scripts.
Syntax highlighting colors the words within the source file so that variables, reserved words, comments, parameters, and more are easily identifiable.
The name of the file you are editing is displayed in the toolbar. If you have modified the file, an asterisk
* appears beside the file name.
This lets you know that changes have been made to the content of the file. It acts as a reminder that if you want to keep the changes you need to save the file.
Saving Changes to a File
To save your changes, click the “Save” button in the toolbar. You can also press Ctrl+S to save the file.
To save your file with a different name or in a different location click the menu button on the toolbar and then select “Save As” from the menu.
This will open a standard file save dialog. You can browse to the directory you wish to save the file in, and you can provide a name for the file. Click the green “Save” button to save the file.
Editing System Files
To edit a system file, you will usually need to use
sudo because the owner of the file is likely to be
root. To be strictly accurate you’ll be able to open a system file even if you don’t use
sudo, but you won’t be able to save any changes back to the file unless you have used
sudo gedit /etc/samba/smb.conf
Warning: Don’t edit system files if you don’t know exactly what your changes are going to do to your system. Mess up the wrong system file, and you can find yourself locked out of your computer following a reboot.
This command opens
gedit and loads the samba config file for editing.
Replicating Ownership and Permissions to a New FIle
A cautious way to edit system files–and therefore a commendable way to edit system files—is to copy the file and then edit the copy. When you’ve finished editing the new file, you can copy it back over the original file. If you make a mess of editing the copied file, there’s no harm done. Delete it and start over.
When you copy a file, the file ownership can change, and the file mode permissions can be altered. You need to make sure these are exactly the same on your new file as they are on the original file before you copy the new version over the original file. This is how you can do that.
Let’s say we want to edit the
To ensure we have a change of file ownership and mode permissions, we’ll create a new file and then copy the existing file over it. This step is purely for demonstration purposes to make sure the new file does not have the same mode permissions and ownership as the original file. You won’t need to do this when you are editing your own files.
We can use
ls to check the file attributes and see what file mode permissions it has and who the file owner is.
ls -l new_fstab
The file owner is dave, and the file mode permissions are read and write for the file owner and read-only for the group and for others.
Now, we’ll copy the
/etc/fstab file over the new file we just created. We’ll then check the file attributes to see if they have changed.
sudo cp /etc/fstab new_fstab
ls -l new_fstab
fstab has been copied over the
new_fstab file. The file attributes of
new_fstab have not changed. Let’s check the file attributes of the original
ls -l /etc/fstab
As we can see the owner is
root and the file mode permissions are different. The group permissions are read and write. The group permissions for
new_fstab are read-only. We’ll need to correct these two attributes before we copy the file back.
First, we’ll launch
gedit and edit the
new_fstab file to make the required changes.
Once we’ve edited the file and saved our changes we need to set the file ownership and file mode permissions back to what they should be.
We can do this using the
--reference option of the
--reference option takes a filename as a parameter. It forces
chown to take the file mode permissions and file ownership values from that file and to copy them to the target file. We can then use ls to check that the attributes of the edited file are correctly set before we copy that back over the original file.
sudo chmod --reference=/etc/fstab new_fstab
sudo chown --reference=/etc/fstab new_fstab
ls -l new_fstab
The file permissions and ownership are now correct. We can copy the
new_fstab over the existing
fstab and our changes will have been made.
As these are changes to the fstab file, they’d take effect when the computer was next rebooted, or immediately if the mount command was used as so:
sudo mount -a
Be Careful Out There
My watchword is caution, and I’m not above repeating warnings. If you’re at all unsure about how your changes to a system file are going to make your computer behave, don’t make the changes.
When you do need to edit a text file, whether it is a system file or not, you’ll find
gedit is a fast and simple editor that doesn’t bog you down with too many options and yet has enough capability to let you get the job done.