Changing the MAC (Media Access Control) address of NICs (Network Interface Cards)

In some networks, e.g. the one of Helsinki University, computer's have to be registered in order to get connected. Upon registration the computer receives a name and an IP address. Every time it connects to the network, the DHCP server assigns it the same name and IP address. The DHCP server identifies computers based on the MAC address which is unique for every NIC. However, if you know the MAC address of a registered coputer, you can use it to connect to the network faking the MAC address. This is especially easy with Linux computers:
ifconfig eth0 down
macchanger --mac=00:B0:D0:20:A5:D9 eth0
ifconfig eth0 up

If you need to execute this change every time the machine is booting you can put it into a script. Under Ubuntu Trusty Tahr (14.04), the ifconfig lines didn't work for me anymore. It works, however, it by adding the following three lines to the end of file /etc/rc.local (but before the "exit 0" line):
dhclient -r eth0
macchanger -m 00:21:5a:76:44:b0 eth0
dhclient eth0

Unfortunately, on MacOSX this seems to be almost impossible, at least the way how to do so seems to change with every update of the OS. The easy way around is to use a router, since most of these support MAC address cloning.

And with Ubuntu 16.04 and later, the easiest way seems to be to use systemd:
sudo touch /etc/systemd/system/macchange@.service
sudo emacs /etc/systemd/system/macchange@.service

Paste the following content into the file:
[Unit]
Description=changes mac for %I
Wants=network.target
Before=network.target
BindsTo=sys-subsystem-net-devices-%i.device
After=sys-subsystem-net-devices-%i.device

[Service]
Type=oneshot
ExecStart=/usr/bin/macchanger --mac=00:B0:D0:20:A5:D9 %I
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Then enable the service. Remember to replace the enp2s0 with the name of your network interface!
sudo systemctl enable macchange@enp2s0.service