Linux text editor root

Editing system files in Linux (as root) with GUI and CLI text editors #3

One year and a half ago I posted the second iteration of this script for a review here: Editing system files in Linux (as root) with GUI and CLI text editors #2 Since then, it has been «hibernated» as I had way too much work, and I would like to ask you for a review of the possible final edit. I made a big effort for it to be final, but we all know there is always some space to improve. Thank you in advance! As stated in there:

My intention is to POSIX-ly write one generalized function for running various text editors I use for different purposes through sudoedit , i.e. editing files as root safely. Safely = for instance, if a power loss occurs during the file edit; another example could be lost SSH connection, etc.

The script follows

#!/bin/sh # Please, customize these lists to your preference before using this script! cli_editors='vi nano' gui_editors='subl xed' # NON-COMPLIANT: code -w --user-data-dir; I did not find a way to run it through `sudoedit`; # atom -w --no-sandbox; gedit -w does not work via rooted ssh; pluma does not have -w switch # USAGE INSTRUCTIONS # 1. Customize the editor lists at the beginning of this script. # # 2. Bash: Source this script in your ~/.bashrc or ~/.bash_aliases with: # . /full/path/to/sudoedit-enhanced # Other shells: Generally put it inside your shell's startup config file. # # 3. Call an alias, for instance, one CLI, and one GUI editor: # sunano /path/to/file # susubl /path/to/file # # Explanation: This script works with standard `sudoedit`, but # it does a bit more checks and allows some GUI editors to be used. # It needs to be sourced into your shell's environment first. # Then simply use the pre-defined aliases or define some yourself. sudoedit_err () < printf >&2 '%s\n' 'Error in sudoedit_run():' printf >&2 '%b\n' "$@" exit 1 > sudoedit_run () < # print usage if [ "$#" -eq 2 ]; then printf >&2 '%s\n' 'Usage example: sunano /file1/path /file2/path' exit 1 fi # sudo must be installed if ! command -v sudo > /dev/null 2>&1; then sudoedit_err "'sudo' is required by this function. This is because" \ "'sudoedit' is part of 'sudo'\`s edit feature (sudo -e)" fi # the first argument must be an editor type case "$1" in ('cli'|'gui') ;; (*) sudoedit_err "'$1' is unrecognized editor type, expected 'cli' or 'gui'." ;; esac # store editor's type and name and move these two out of argument array editor_type=$1; editor_name=$2; shift 2 # find and store path to this editor editor_path=$( command -v "$editor_name" 2> /dev/null ) # check if such editor = executable path exists if ! [ -x "$editor_path" ]; then sudoedit_err "The '$editor_name' editor is not properly installed on this system." fi # `sudoedit` does not work with symlinks! # translating symlinks to normal file paths using `readlink` if available if command -v readlink > /dev/null 2>&1; then for file in "$@"; do if [ -L "$file" ]; then if ! file=$( readlink -f "$file" ); then sudoedit_err "readlink -f $file failed." fi fi set -- "$@" "$file" shift done fi if [ "$editor_type" = gui ]; then # 1. GUI editors will "sit" on the terminal until closed thanks to the wait option # 2. Various editors errors might flood the terminal, so we redirect all output, # into prepared temporary file in order to filter possible "sudoedit: file unchanged" lines. if tmpfile=$( mktemp /tmp/sudoedit_run.XXXXXXXXXX ); then SUDO_EDITOR="$editor_path -w" sudoedit -- "$@" > "$tmpfile" 2>&1 grep 'sudoedit:' "$tmpfile" rm "$tmpfile" else sudoedit_err "mktemp /tmp/sudoedit_run.XXXXXXXXXX failed." fi else # 1. CLI editors do not cause problems mentioned above. # 2. This is a generic and proper way using `sudoedit`; # Running the editor with one-time SUDO_EDITOR setup. SUDO_EDITOR="$editor_path" sudoedit -- "$@" fi > sudoedit_sub () < ( sudoedit_run "$@" ) ># Editor aliases generators: # - avoid generating editors aliases for which editor is not installed # - avoid generating editors aliases for which already have an alias for cli_editor in $cli_editors; do if command -v "$cli_editor" > /dev/null 2>&1; then # shellcheck disable=SC2139,SC2086 if [ -z "$( alias su$cli_editor 2> /dev/null)" ]; then alias su$cli_editor="sudoedit_sub cli $cli_editor" fi fi done for gui_editor in $gui_editors; do if command -v "$gui_editor" > /dev/null 2>&1; then # shellcheck disable=SC2139,SC2086 if [ -z "$( alias su$gui_editor 2> /dev/null)" ]; then alias su$gui_editor="sudoedit_sub gui $gui_editor" fi fi done unset cli_editors gui_editors 

Источник

Читайте также:  Linux узнать файловый дескриптор

sudo text editor

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. 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.

How do I Sudo to open a file?

  1. Open the Terminal.
  2. Type sudo su and press enter.
  3. Provide your password and press enter.
  4. Then type apt-get install -y nautilus-admin and press enter.
  5. Now type nautilus -q and press enter.
  6. Finally type exit and press enter, and close the terminal window.

How do I open the text editor as root in Linux?

To open (text) files as root I use Nemo, right click the folder containing the file and select “Open as Root”. With “Elevated Privileges” e.g. xed will let me edit and save.

How do I use text editor in Linux?

To start writing or editing, you must enter insert mode by pressing the letter i on your keyboard (“I” for insert). You should see —INSERT— at the bottom of your terminal page if you did it correctly. When you are finished typing, and you want to save your work, you need to exit insert mode.

How do I restart Sudo service?

  1. Linux provides fine-grained control over system services through systemd, using the systemctl command. .
  2. To verify whether a service is active or not, run this command: sudo systemctl status apache2. .
  3. To stop and restart the service in Linux, use the command: sudo systemctl restart SERVICE_NAME.

