10

I am writing a simple shell script which changes the mac address of the network hardware. One of the line is :

sudo ifconfig eth0 hw ether 00:99:99:00:00:00

My problem is with sudo the script prompts for password. Is there any way that I could do this without prompting the user for password ???

5 Answers 5

8

Most definitely, if you don't mind making that particular command 'free for use' for that particular user:

See basically my other answer here: Shell script - Sudo-permissions lost over time

The simplest thing that may work is

myuser = NOPASSWD: /sbin/ifconfig

Also, you could sudo an arbitrary command on the same terminal (tty/vty), and sudo will cache the authentication for a while (or until sudo -k), so you may start the script and it will 'remember' the credentials from your earlier sudo invocation. I sometimes do this when composing pipes with sudo (just preceded them with sudo true)

4
  • Did you mean something like username ALL=NOPASSWD: /sbin/ifconfig in the sudoers file ??? I did try it but its not working
    – ro ko
    Nov 13, 2011 at 13:52
  • 1
    Yes. Did you (1) include the spaces (2) use sudoedit (or visudo)? Also, I fully DON'T recommend using ALL for password less grants. WARNING Anything else but the 'blessed' tools (like visudo) WILL render your system unusable when you mess up the sudoers file (unless you have the root password to fix things again)
    – sehe
    Nov 13, 2011 at 14:05
  • @sehe I agree, using ALL is pretty dangerous.
    – Tilo
    Nov 13, 2011 at 14:08
  • If you don't want sudo to ask for a password but print an error instead use -n.
    – gitaarik
    Mar 24, 2017 at 14:43
3
echo "password" | sudo -S ifconfig eth0 hw ether 00:99:99:00:00:00
1
  • While this will work, it is incredibly insecure. First of all, they can read the password from the script that contains the command. Second, the password is going to be stored in places like shell histories. As a general rule, never put a password on a command line. Let the program prompt for it. For your specific use case, the best option is to edit the shudders file to not require a password when executing the ifconfig command.
    – W3t Tr3y
    May 18, 2014 at 2:04
2

You need a sudo configuration line which allows for the command to be executed by the user without password prompt.

You can disable the password prompt for a whole user (more dangerous, but perhaps ok if you are the only user on your desktop -- DONT do this on a server ):

yourusername  ALL=(ALL) NOPASSWD: ALL

or more restrictive, only allowing the ifconfig command:

yourusername  ALL= NOPASSWD: /sbin/ifconfig

See: man sudoers , man sudo

http://ubuntuforums.org/showthread.php?t=1132821

http://www.linuxhelp.net/guides/sudo/

8
  • I tried it, and I seem to have got stuck with new error sudo: parse error in /etc/sudoers near line 18 sudo: no valid sudoers sources found, quitting while trying to edit the sudoers file. how do I do it now ???
    – ro ko
    Nov 13, 2011 at 13:57
  • which means that you have a syntax error in line 18 of your /etc/sudoers file -- you need to fix that or comment it out
    – Tilo
    Nov 13, 2011 at 13:59
  • can you add the line from your /etc/sudoers file to your question? Also: check the link I mentioned in the Ubuntu forums
    – Tilo
    Nov 13, 2011 at 14:00
  • I know that I have an error and I know I have to fix it I already mentioned it, problem is without sudo access you cannot edit sudoers file and when I type sudo I get that error. Though I do know what is in line 18 in /etc/sudoers : myname ALL= NOPASSWD: ifconfig
    – ro ko
    Nov 13, 2011 at 14:07
  • make sure there is no space in front of the username
    – Tilo
    Nov 13, 2011 at 14:09
1

A safer way to do it would be:

sudo visudo -f sudoers

then add

myuser ALL=NOPASSWD:/sbin/ifconfig

to the editor window that appeared. Once you are done, use :x to quit

0

Here is a Zenity dialog that does something similar to Tman's comment,
though the password isn't stored in history... This may be a good alternative

#!/bin/bash
ENTRY=`zenity --password`
case $? in
0)
pw=$(echo $ENTRY | cut -d'|' -f1)
;;
1)
echo "Stop login.";;
-1)
echo "An unexpected error has occurred.";;
esac
TMP=$(echo "${pw}" | sudo -Sv)
TMP=0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.