![](/uploads/1/2/5/7/125746207/481216424.jpg)
Are there any special considerations to editing a file as root? What is the safest way to do this?
GEdit is a Linux text editor commonly deployed as part of the GNOME desktop environment. Most Linux guides and tutorials will get you to use the nano editor or vi to edit text files and configuration files and the reason for this is that nano and vi are almost guaranteed to be installed as part of the Linux. How can the answer be improved?
strugeestrugee8,4991212 gold badges5858 silver badges103103 bronze badges
![Linux Edit File Linux Edit File](/uploads/1/2/5/7/125746207/783428993.png)
2 Answers
Obviously, if you know how to run any command as root, you can run an editor. But that's not the most convenient or the safest method.
sudoedit
(or its synonym sudo -e
) is a mode of sudo where you edit a temporary copy of a file in your favorite editor, and that copy is moved into place when you finish editing.Note: There are a few files that you should not edit directly.
Never edit
/etc/sudoers
directly. Always use the visudo
command for that. If you make a mistake in the syntax of the sudoers file, you could be locked out of the root account! When you run visudo
, it lets you edit a temporary copy of the file and performs a syntax verification before replacing the actual file by your edits.The same principle applies to the user, group, user password, and group password databases,
/etc/passwd
, /etc/group
, /etc/shadow
, and /etc/gshadow
, respectively. On systems where they are available, use the vipw
, vigr
commands for /etc/passwd
and /etc/group
. To edit the password databases, pass the -s option. For example, to edit /etc/gshadow
, use vigr -s
.Always make sure to keep a backup of the file you are editing. Some people like to put a time stamp on after that. This ensures you can always revert (well, hopefully )if the system breaks as a result of the change. In addition to the command listed by strugee, most set ups will allow you to run your usual editors (vi,vim,nano,emacs). Just be cognicent whether the file is read only or not.
LukeLuke
Not the answer you're looking for? Browse other questions tagged sudoroot or ask your own question.
![](/uploads/1/2/5/7/125746207/481216424.jpg)