VLAN Interface creation in Linux using nmcli (script)

Hi there.

I switched from Windows 10 to Fedora Linux (Plasma 5) on my work laptop a couple of months ago and I am quite happy with it so far. There are a few annoying things, like detaching and reattaching the laptop to the docking station causes the external displays to sometimes stay turned off. This does not happen every time but often enough to get frustrating. The only solution I found was to restart the whole system. I don’t know if this is a hardware compatibility related issue or if it’s just the software. Another “problem” I have, is the fingerprint reader. It does not work. Not much to add to that. Other than that, there are a few hiccups here and there. The taskbar sometimes (very rarely) freezes up for 10-30sec for instance. Everything else is working though. So nothing major I would say. The Laptop is a Lenovo T470 by the way. Great system otherwise.

Anyway, that’s not the reason I am writing this. The reason I am writing this is that lately I have been doing a lot of network stuff at work and I had several occasions where I needed a tagged VLAN interface on my laptop. Just to test things or if I wanted to access several networks (without routing) at the same time.

It is very easy to create one with “nmcli” (if you are using NetworkManager for your network). The problem is remembering the whole command. Also, I don’t think you can create multiple interfaces with one command. But let’s not get ahead of ourselves.

Let’s start with the normal way.

Creating VLAN Interfaces with nmcli

This command creates an interface with the VLAN id 20.

fedora-kde :: ~ » nmcli connection add type vlan con-name enp34s0.20 dev enp34s0 id 20 save no Connection 'enp34s0.20' (57b8a0b9-d46c-46ea-8a9f-38dea6bb2c0e) successfully added. fedora-kde :: ~ »  

Let’s take a look at the interfaces.

fedora-kde :: ~ » nmcli connection show
NAME        UUID                                  TYPE      DEVICE     
enp34s0.20  57b8a0b9-d46c-46ea-8a9f-38dea6bb2c0e  vlan      enp34s0.20 
enp34s0     8b05eb10-1bb7-3089-9e1c-84140df50c5d  ethernet  enp34s0    
fedora-kde :: ~ » nmcli device
DEVICE      TYPE      STATE                                  CONNECTION 
enp34s0     ethernet  connected                              enp34s0    
enp34s0.20  vlan      connecting (getting IP configuration)  enp34s0.20

That’s it.

Deleting VLAN Interface with nmcli

This can be used to delete other interface types as well of course.

fedora-kde :: ~ » nmcli connection delete enp34s0.20
Connection 'enp34s0.20' (ea9c2ff9-6f7f-48c4-966d-634b31a485c6) successfully deleted.

(Bonus) Script to create multiple VLAN Interfaces

Now, I don’t know how useful this will be in the long run and if it actually saves any time at all. But I wrote it… Sooo here it is.

This is a tiny script that runs a “for loop” for every VLAN ID you enter. An example.

fedora-kde :: ~ » ./vlan-creation.sh -i enp34s0 -v "20 30 40" -s yes -u no
Connection 'enp34s0.20' (ef84465f-3c64-4969-bf46-baf3d9449956) successfully added.
Connection 'enp34s0.30' (9578e86a-3ad4-4b8d-91bc-8e03aeba4692) successfully added.
Connection 'enp34s0.40' (270d0db0-6af3-4ea0-8b64-0d6649743ec1) successfully added.

This created the VLAN tagged interfaces “20” “30” and “40” with one command.

What if we want to delete only “20” and “40”?

fedora-kde :: ~ » ./vlan-creation.sh -i enp34s0 -v "20 40" -d
Connection 'enp34s0.20' (fa6a75cb-4874-4dd1-8d0e-bbbb0f798e95) successfully deleted.
Connection 'enp34s0.40' (136825ff-1de2-441e-943a-27147e8550ab) successfully deleted.

Also a simple “help”.

fedora-kde :: ~ » ./vlan-creation.sh -h
Usage:
        Create:
        vlan-creation.sh -i INTERFACE-NAME -v "VLANS" (Space separated) -s yes/no -u yes/no
        Delete:
        vlan-creation.sh -i INTERFACE-NAME -v "VLANS" (Space separated) -d
Example:
        vlan-creation.sh -i ens0 -v "20 30" -s no -u yes
Options:
-h              Help
-i              Interface
-v              VLAN IDs
-s              Make configuration persistent yes or no
-u              Disable Interface yes or no
-d              Delete Interface

Keep in mind that this script has no failsafe of any kind. For instance, you could create a second interface with the same name and VLAN id. The script does not check for duplicates.

KDE Plasma 5. using the GUI to create VLAN Interfaces

One more thing. If you are using KDE Plasma 5 and want to use the GUI to create or just set an IP on the VLAN interfaces, you have to select “Show virtual connections” in the “Connections” Settings. Otherwise, the interfaces won’t show up.

Here is the script.

#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

## Variables
VLANS=""
INTERFACE=""
SAVE=""
DISABLE=""


usage () {
        cat <<EOF
Usage:
	Create:
	vlan-creation.sh -i INTERFACE-NAME -v "VLANS" (Space separated) -s yes/no -u yes/no
	Delete:
	vlan-creation.sh -i INTERFACE-NAME -v "VLANS" (Space separated) -d
Example:
	vlan-creation.sh -i ens0 -v "20 30" -s no -u yes
Options:
-h		Help
-i		Interface
-v		VLAN IDs
-s		Make configuration persistent yes or no
-u		Disable Interface yes or no
-d		Delete Interface
EOF
        exit 0
}


OPTSTRING='hi:v:s:u:d'

while getopts ${OPTSTRING} flag; do

	case "${flag}" in
	    h)
            usage
            exit 0
            ;;
		i)
			INTERFACE=$OPTARG
			;;
		v)
			VLANS=($OPTARG)
			;;
        s)
            SAVE=$OPTARG
            ;;
		u)
			DISABLE=$OPTARG
			;;
		d)
			for item in ${VLANS[*]}

				do
					nmcli con del $INTERFACE.$item
				done
			exit
			;;
		:)
      		echo "$0: Must supply an argument to -$OPTARG." >&2
      		exit 1
      		;;
		\?)
			echo "Invalid option: -$OPTARG" <&2
			exit 1
			;;
	esac
done



for item in ${VLANS[*]}

	do
		nmcli con add type vlan con-name $INTERFACE.$item dev $INTERFACE id $item save $SAVE
		if [ $DISABLE = "yes" ]
		then
			nmcli con down $INTERFACE.$item
		fi
	done

Leave a Reply