Generate a uuid for an Ubuntu network interface

Introduction

In short

MYUID=$(uuidgen enp0s8)
sed -i s/UUID=*/UUID=$MYUID/ /etc/sysconfig/network-scripts/ifcfg-enp0s8
unset MYUID

Explanation

uuidgen device_name

For example

uuidgen enp0s8

odc11c41-d526-4998-90c4-8b7afcbd70ae

Now this uuid can be entered into /etc/sysconfig/network-scripts/ifcfg-enp0s8

This file can be viewed with cat or vim

vi /etc/sysconfig/network-scripts/ifcfg-enp0s8

TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=dhcp DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=enp0s8 UUID=bddba352-22bc-4210-b460-404bd3043c6e DEVICE=enp0s8 ONBOOT=yes

Now the old UUID needs to be replaced with the new one

sed s/UUID=*/UUID=odc11c41-d526-4998-90c4-8b7afcbd70ae/ /etc/sysconfig/network-scripts/ifcfg-enp0s8

Optimization

To avoid copying the uuid by hand, you can save the output uuidgen into a variable and use it in the sed command.

MYUID=$(uuidgen enp0s8)
sed -i s/UUID=*/UUID=$MYUID/ /etc/sysconfig/network-scripts/ifcfg-enp0s8

After that it is advisable to clear the variable with unset

unset MYUID

Article author: Andrei Olegovich