Ubuntu: Pass commands when switching to root


I am switching to root user using sudo -i , but is it possible to pass a command for execution in the same line itself ? Ex: sudo -i -c "mkdir qq"


You can use something like:

$ sudo -i /bin/bash -c 'mkdir qq && bash'    # pwd  /root    # ls -d qq  qq  

first you pass /bin/bash as your shell, mkdir qq as the command you want to be run and && bash to get a shell with root access.


sudo runs commands. Just do:

sudo -i mkdir qq  

You don't need a special option like with su.


As muru says, the typical use of sudo is to run just one command. You don't need to pass the -c flag like you would with most shells or the su command.

However, depending on what you intend the result to be of running mkdir qq, you may want to omit the -i flag as well. Whether you run sudo -i without a command argument to start a shell or with a command argument (sudo -i command...) to run a single command, sudo -i simulates the environment you would get if you were to log in as the target user. When no target user is specified, that user is root, whose home directory is /root. Logging in as a user places you in their home directory, so sudo -i does that, too, though just for the shell being launched or the single command being run.

Thus running sudo -i mkdir qq actually runs mkdir qq as root from /root. Since qq is a relative path, the directory that command creates is actually /root/qq, i.e., a directory called qq inside the /root directory. If that's what you want, great! But if what you want is actually to create a directory called qq wherever you are right now, then you should do one of these things instead:

  • To create a directory there is usually no reason you need root's full login environment. So you can just drop the -i flag and run sudo mkdir qq.
  • In the quite unlikely event you needed root's login environment to create a directory--I cannot think of a reason you would--then you can use the full path to the directory. Although I doubt you'll need that in this case, it's good to know that you use a full path in a command you run with sudo -i to make sure you're referring to the correct file or directory. For example, if your goal were to create qq in /opt, you could run sudo -i mkdir /opt/qq. Again, though, for mkdir this shouldn't be necessary, just drop the -i.
  • If you don't need to run a command as root or any other alternate user, don't. To create a new directory somewhere your user already has write access, you're better off just running mkdir qq without sudo. In particular, if this qq directory is where you are going to install the instant messaging program QQ for use only by you, and not other users on the system, and you are creating it inside your home directory, then it's best not to use sudo when you install or run the program. (I'm not assuming this is for QQ. That's just an example.)

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »