Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 310
Aaron Avatar asked Apr 30 '26 15:04

Aaron


2 Answers

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

like image 161
Stephane Rouberol Avatar answered May 02 '26 04:05

Stephane Rouberol


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.

like image 24
bchurchill Avatar answered May 02 '26 04:05

bchurchill