1. Overview
The Maximum Transmission Unit (MTU) is the maximum packet size that we can send over a network without fragmentation. The default MTU size for Ethernet is 1500 bytes.
Sometimes, we may want to change the MTU size. For example, we can use jumbo frames having an MTU size of 9000 bytes on a VLAN (Virtual Local Area Network) to increase performance.
In this tutorial, we’ll discuss how to set the MTU size of a network interface permanently. We’ll see that changing the MTU size depends upon whether we use static or dynamic IP addressing.
2. Changing the MTU Size Using ip
We can change the MTU size of a network interface using the ip command. First, let’s check the MTU sizes of the network interfaces we’re using:
$ ip a | grep mtu
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
The ip a command shows the IP addresses of all the network interfaces. We filter the output using grep mtu to show only the lines containing the MTU sizes.
Our host contains two network interfaces. The first is the loopback interface, lo, whose MTU size is 65536 bytes. On the other hand, the second interface, enp0s3, has an MTU size of 1500 bytes.
Let’s increase the MTU size of the enp0s3 interface to 9000 using the ip command:
$ sudo ip link set dev enp0s3 mtu 9000
Changing the MTU size requires root privileges, so we use the ip command together with the sudo command. The ip link set command changes the attributes of a network device. The name of the device — the network interface card — is specified using dev enp0s3. Finally, we specify the new size of the MTU using mtu 9000.
Let’s check the MTU size of enp0s3 again:
$ ip a | grep mtu | grep enp0s3
2: enp0s3: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9000 qdisc fq_codel state DOWN group default qlen 1000
As is apparent from the output, we’re successful in changing the MTU size of the enp0s3 interface.
We can also change the MTU size using the older Linux command ifconfig:
$ sudo ifconfig enp0s3 mtu 9000 up
Although changing MTU sizes using ip link set or ifconfig is easy, we lose the updates when we restart the host. Therefore, in our case, the MTU size retains its default value of 1500 for the enp0s3 interface after a reboot.
3. Changing the MTU Size Permanently
What if we want to change the MTU size permanently? The solution depends on whether we use static or dynamic IP addressing.
3.1. Static IP Addressing
When we assign a static IP address to a host, the IP address doesn’t change over time unless we change it explicitly. We can configure the attributes of a network interface by editing the corresponding interface configuration (ifcfg) file.
The ifcfg file for the enp0s3 interface is /etc/sysconfig/network-scripts/ifcfg-enp0s3 on our host. The directory containing ifcfg files may vary across different Linux distros. For example, the directory is /etc/sysconfig/network-scripts in RHEL while it’s /etc/network/interfaces in Ubuntu.
Let’s check whether /etc/sysconfig/network-scripts/ifcfg-enp0s3 contains an MTU entry using grep:
$ grep MTU /etc/sysconfig/network-scripts/ifcfg-enp0s3
Notably, it doesn’t contain any MTU entry. Therefore, the enp0s3 interface uses the default MTU size of 1500. Let’s change the MTU size of this interface to 9000 by appending MTU=9000 to the end of the ifcfg file using an editor like vi. Now, we have the MTU size set to 9000:
$ grep MTU /etc/sysconfig/network-scripts/ifcfg-enp0s3
MTU=9000
Let’s check the MTU sizes after rebooting the host:
$ ip a | grep mtu | grep enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9000 qdisc fq_codel state UP group default qlen 1000
As we see from the output, the MTU size of the enp0s3 interface is 9000 after the reboot. Therefore, we’re successful in setting the MTU size of the interface permanently.
3.2. Dynamic IP Addressing
A dynamic IP address is assigned to a host as it joins a network. The host gets its dynamic IP configuration from a server. DHCP (Dynamic Host Configuration Protocol) is the most widespread protocol used for dynamically assigning IP addresses to hosts in a network.
A DHCP client requests the dynamic IP address and other configuration parameters like the MTU size from the DHCP server. dhclient is the default DHCP client. It reads the configuration information from the /etc/dhcp/dhclient.conf file.
We can override the MTU size provided by the DHCP server by adding a single statement to /etc/dhcp/dhclient.conf:
interface "enp0s3" {
supersede interface-mtu 9000;
}
The supersede interface-mtu 9**000 statement overrides the MTU size provided by the DHCP server and sets it to 9000. We use the supersede statement if we want the client to use a locally configured value rather than the one provided by the server. The interface “enp0s3**“ statement, on the other hand, specifies that this configuration is only for the enp0s3 interface.
Having modified /etc/dhcp/dhclient.conf, we can apply the MTU size change for enp0s3 immediately using dhclient:
$ sudo dhclient enp0s3
$ ip a | grep mtu | grep enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9000 qdisc fq_codel state UP group default qlen 1000
We must run dhclient automatically at reboot to make the changes persistent. Therefore, one way of making the changes persistent is by adding the dhclient enp0s3 command to /etc/rc.d/rc.local using an editor like vi. Modifying /etc/rc.d/rc.local requires root privileges.
Then, we must give execution permission to /etc/rc.d/rc.local using chmod and enable the rc-local service using systemctl:
$ sudo chmod 744 /etc/rc.d/rc.local
$ sudo systemctl enable rc-local >& /dev/null
After applying these changes, dhclient automatically runs when the host is rebooted and applies the MTU size specified in /etc/dhcp/dhclient.conf.
4. Conclusion
In this article, we discussed how to set the MTU size of a network interface permanently. First, we saw that changing the MTU size of a network interface using the ip command isn’t persistent.
Then, we learned that we can use the ifcfg files to update MTU sizes in case of static IP addressing. On the other hand, we must update the dhclient.conf file if we use DHCP for assigning dynamic IP addresses. Additionally, we must run dhclient while starting a host to apply the changes in dhclient.conf.