Linux users normally edit configuration files with terminal-based tools such as nano and vim. If you want to edit a file graphically – even a system file -, the gedit text editor makes it simple and fast.
Files, files everywhere
An often repeated phrase about Linux and other Unix operating systems such as macOS is "everything is a file. "
Although this is not strictly accurate, text files are often used for logs and system configuration. You can read these files to learn more about the internal workings of your operating system and modify them to change their behavior.
The default GNOME text editor is gedit. You must find it on any system with a GNOME desktop environment. This 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 do the work without the learning curve of some of the top performing editors like vim.
To start gedit from the command line, type gedit and press Enter.
The gedit text editor will appear shortly.
It is a clean and uncluttered application window. You can start typing anything you're working on, without distraction.
Of course, you can also run gedit from the applications menu on your Linux desktop. It is often called "Text Editor". Just search for "gedit" in the application menu.
Launch gedit in the background
The terminal window will wait until gedit closes before returning to the command prompt. If you want to use the terminal window while gedit is still open, run gedit with this command. This opens gedit in the background. You immediately get the command line and you can continue to use the terminal window even when gedit is running.
Type gedit, a space, an ampersand & then press Enter – like this:
Open 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 menu of recent files. If you want to reopen one of the listed files, click the file name. If you want to open another file, click on the "More documents …" button at the bottom of the menu.
This opens a standard open file dialog. You can use it to search the location of the file you want to edit.
Click the green "Open" button when you have selected the file you want to edit.
Open a file from the command line
You can ask gedit to open a file as soon as it is launched by specifying the file name on the command line. This allows gedit to load the file so that it is ready to be edited as soon as gedit appears.
The syntax highlighting feature of gedit makes it particularly nice to edit source code files for programs and shell scripts.
Highlighting the syntax colors the words in the source file so that variables, reserved words, comments, parameters, etc., 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 next to the file name.
This lets you know that changes have been made to the contents of the file. This reminds you that if you want to keep the changes, you must 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 under a different name or location, click the menu button on the toolbar and select "Save As" from the menu.
This will open a standard file backup dialog. You can access the directory where you want to save the file and give it a name. Click the green "Save" button to save the file.
Editing system files
To edit a system file, you will usually have to use sudo because the file owner is probably root. To be precise, you will be able to open a system file even if you do not use sudo, but you will not be able to save any changes to the file if you have not used sudo.
sudo gedit /etc/samba/smb.conf
WarningDo not modify system files if you do not know exactly what your changes will do to your system. Remove the wrong system file and you may get stuck after a reboot.
This command opens gedit and loads the samba configuration file for editing.
Replication of ownership and permissions to a new domain
A conservative way to edit system files – and therefore a recommended way to edit system files – is to copy the file and then edit the copy. When you have finished editing the new file, you can copy it to the original file. If you modify the copied file, you will not risk anything. Delete it and try again.
When you copy a file, the file's property may change, and file-mode permissions may change. Before copying the new version on top of the original file, make sure that these are exactly the same on your new file and on the original file. That's how you can do that.
Let's say we want to edit the fstab file.
To ensure that the mode and file owner permissions are changed, we will create a new file and then copy the existing file to it. This step is purely for demonstration purposes to ensure that the new file does not have the same mode and property permissions as the original file. You do not need to do this when you are editing your own files.
We can use ls to check file attributes and see what permissions are in file mode and who owns the file.
ls -l new_fstab
The owner of the file is dave, and file-mode permissions are read and write for the file owner and read-only for the group and others.
We will now copy the / etc / fstab file over the new file we just created. We will then check the file attributes to see if they have changed.
sudo cp / etc / fstab new_fstab
ls -l new_fstab
The fstab file has been copied to the new_fstab file. The file attributes of new_fstab have not changed. Let's check the file attributes of the original fstab file.
ls -l / etc / fstab
As we can see, the owner is root and the permissions in file mode are different. Group permissions are read and write. Group permissions for new_fstab are read-only. We will have to correct these two attributes before copying the file.
First, we will launch gedit and edit the new_fstab file to make the required changes.
Once we have modified the file and saved our changes, we need to redefine the property and file mode permissions on what they should be.
We can do this using the –reference option of the chmod and chown commands.
The –reference option takes a file name as a parameter. It forces chmod and chown to extract the file mode permissions and file property values from this file and copy them to the target file. We can then use ls to verify that the attributes of the modified file are set correctly before copying them to 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 the property are now correct. We can copy the new_fstab to the existing fstab and our changes will have been made.
Regarding changes made to the fstab file, they will take effect the next time the computer reboots or immediately if the mount command was used as follows:
sudo mount -a
Be careful there
My word of caution is caution, and I am not above repeating warnings. If you are not sure how your changes in a system file will cause your computer to behave, do not make changes.
When you need to edit a text file, whether it's a system file or not, you'll find that gedit is a quick and easy editor that does not take you away with too many options, yet offers enough work done.