var img = document.createElement('img'); img.src = "https://easystat.de/piwik.php?idsite=13&rec=1&url=https://docs.vps2day.com" + location.pathname; img.style = "border:0"; img.alt = "tracker"; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(img,s);
Skip to main content

How to edit a file in Linux

To follow along tutorials, it's often necessary to open and edit files on your server. In this tutorial, we'll show you how to edit a file in Linux using a variety of different editors. This tutorial is not specific to any kind of distribution and should work on any Linux distribution - given that you have installed the editor you want to use.

If you're uncertain which editor to use and never worked with Linux before, we recommend using nano as it's a simple yet powerful editor that comes pre-installed on most Linux distributions.

Before you start

Generally, it's a good idea to make a backup of a file before editing it, especially if it's a configuration file or if you plan to make a lot of changes to a file. If something goes wrong, you can easily revert your changes by restoring the backup.

info

To make a backup of a file, you can use the cp command. For example, if you want to make a backup of the file /etc/ssh/sshd_config, you can run the following command:

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

If you later want to restore that backup, you can simply run the following command:

cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config

Using nano

In order to edit a file using nano, you need to run the following command:

Opening the editor

nano <FILE>

You need to replace <FILE> with the path to the file you want to edit. For example, if you want to edit the file /etc/ssh/sshd_config, you need to type:

nano /etc/ssh/sshd_config
info

Depending on which file you edit, you might get a red warning notice in the editor that tells you that the file you're editing is unwritable.

Permission Denied

In this case, you need to run the command as privileged user by prepending the sudo command in front of your command:

sudo nano <FILE>

That will run nano as privileged user, and you'll be able to edit and actually save the file.

Once you're in the editor, you can use your arrow keys to navigate the file. You can also use the Page Up and Page Down keys to scroll through the file.

Cursor Position

If you want to see in which line you are currently at, you can press CTRL + C and it will show you the current line number like this:

[ line 5/125 (4%), col 1/1 (100%), char 164/3309 (4%) ]

In this example, we're in line 5 of 125 the file has in total. Within that line we're in column 1 of 1 (which means that the line is empty).

Searching

To search for a specific string in the file, you can press CTRL + W and then type the string you want to search for. Afterward, you can hit Enter and the cursor will jump to the first occurrence of the string you searched for. To jump to the next occurrence, you can press CTRL + W followed by Enter again.

Saving the file

Usually, when you're done editing, you want to save the file and quit the editor afterward. In order to achieve this in nano, all you need to do is hitting STRG + X followed by pressing Y and then Enter and then Enter a second time.

danger

Once you've saved the file and exited it, you can not revert your changes, unless you made a backup of the file before editing it.

If you want to exit the editor without saving the file, you can press STRG + X followed by pressing N. That will immediately close the editor without saving any changes.