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 ???

Owen Blacker
  • 4,117
  • 2
  • 33
  • 70
ro ko
  • 2,906
  • 3
  • 37
  • 58

5 Answers5

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)

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 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 '11 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 '11 at 14:05
  • @sehe I agree, using ALL is pretty dangerous. – Tilo Nov 13 '11 at 14:08
  • If you don't want `sudo` to ask for a password but print an error instead use `-n`. – gitaarik Mar 24 '17 at 14:43
3
echo "password" | sudo -S ifconfig eth0 hw ether 00:99:99:00:00:00
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Tman
  • 59
  • 3
  • 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 '14 at 02: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/

Tilo
  • 33,354
  • 5
  • 79
  • 106
  • 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 '11 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 '11 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 '11 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 '11 at 14:07
  • make sure there is no space in front of the username – Tilo Nov 13 '11 at 14:09
  • Thanks for your answers but I got one last and silly question how do I edit sudoers file without using sudo (since I cannot use it) and I forgot my root passwd as well – ro ko Nov 13 '11 at 14:14
  • ugh! you just locked yourself out of the system... you need to become root via `su - ` and then type the root password .. then edit the sudoers file – Tilo Nov 13 '11 at 14:14
  • You had to add a tab character in front of yourusername. ie, yourusernameALL....(continue). Not doing so would have contributed to the syntax error – Shrikrishna Holla Mar 20 '14 at 13:28
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

Abandoned Cart
  • 4,512
  • 1
  • 34
  • 41
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
Majora320
  • 1,321
  • 1
  • 13
  • 33
I-Jo
  • 101