How do I edit a file in Nano?

  1. Log into your server via SSH.
  2. Navigate to the directory location you want to create the file, or edit an existing file.
  3. Type in nano followed by the name of the file. .
  4. Start typing your data into the file.
Читайте также:  Wo mic linux driver

How do I give a user sudo access?

  1. Log into the system with a root user or an account with sudo privileges.
  2. Open a terminal window and add a new user with the command: adduser newuser. .
  3. You can replace newuser with any username you wish. .
  4. The system will prompt you to enter additional information about the user.

What is sudo su?

sudo su — The sudo command allows you to run programs as another user, by default the root user. If the user is granted with sudo assess, the su command is invoked as root. Running sudo su — and then typing the user password has the same effect the same as running su — and typing the root password.

How do I know if a user has sudo access?

To know whether a particular user is having sudo access or not, we can use -l and -U options together. For example, If the user has sudo access, it will print the level of sudo access for that particular user. If the user don’t have sudo access, it will print that user is not allowed to run sudo on localhost.

How do you edit a root file?

Then, enable root function at File Expert (More – Settings – File Explorer Settings – tick on Root Explorer). Then mount as read write mode (More – Mount – Mount As Read Write). Enable write permission to all of the file that you want to edit (Hold on the file – File Operations – Permission – tick all on write).

How do I start the GUI editor in Linux?

  1. Right-click a text or php file.
  2. Select «Properties»
  3. Select «Open with» tab.
  4. Choose among the listed/installed text editors.
  5. Click «Set as default»
  6. Click «Close»

How do I open the text editor as root in Ubuntu?

Take great care when editing files as the root user. Using the sudo command, you will need to successfully enter your password before gedit will open. You would then launch gedit using the gedit command. Once you have opened gedit with administrative privileges, gedit will keep those privileges until you close it.

Best Plugins for PyCharm

Pycharm

Here is a rundown on the best plugins you can install for PyCharm:Key Promoter X. . String Manipulation. . Save Actions. . Ace Jump. . Nyan Pr.

Sway - Tiling Window Manager Specially Crafted for Wayland

Sway

What is Sway Wayland?Should I use a tiling window manager?How do you use a sway compositor?Is openbox a tiling window manager?What is Sway Wayland?Swa.

How To Install And Use Shutter Screenshot Tool In Ubuntu 18.04

Shutter

Shutter Screenshot Tool Installation Through GUI The Ubuntu Software utility will open, from where you can search for Shutter by clicking the search b.

Latest news, practical advice, detailed reviews and guides. We have everything about the Linux operating system

Источник

How to edit files in GUI text editors as root?

This time and another I need to edit some files that are supposed to be edited by root only. I would very much prefer to do it in a GUI text editor rather than using command line tools like nano or vi. Yes, it is perfectly possible to do editing using either one of them or the likes, I am merely dissatisfied with this option, because it is inconvenient when compared to working with kate or gedit (personal opinion; it is totally fine if you have the opposite, but I stick to mine). That being said, I would not like to completely compromise security with a nuclear option xhost si:localuser:root . Is there any middle ground solution (like sudo that, unfortunately, have intentionally been crippled when it comes to running GUI stuff)?

Читайте также:  Dhcp server on kali linux

@mikewhatever Did not work. Unable to init server: Could not connect: Connection refused (gedit:1647550): Gtk-WARNING **: 22:26:54.534: cannot open display:

Have you tried sudo gedit /path/to/file ? It’s probably not the greatest way to do it, but it works for me.

@moep Thank for you suggestion. I has tested your script and I can confirm it working, but. I see that it is based on what I called the nuclear option in my original post ( xhost si:localuser:root ). This solution does work, but it poses a serious security risk. And the latter is what I would prefer to avoid, if possible.

2 Answers 2

1. The admin:// URI

In Ubuntu, the official way to edit system files as root with a graphical editor is to use the admin:// URI. For example, to edit /etc/fstab , issue the following command in the run dialog you obtain after pressing Alt + F2 or on the terminal:

In older Ubuntu versions (prior to 20.10), the very first time you do this, you need to supply your user password two times in a row. This has been fixed in 20.10.

Of course, your user needs to belong to the root group in order to edit system files.

2. Using sudoedit

An approach valid for any desktop environment with any editor would be to use sudoedit . Setup the SUDO_EDITOR environment variable to point to the binary of your graphical editor, for example: export SUDO_EDITOR=»/usr/bin/gedit» . Then, you can use the command sudoedit or sudo -e to open a system file in your graphical editor. Much the way the admin:// URI does, this will create a temporary copy, which you edit as a normal user. Once the editor is closed, the modified temporary copy is copied back over the system file.

This can actually be combined in a single command:

env SUDO_EDITOR="/usr/bin/gedit" sudoedit

Thus, the environment is changed only when you explicitly want to use the graphical editor. You create an alias or create a small script to edit system text files in a graphical editor with a single command.

3. Discouraged: using pkexec

These two options do not at any time run your graphical editor as root, which is preferred. Still, it is possible to run a graphical editor as root using pkexec . You can install a PolicyKit file, but you also may run it with some environment variables. For nautilus users, the package nautilus-admin installs a PolicyKit file for gedit and provides a right-click menu option in nautilus to launch a text file in gedit with root permissions. However, these options, where a graphical application is run as root are, according to man pkexec , discouraged:

As a result, pkexec will not allow you to run X11 applications as another user since the $DISPLAY and $XAUTHORITY environment variables are not set. These two variables will be retained if the org.freedesktop.policykit.exec.allow_gui annotation on an action is set to a nonempty value; this is discouraged, though, and should only be used for legacy programs.

Источник

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