2

I've googled extensively but haven't found how to add a password option so I don't have to type it in manually. I've created the following alias:

alias int='sudo service network-manager restart'

But I don't want to type in my password every time, is there some command I can pass to the alias with my password?

2 Answers 2

6

Echoing the password into the command works but should only be exercised in extreme cases where there's absolutely no security concerns.

However, I believe a better solution would be to make a new rule in /etc/sudoers to allow certain commands (sudo service network-manager restart in this case) to be executed without password. See the answer to this question to understand how to do it.

2
  • So in my case I would do <user> ALL=(ALL:ALL) NPASSWD:path/to/command? What is the actual path to the network-manager restart command? I'm not a savvy user...
    – mind.blank
    Apr 14, 2013 at 11:40
  • Use the command which or whereis (more verbose) to find out the full path of a command (Ex. which service). Anw, this is what you should append to the file: <user> ALL=(ALL:ALL)NOPASSWD:/usr/sbin/service network-manager restart. Note that sudoers does allow fine grain control using regular expression. More here.
    – chin
    Apr 14, 2013 at 11:47
2

This is what I use:

alias nm='echo "abcdefg" | sudo -S service network-manager restart'

where "abcdefg" stands for my actual password. I'm the sole user of my computer and no one else has access to it. So I'm not too concerned about security.

From the sudo man page:

The -S (stdin) option causes sudo to read the password from standard input instead of the terminal device. The password must be followed by a newline character.

2
  • Thanks, that works. Can you please explain how the -S option works?
    – mind.blank
    Apr 14, 2013 at 11:30
  • 1
    @chin also mentioned that you should only use this extreme cases where there's absolutely no security concerns. But to extend on that: If you use it, make sure that the file containing the alias is not readable by other people (chmod it!)
    – Hennes
    Apr 14, 2013 at 11:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .