You are running as a regular user and need to sudo several commands at once, or you need to use redirection that applies to the commands and not to sudo.
Use sudo to run a subshell in which you may group your commands and use pipe-lines and redirection:
sudo bash -c 'command1 && command2 || command3
'
This requires the ability to run a shell as root. If you can’t, have your system administrator write a quick script and add it to your sudo privilege specification.
If you try something like sudo
command1 && command2|| command3
you’ll find that command2
and
command3
are running as you, not as
root. That’s because sudo’s
influence only extends to the first command and
your shell is doing the redirection.
Note the use of the -c
argument
to bash, which causes it to just execute the given
commands and exit. Without that you will just end up running a new
interactive root shell, which is probably not what you wanted. But as
noted above, with -c
you are still
running a (non-interactive) root shell, so you need to have the
sudo rights to do that. Mac OS X and some Linux distributions, such as Ubuntu, actually disable the root
user to encourage you to only log in as a normal user and
sudo as needed (the Mac hides this better) for
administration. If you are using an OS like that, or have rolled your
own sudo setup, you should be fine. However, if you
are running a locked-down environment, this recipe may not work for
you.
To learn whether you may use sudo and what
you are and are not allowed to do, use sudo
-l
. Almost any other use of sudo will
probably trigger a security message to your administrator tattling on
you. You can try using sudo sudo-V |
less
as a regular user or just sudo
-V | less
if you are already root to get
a lot of information about how sudo is compiled and
configured on your system.
su and sudo
It’s always been a best practice to run as a regular user and only use root privileges when absolutely necessary. While the su command is handy, many argue that sudo is better. For example:
It takes more work to get sudo working
properly (in other words, locked down rather than just "ALL=(ALL) ALL
“) and it can be slightly
less convenient to use, but it can also foster more secure
work-practices.
You can forget that you have su‘d to root and do something unfortunate.
Having to type sudo all the time makes you think about what you are doing a little more.
sudo allows delegation of individual commands to other users without sharing root’s password.
Both commands can incorporate logging, and there are some tricks that can make each command work very much like the other; however, there are still some significant differences. The two most important are that with sudo you enter your own password to confirm your identity before being allowed to execute a command. Thus, root’s password is not shared if more than one person needs some root privileges. Which brings us to the second difference; sudo can be very specific about what commands a given user can and cannot execute. That restriction can be tricky, since many applications allow you to shell out and do something else, so if you are able to sudo into vi, you can shell out and have an unrestricted root prompt. Still, used carefully sudo is an excellent tool.
man su
man sudo
man sudoers
man visudo
sudo
3.128.226.121