I need to run a command with administrator privileges Someone said i should run a command as root. What should be done about this?

Best Answer


There are two main possible command line options

  • Use su and enter the root password when prompted.
  • Put sudo in front of the command, and enter your password when prompted.

Running a shell command as root

sudo (preferred when not running a graphical display)

This is the preferred method on most systems including debian ubuntu linux mint even ubuntu Use this method if you do not have a separate root password

Sudo requires you to provide your own password (The purpose is to limit the damage if you leave your keyboard unattended and unlocked, and also to ensure that you really wish to run that command and it wasn't e.g. a typo.) It is often configured to not ask again for a few minutes so you can run several sudo commands in succession.

Example.

sudo service apache restart

If you need to run several commands as root, prefix each of them with sudo . Sometimes, it is more convenient to run an interactive shell as root. You can use sudo -i for that.

$ sudo -i
# command 1
# command 2
...
# exit

Instead of sudo -i , you can use sudo -s . The difference is that -i re i nitializes the environment to sane defaults, whereas -s uses your configuration files for better or for worse.

For more information, see the sudo website , or type man sudo on your system. Sudo is very configurable; for example it can be configured to let a certain user only execute certain commands as root. Read the sudoers man page for more information; use sudo visudo to edit the sudoers file.

su

The su command exists on most unix-like systems. You can run a command as a user provided you know the password of that user When run with no user specified, su will default to the root account.

Example.

su -c 'service apache restart'

The command to run must be passed using the -c option. Note that you need quotes so that the command is not parsed by your shell, but passed intact to the root shell that su runs.

To run multiple commands as root, it is more convenient to start an interactive shell.

$ su
# command 1
# command 2
...
# exit

On some systems, you need to be in group number 0 (called wheel ) to use su . (The point is to limit the damage if the root password is accidentally leaked to someone.)

Logging in as root

If there is a root password set and you are in possession of it, you can simply type root at the login prompt and enter the root password. Be very careful and avoid running complex applications as root as they can do something you didn't intend to Logging into a root account is primarily helpful for emergency situations such as disk failures or when you've locked yourself out of your account

Single User Mode

The single user mode or runlevel 1 also grants you root privileges This is primarily intended for emergency maintenance situations where it is not possible to boot into a multi-user run-level You can boot into single user mode by passing single or emergency on the kernel command line. Note that starting a single-user session is not the same as booting a system normally and logging in as root Rather, the system will only start the services defined for run-level 1. Usually this is the smallest number of services required to have a usable system

You can also get to single user mode by using the telinit command: telinit 1 ; however, this command requires you to already have gotten root privileges via some other method in order to run.

On many systems to boot into single user mode will give the user access to a root shell without prompting for a password Notably, systemd -based systems will prompt you for the root password when you boot this way.

Other programs

Calife

Calife lets you run commands as another user by typing your own password, if authorized. It's similar to the much more widespread sudo seen above Calife is more light-weight than sudo but also less configurable.

Op

Op lets you run commands as another user, including root. This not a full-blown tool to run arbitrary commands: you type op followed by a mnemonic configured by the system administrator to run a specific command.

Super

Super lets you run commands as another user, including root. The command must have been granted by the system administrator

Running a graphical command as root

See also Wikipedia .

PolicyKit (preferred when using GNOME)

Simply prefix your desired command with the command pkexec . Please be aware that while this works in most cases it does not work universally

See man pkexec for more information.

KdeSu, KdeSudo (preferred when using KDE)

kdesu and kdesudo are graphical front-ends to su and sudo respectively. They allow you to run x window programs as root with no hassle. They are part of KDE . Type

kdesu -c 'command --option argument'

and enter the root password, or type

kdesudo -c 'command --option argument'

and enter your password (if authorized to run sudo ). If you check the keep the password option in kdesu you will only have to type the root password once per login session

Other programs

Ktsuss

Ktsuss (“keep the su simple, stupid”) is a graphical version of su.

Beesu

Beesu is a graphical front-end to the su command that has replaced Gksu in Red Hat-based operating systems. It was mainly developed for rhel and fedora

Obsolete methods

gksu and gksudo

gksu and gksudo are graphical front-ends to su and sudo respectively. They allow you to run x window programs as root with no hassle. They are part of Gnome . Type

gksu command --option argument

and enter the root password, or type

gksudo command --option argument

and enter your password (if authorized to run sudo ).

gksu and gksudo are obsolete. They were replaced in gnome by policykit and many distributions such as ubuntu no longer install them by default You shouldn't expect them to be there or to work properly

Manually via one of the shell-based methods

Use one of the methods in the "running a shell command as root section". You will need to ensure that neither the DISPLAY environment variable nor the XAUTHORITY environment get reset during the transition to root. This may require additional configuration of those methods which is out of the scope of this question

This is a bad idea mostly because graphical applications will read and write configuration files as root and when you try to use these applications again as your normal user these applications will not have permission to read their own configurations

Editing a file as root

How can i edit a file from root?