Aaron
Aaron

Reputation: 3325

shell script to auto enter password

So I made a tiny script to change my mac address but it requires me to enter my password to make the switch... what can I add to have it enter it for me?

sudo ifconfig en1 ether 00:11:22:33:44:55

Upvotes: 3

Views: 2258

Answers (3)

Eric
Eric

Reputation: 3172

You don't want to put your root password in an accessible file.

Better would be to make just that command sudoable without password.

Try entering in /etc/sudoers

<your username> ALL=(ALL) NOPASSWD: ifconfig

This says that your user shouldn't need to enter a password for sudo as long as the command is ifconfig.

You should probably read about how the sudoers file works in general first though. Don't know what is you're on, but here's docs for Ubuntu: https://help.ubuntu.com/community/Sudoers

Here are docs for OSX http://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/sudoers.5.html

Upvotes: 0

bchurchill
bchurchill

Reputation: 1420

EDIT: I've realized by now that you can't actually do this unless you wrap these commands in a binary. For example, using the system() function from a C program. I guess that setuid shell scripts are such a bad idea that linux doesn't use them at all :).

While it can be dangerous in some circumstances, you can make your script setuid. To do this, put that command into a file like so:

#!/bin/bash
ifconfig en1 ether 00:11:22:33:44:55

Then set the permissions so that it's owned by root but only you can touch the file:

$sudo chown root file.sh
$sudo chgrp username file.sh
$sudo chmod 070 file.sh

Then make it suid:

$sudo chmod +s file.sh

Now you should be able to execute this file as your user, but have it run with root privileges.

Upvotes: 3

Stephane Rouberol
Stephane Rouberol

Reputation: 4384

you may add your ifconfig command for your id in your /etc/sudoers sudo config file

Upvotes: 4

Related Questions