Writing a short "C" script to change mac address

Forum rules
Before you post please read this

Writing a short "C" script to change mac address

Postby jessie on Sat Nov 16, 2013 1:12 pm

To connect my linux machine to the internet in my accommodation I have to change its mac address. Which means using roughly the following:

[-> sudo -s]

system("ifconfig eth0 down")
system("ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX")
system("ifconfig eth0 up")

Which only seems to work if I run each one via a separate program :? Otherwise it registers the mac as changing but still no connection. So, I have to root via sudo -s and then run 3 programs after logging in which I call via:


Can this all be done via one program and/or could this all be automated to run at start up?
Level 1
Level 1
Posts: 5
Joined: Sat Nov 09, 2013 11:45 am

Linux Mint is funded by ads and donations.

Re: Writing a short "C" script to change mac address

Postby cwsnyder on Sat Nov 16, 2013 1:52 pm

The default MAC address is built-in to your Network Interface in your machine. My suggestion would be to remove/replace/disable your existing Network Interface with an expansion card Network Interface Card. What you are doing can't be made permanent. If you wish, you could execute as a shell script as follows:
Code: Select all
wait(sudo ifconfig eth0 down)
wait (sudo ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX)
wait(sudo ifconfig eth0)
In this case compiling a C executable would actually be counter-productive. Once you make the shell script, and make it executable with a chmod -x <scriptname>, where <scriptname> is the name for your script, you can add it to your start-up applications, but include the full path when you execute as a start-up, as in /home/<user>/<scriptname> .
LMDE Mate 64-bit, LM16 Mate 64-bit
Debian Xfce 64-bit, Xubuntu 13.10 64-bit, Xubuntu Trusty Tahr 64-bit, Antergos Xfce 64-bit, PCLinuxOS LXDE 64-bit
Level 6
Level 6
Posts: 1039
Joined: Wed Oct 20, 2010 6:49 am
Location: Nappanee, IN, USA

Re: Writing a short "C" script to change mac address

Postby xenopeek on Sun Nov 17, 2013 4:29 am

cwsnyder's suggestion will ask for your password, as ifconfig needs root permissions. So probably better to run this as root.

I suggest you instead make a script like this:
Code: Select all

# redirect stdout and stderr to logfile
exec >>/var/log/mac-address-changer.log 2>&1

# change mac address
ifconfig eth0 down
ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX
ifconfig eth0 up

Make it executable as per cwsnyder's example and then edit root's cron to run this file at startup. To edit root's cron run:
Code: Select all
sudo crontab -e

If you have no default editor, you will be asked which you want to use. Pick nano from the list. At the end of the file add a line like:
Code: Select all
@reboot /path/to/your/script

And replace /path/to/your/script with the full path to your script. This will run the script as root on start up, not needing you to enter your password. I've added a command at the top to log output to a file, which you should always do for scripts running through cron--so you can look at the log file if it doesn't work and see where it went wrong. Log file is /var/log/mac-address-changer.log in my example.

Alternatively, you could put your three commands in /etc/rc.local before the final `exit 0`.
Forum Rules | IRC Channel Rules
Arch Linux / 64-bit / Gnome Shell
User avatar
Level 21
Level 21
Posts: 13678
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 3 guests