Run same command again but as root


Answer #: 1

The simplest way is to run:

sudo !!

This will run the last command but as super user.


Answer #: 2

Answer: you enter sudo !! straight afterwards to repeat the previous command with escalated privileges.

edit: Image found via reddit, and it’s a parody of the original xkcd comic by Randall Munroe.

Answer #: 3

You could try the up-arrow key to scroll through your old commands and rewrite/change them and add the sudo in front of them. (the home button above the arrow keys will set the position to the beginning.)

I dislike the sudo !! idea, because sometimes you add another command in between and don’t remember anymore.

Answer #: 4

I’ll just add an alternative: as short as typing sudo !! and more flexible:

Up-arrow (once, or until you find the command you want to sudo/edit, if it's older) ctrl+A (place cursor at beginning of line) "sudo "...
0 0

Dear nixCraft,

I just want to know how to run Linux commands as another user or as root user?

Confused About Linux commands.

Dear Confused,

You can use the following commands to run as another user or as root user.

#1: runuser command

The runuser command run a shell with substitute user and group IDs. This command is useful only when run as the root user:

Only session PAM hooks are run, and there is no password prompt. If run as a non-root user without privilege to set user ID, the command will fail as the binary is not setuid. As runuser doesn’t run auth and account PAM hooks, it runs with lower overhead than su.

The syntax is:

For example, as a root user you may want to check shell resource limits for oracle user, enter:
# runuser -l oracle -c 'ulimit -SHa'
OR check nginx or lighttpd web server limitations:
# runuser -l nginx -c 'ulimit -SHa'
# runuser -l lighttpd -c...

0 0

Hi all I updated my Module's today and i am getting the errors Below updating UCP Node Server 13.0.8

Is it ok to disable the module or remove it i am not sure what this module is for?
I have Included Debug log Also!

Please wait while module actions are performed Installing ucpnode Installing/Updating Required Libraries. This may take a while...................... Finished updating libraries! There was an error installing. Please review the install log. (/var/www/html/admin/modules/ucpnode/node/logs/install.log) Error(s) installing ucpnode: Failed to run installation scripts Updating Hooks...Done

Here is output from /var/www/html/admin/modules/ucpnode/node/logs/install.log

npm ERR! Linux 2.6.32-504.8.1.el6.x86_64
npm ERR! argv "node" "/usr/bin/npm" "update"
npm ERR! node v0.10.29
npm ERR! npm v2.14.1
npm ERR! path /home/asterisk/.npm/node-xmpp-client/1.0.0-alpha18/package/package.json.a047f00fd6e9d0df60cf8de798fb2f80
npm ERR!...

0 0

I get this error when trying to install. I don't have the permissions?

"npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/learnyounode'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/learnyounode']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/learnyounode',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/learnyounode',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 12.5.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "learnyounode"

0 0

Note: For help with configuring sudo privileges via its configuration file /etc/sudoers, please see Sudoers.

In Linux (and Unix in general), there is a SuperUser named root. The Windows equivalent of root is the Administrators group. The SuperUser can do anything and everything, and thus doing daily work as the SuperUser can be dangerous. You could type a command incorrectly and destroy the system. Ideally, you run as a user that has only the privileges needed for the task at hand. In some cases, this is necessarily root, but most of the time it is a regular user.

By default, the root account password is locked in Ubuntu. This means that you cannot login as root directly or use the su command to become the root user. However, since the root account physically exists it is still possible to run programs with root-level privileges. This is where sudo comes in - it allows authorized users (normally "Administrative" users; for further information please refer to...

0 0
0 0

Using su to Run Commands as Other Users

The su commandcan also be used to run an individual command as another user. This was seen in the startup script for oracle in the last chapter. Below is a partial listing:

case "$1" in
# Start the listener:
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start"
# Start the databases:
su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
# Start Management Server

Here the su command is being used with the -c option. The command in the argument following the -c option is executed with the rights of the specified user, in this case oracle. Since the command contains more than one word, it is put into quotes, but a single-word command could be provided without quotes.

$ su - -c 'shutdown -h now'


It is good practice to use the dash to indicate running...

0 0


I am trying to set up some PHP scripts that will turn on/off a daemon (fatcontroller for some AWS SQS processing) via /etc/init.d/. Turnkey appliances seem to come without suod installed, so I ran a simple apt-get install sudo.

When I just use the php function:

exec('/etc/init.d/fatcontrollerd start');

I get: You need root privileges to run this script

Ok, this makes sense... the Apache www-data user doesn't have permissions to start/stop daemons. So I read about how you can give the password to the sudo command using a pipe (, and I tried this:

exec('echo password | sudo /etc/init.d/fatcontrollerd start');

This doesn't return anything (usually exec returns output of the command)... so I tried running the same command via a terminal window after doing su www-data to see exactly what's happning. This is the output I get:

So... it...

0 0

please try running this command again as root administrator error when trying using -'node.js,less'

I'm trying to install LESS on my machine and have installed node already. However, when I enter "node install -g less" I get the following error and am not sure what to do?

FPaulMAC:bin paul$ npm install -g less
npm ERR! Error: EACCES, unlink '/usr/local/lib/node_modules/less'
npm ERR! { [Error: EACCES, unlink '/usr/local/lib/node_modules/less']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/less' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 13.3.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "less"
npm ERR! cwd /usr/local/bin
npm ERR! node -v v0.10.33
npm ERR! npm -v 1.4.28
npm ERR! path /usr/local/lib/node_modules/less
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack...

0 0

Another starter article to grow with my needs, we’ll start off with the shebang, or bang…or simply “!”. The first two commands I believe are the most useful and could be commonly used. Note that some of these can be a bit difficult to remember, we’ll also go over making aliases so we can use something a little easier to remember in a later article.

Prints the last command executed (does not run it)

This bang command, when entered into the bash shell will run the previously entered command. It the same thing as hitting the up arrow to take you to the previous command and then hitting enter.

>date Monday, June 6, 2011 3:56:20 PM CDT >!! date Monday, June 6, 2011 3:56:22 PM CDT

Whats nice about the !! command though is if you forget to run something as root you can run the same command again, slighly modified:
>mount -a mount: only root can do that >sudo !! (Success)

( being whatever command you want to...

0 0

The sudo command allows users to do tasks on a Red Hat Enterprise Linux system as another user.

sudo is different from the su command and is more flexible and more secure. One significant advantage is that it can log usage. By default the program saves log data in the file /var/log/secure.

The sudo program uses a configuration file /etc/sudoers to store rules that are used to decide whether a command is allowed or not. It is recommended that a program visudo provided with the sudo package be used to edit the /etc/sudoers file.

Assume that we want to be able to run programs as root from a user called normaluser. First lets attempt to use sudo to run a privileged command:

$ sudo /sbin/service sendmail restart Password: normaluser is not in the sudoers file. This incident will be reported.

The sudo command has logged the attempt to the log file /var/log/secure:

# tail /var/log/secure ... Aug 2 14:37:49 somehost sudo: normaluser : user NOT in sudoers ;...
0 0

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.

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

I won't get into how much this is a bad idea; simply put, running sudoin crontab requires your password to be stored somewhere in plaintext.

It's a bad idea.

The following is the preferred method of running administrative tasks through cron. Since you don't really need to write sudo in the crontab, if you are modifying root's crontab.

Use root's crontab

Run the following command:

sudo crontab -e

This opens up root's crontab. sudo is not necessary to run your command in this context, since it'll be invoked as root anyway.

Therefore, you would simply append the following to root's crontab.

@hourly rm somefile

Now, if you absolutely want to be unsafe and take risks with your password, the following will run your command from your own crontab, and enter your password automatically when prompted by sudo.

Again, this is not recommended.

In your own crontab, write your command like so:

@hourly echo "password" | sudo -S...
0 0

have downloaded a file from internet that ends with .bin extension. The documentation (INSTALL.TXT) file says just run bin file. What command I need to type in order to run bin files in Linux or Unix-like operating systems?

A .bin file is a self extracting binary file for Linux and Unix-like operating systems. For example Java or Flash are two examples of these types of file. Just type following two commands to run .bin files. In this example, I am going to run a binary file called



Run .bin file in Linux / UNIX

Change the permission of the file you downloaded to be executable. Type the following command:
$ chmod +x file.bin

Start the installation process or run .bin file. Type the following command:

For example if .bin file name is application.bin. Type the following commands:
$ chmod +x application.bin
$ ./application.bin

Another example for Java Linux self extracting...

0 0

From my mail bag:

I would like to run few commands such as stop or start web server as a root user. How do I allow a normal user to run these commands as root?

You need to use the sudo command which is use to execute a command as another user. It allows a permitted user to execute a command as the superuser or another user, as specified in the /etc/sudoers (config file that defines or list of who can run what) file. The sudo command allows users to do tasks on a Linux system as another user.

sudo command

sudo is more more secure than su command. By default it logs sudo usage, command and arguments in /var/log/secure (Red Hat/Fedora / CentOS Linux) or /var/log/auth.log (Ubuntu / Debian Linux).

If the invoking user is root or if the target user is the same as the invoking user, no password is required. Otherwise, sudo requires that users authenticate themselves with a password by default. Once a user has been authenticated, a timestamp is...

0 0
0 0

Place the script you want to run in the /etc/init.d directory and make the script executable.

chmod 755 myscript

Once that is done create a symbolic link in the run level directory you would like to use, for example if you wanted to run a program in the graphical runlevel 2, the default runlevel for Ubuntu, you would place it in the /etc/rc2.d directory. You just cannot place it the directory, you must signify when it will run by indicating the startup with an “S” and the execution order is important. Place it after everything else that is in the directory by giving it a higher number.

If the last script to be run is rc.local and it is named S99rc.local then you need to add your script as S99myscript.

ln -s /etc/init.d/myscript /etc/rc3.d/S99myscript

Each backward compatible /etc/rc*.d directory has symbolic links to the /etc/init.d/...

0 0

All you have to do is use the runas command to run your program as Administrator (with a caveat).

runas /user:Administrator "cmdName parameters"

In my case, this was

runas /user:Administator "cmd.exe /C %CD%\installer.cmd %CD%"

Note that you must use Quotation marks, else the runas command will gobble up the switch option to cmd.

Also note that the administrative shell (cmd.exe) starts up in the C:\Windows\System32 folder. This isn't what I wanted, but it was easy enough to pass in the current path to my installer, and to reference it using an absolute path.

The caveat to using runas this way is this: The administrative account must be enabled, which is not the default on Windows 7 or Vista. However, here is a great tutorial on how to enable it, in three different ways:

I myself enabled it by opening Administrative Tools, Local Security Policy, then navigating to Local Policies\Security Options and changing the value of the Accounts:...

0 0

I found that the most straight forward thing to do, in order to easily replicate this behavior across multiple servers, was the following:

sudo visudo

Change this line:

# Members of the admin group may gain root privileges %admin ALL=(ALL) ALL

to this line:

# Members of the admin group may gain root privileges %admin ALL=(ALL) NOPASSWD:ALL

And move it under this line:

# Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) ALL

you should now have this:

# This file MUST be edited with the 'visudo' command as root. # # Please consider adding local content in /etc/sudoers.d/ instead of # directly modifying this file. # # See the man page for details on how to write a sudoers file. # Defaults env_reset Defaults mail_badpass Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" # Host alias specification # User alias specification # Cmnd alias specification # User privilege specification root ...
0 0