Linux open config file

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.

Welcome to Ask Ubuntu, please edit your post and provide more details, what do you exactly want to do?

Which files did you want to edit? I am asking because some files require root privileges and some highly recommend that you run a command specifically for editing them (example visudo for the sudoers file or crontab -e for adding cron entries)

4 Answers 4

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:

You must use:

sudo -H gedit /etc/default/grub 

At which point you will be prompted for your password.

The answer depends on which configuration file you want to edit. Most are text files that can be edited with admin privileges. Some are not. For example Firefox configuration should be done from inside Firefox using about:config page.

@user68186 Yes there are all kinds of specialized solutions like you mention. However generically most of the default packages in Ubuntu require 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.

@AndreaLazzarotto In the old days I created 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.

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 :

Читайте также:  На линуксе пропал wifi

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.

Источник

How to Edit Config Files in Ubuntu

Ubuntu Edit Config File

Configuration files contain the information needed to control the operation of a program. Linux users, especially Linux administrators, need to know how to edit configuration files to maintain an Ubuntu server or desktop. Configuration files on Ubuntu are stored in subdirectories of the /etc folder as text files and can be edited with text editors. Some examples of configuration files are /etc/hosts, host.conf, hosts. resolv.conf, /etc/networks, syslog.conf, etc. in Linux-based operating systems.

Читайте также:  Dell vostro wifi linux

To make configuration changes, you need to open the file with a text editor and edit it. In this article, I describe several ways to edit configuration files in Ubuntu. The steps described in this guide have been tested on Ubuntu 22.04, but will work on other Ubuntu versions as well.

  • Using Gedit Editor
  • Using Nano Editor
  • Using Vim Editor
  • Using Gvim Editor
  • Using Emacs Editor

Now let’s take a look at these text editors. By using these editors, you can easily edit files in the terminal .

Using Gedit Editor

Gedit is the default text editor of the Gnome desktop environment. It supports tabs so you can open different files in multiple tabs. It is a simpler and easier text editor as compared to other available text editors.

To edit a config file in the text editor, launch Terminal by pressing Ctrl+Alt+T key combinations. Then type the below command as sudo:

$ sudo gedit /path/to/filename

Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter sudo password.

Example: editing of the /etc/resolv.conf file

Edit file with gedit

It is like the notepad editor in Windows OS. As soon as the editor window opens, you can start typing or editing without entering in any mode. Once you have done with the editing, press Ctrl+S to save the file.

Editing configuration file with GEdit

Using Nano Editor

Nano is the simplest and the most powerful built-in editor in the many Linux distributions. You do not have to learn the Nano editor before using it and also there is no need to remember the commands. They are displayed at the bottom and can be triggered with the Ctrl key. For instance to save a file, press Ctrl+O.

To edit any config file, simply open the Terminal window by pressing the Ctrl+Alt+T key combinations. Navigate to the directory where the file is placed. Then type nano followed by the filename that you want to edit.

Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter sudo password.

edit file in terminal

Now you can edit and make changes in the config file using nano editor.

Edit config file in nano editor

Once you have done editing, press Ctrl+O to save and Ctrl+X to exit the Editor.

Using Vim Editor

Vim or Vi editor also comes pre-installed with most Linux distributions. It has no visible prompts or control that can be a little harder for beginners. However, once you learned it, it is a more quick and powerful tool for editing. Using Vim, you can edit and view multiple files in one session.

To open a file in Vim editor, open the Terminal window by pressing the Ctrl+Alt+T key combinations. Then type vi as sudo followed by filename along with the file path like:

Читайте также:  Linux get cpu clock

Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter sudo password.

Edit file in Vi editor

Then edit the file using Vim editor. Note that when the editor opens, you enter command mode. Here are the few steps to edit a file in Vim editor:

  • To edit the file, Press I from the keyboard to enter into insert mode, here you can do editing just like the normal editor.
  • When done with editing, get out of this mode by pressing Esc. It will return you to normal mode.
  • To save the file, type :w in command mode
  • To quit the editor, type :q in command mode
  • To combine the above actions type :wq to save and quit simultaneously.

Using Vi File Editor on Ubuntu

Using Gvim Editor

Gvim is a Vim editor with a graphical user interface. To install Gvim, launch Terminal by pressing Ctrl+Alt+T. then type the below command in Terminal to start the installation.

$ sudo apt-get install vim-gnome

Install Gvim

Once done with the installation, you can open and edit any config file in it. Type the below command with sudo in Terminal to open any config file in Gvim:

Edit file in linux terminal using GVim

Press I to enter in insert mode where you can edit the file.

GVim file editor

In Gvim editor, you can do various functions using the toolbar just like any normal GUI application like saving the file, undo, redo, finding a keyword, etc.

Using Emacs Editor

Emacs is also a text editor but it is not a built-in editor. You have to manually install it using the Ubuntu Terminal. It has so many features including a built-in web browser, calculator, calendar, Terminal, etc.

To install Emacs editor, launch Terminal by pressing Ctrl+Alt+T key combinations. Type the below command in Terminal as sudo:

Install Emacs editor

Once done with the installation, you can open and edit any config file in it.

To open a config file using the emacs editor, type emacs followed by filename along with the file path in the below syntax:

$ sudo emacs /path/to/filename

Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter the sudo password.

Editing text files in Emacs

Edit the file as normal and save it using the toolbar.

Emacs editor on Ubuntu Linux

You can also open the config file in bash instead of a separate window.

Type the following command as sudo in Terminal:

Using emacs shell mode

Edit the file as a normal text file. Then press Ctrl+X and Ctrl+C to save the file. When it prompts for saving the file, press y to save and quit the editor.

Emavs in shell mode

So these were some simple editors you can use to easily and safely edit config files in your Ubuntu operating system.

About This Site

Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

Latest Tutorials

Источник

Оцените статью
Adblock
detector