Run command as root user linux

How do I run a command as the system administrator (root)

I need to run a command with administrative privileges. Someone said I should run a command as root. How do I do this?

4 Answers 4

The main two commandline possibilities are:

  • 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 Ubuntu, Linux Mint, (arguably) Debian, and others. If you don’t know a separate root password, use this method.

Sudo requires that you type 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.

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 reinitializes 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. It lets you run a command as another user, provided you know that user’s password. When run with no user specified, su will default to the root account.

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 might do something you didn’t intend. Logging in directly as root is mainly useful in emergency situations, such as disk failures or when you’ve locked yourself out of your account.

Читайте также:  Утилита linux разрешение экрана

Single User Mode

Single user mode, or run-level 1, also gives you root privileges. This is intended primarily for emergency maintenance situations where booting into a multi-user run-level is not possible. You can boot into single user mode by passing single or emergency on the kernel command line. Note that booting into single-user mode is not the same as booting the system normally and logging in as root. Rather, the system will only start the services defined for run-level 1. Typically, 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 booting 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 is similar to the much more widespread sudo (see 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 allowed by the system administrator.

Running a graphical command as root

PolicyKit (preferred when using GNOME)

Simply prefix your desired command with the command pkexec . 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 password” option in KdeSu, you will only have to type the root password once per login session.

Other programs

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

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

Obsolete methods

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 have been replaced by PolicyKit in GNOME, and many distributions (such as Ubuntu) no longer install them by default. You should not depend on them being available or working properly.

Читайте также:  Узнать серийный номер ноутбука linux

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 that is outside the scope of this question.

Overall, this is a bad idea, mostly because graphical applications will read and write configuration files as root, and when you try to use those applications again as your normal user, those applications won’t have permission to read their own configurations.

Editing a file as root

It should be noted that sudo will only work if (1) it’s installed and (2) if your use is in the sudoers file and it allowed to perform the operation.

In either case, you will be prompted for the root password. For more information, see the manual page.

Since the question was not Linux specific, here’s how you achieve the same goal in Solaris 9+ (or Trusted Solaris 8):

Solaris, since version 9, has included a suite of tools affectionately referred to as RBAC, or Role Based Access Control.

The gist of RBAC is that through the granting of Authorizations and Rights, to Users and/or Role, or the granting of Roles to Users, you can create incredibly fine grained models for who can run what with which privileges.

Essentially, you identify authorization in /etc/security/auth_attr, then grant them to users or roles in /etc/user_attr.

You define profiles in /etc/security/prof_attr. You then associate commands with those profiles in /etc/security/exec_attr, followed by assigning those profiles to users in the /etc/user_attr file.

Once those things are done, you actually run pfexec to execute the command with privileged or authorizations that are granted to that user for that command.

The nice thing about RBAC is that there are no additional privileges granted to the command itself, or the user, only to the combination of user + command. So it’s safer than making a binary +s, or just using sudo to make a user be able to execute pretty much anything. (I know that you can lock down sudo, but in my experience most people don’t)

Another advantage of RBAC is that you can make root a role account, and assign that role to users who are able to become root with the ‘su’ command and the root password. The root user will also be able to log in in Single User Mode, which is better (in my opinion) than the Linux model where you can disable the root password passwd -d root , or lock the root account passwd -l root , both of which make logging in as root quite hard when something goes wrong.

Ben Rockwood has a great blog post on RBAC that can be read at Using RBAC on (Open)Solaris.

Источник

how to run command in bash as root? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

#!/bin/bash FILES="/var/www/html/crm/cicrm/cache/ /var/www/html/crm/cicrm/data/ /var/www/html/crm/cicrm/logs/ /var/www/html/crmtest/cicrm/cache/ /var/www/html/crmtest/cicrm/data/ /var/www/html/crmtest/cicrm/logs/" for f in $FILES do echo "processing $f" 1>&2 if [[ ! -e $f ]]; then mkdir $dir elif [[ ! -d $f ]]; then echo "$f already exists but is not a directory" 1>&2 fi done chown -R manager:phpdev /var/www/html/crm/ chown -R manager:phpdev /var/www/html/crmtest/ chown -R manager:phpdev /var/redmine/crmGit/ chown -R manager:phpdev /var/redmine/owncloudGit/ chmod -R 770 /var/redmine/crmGit/ chmod -R 770 /var/redmine/owncloudGit/ chmod -R 770 /var/www/html/crm/ chmod -R 777 /var/www/html/crm/cicrm/cache/ chmod -R 777 /var/www/html/crm/cicrm/data/ chmod -R 777 /var/www/html/crm/cicrm/logs/ chmod -R 770 /var/www/html/crmtest/ chmod -R 777 /var/www/html/crmtest/cicrm/cache/ chmod -R 777 /var/www/html/crmtest/cicrm/data/ chmod -R 777 /var/www/html/crmtest/cicrm/logs/ 

and the whole dirs is only owned by me : manager:phpdev usually when my developers update all files with git, the permission is changing to developer:developer, how my user (developer:developer) can run my script ?

Читайте также:  Create python service linux

Источник

how to execute a command as root

I develop a C code on Linux (Debian). Time to time, I need to execute some commands through system() I wonder if it is possible to execute a command via system() as root. If it is not the case, is there any function to execute a command (or run a binary) as root that I can use on the C code?

What’s the point of distinguishing different users if any user can execute code as root without additional authorization

3 Answers 3

We met the situation before that we want to execute a root command by a normal user, here is our solution (using setuid/SUID):

Step 1: Write a C code tool: my_sudo.c

. int main(int args, char *argv[]) < if (args < 2) printf("Usage: my_sudo [cmd] [arg1 arg2 . ]"); // cmd here is the shell cmd that you want execute in "my_pro" // you can check the shell cmd privilege here // example: if (argv[1] != "yum") return; we just allow yum execute here char cmd[MAX_CMD]; int i; for ( i = 2; i < args; i ++) < // concatenate the cmd, example: "yum install xxxxx" strcat(cmd, " "); strcat(cmd, argv[i]); >system(cmd); > 

Step 2: Compile my_sudo.c to get a my_sudo executable file

 sudo chown root:gTom my_sudo // user root && gTom group sudo chmod 4550 my_sudo // use SUID to get root privilege #you will see my_sudo like this(ls -l) #-r-sr-x--- 1 root my_sudo 9028 Jul 19 10:09 my_sudo* #assume we put my_sudo to /usr/sbin/my_sudo 

Step 3: In your C code

. int main() < . system("/usr/bin/mysudo yum install xxxxx"); . >#gcc && ls -l #-rwxr--r-- 1 Tom gTom 1895797 Jul 23 13:55 my_pro 

Step 4: Execute ./my_pro

You can execute the yum install without sudo .

⚠ Ouch! you’re concatenating the arguments as a shell command line! Then the user can do mysudo ‘arg;sh’ to get a root shell for instance. And you’re not sanitizing the environment like sudo would.

If you are a user on your system that has sudo privileges to run commands as root , just pre-pend sudo to the command.

system("sudo yum install some-package"); 

If you want anybody to be able to do it, then you have to be administrator on your system, change the owner of the file to be root , and modify the permissions of your executable to run as root . By doing so, you do not need to modify your system() command string with sudo .

chmod +s my_program chown root my_program 

Realize that doing this may open you up to security problems, unless you have proven that your program has no security issues.

The file-system may be such to disallow you from setting the setuid bit on your program. If you need more information along these lines, you should consult SuperUser.

Источник

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