Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

NOTE: This is for RHEL/centOS.

Currently my way of configuring the MTU is via GUI using system-config-network and setting the MTU to 4096. I would like a non-interactive script to do this instead.

How do you configure the MTU for all network adapters via a script?

UPDATE:

Changing the MTU for a specific network adapter using system-config-network will actually result in making a persistent change to the adapter's configuration. I need the script to also make changes that are persistent.

share|improve this question
1  
I was thinking to myself: 1. list all the /etc/sysconfig/network-scripts/ifcfg-eth* files 2. iterate over the list 3. add a new line for each config file MTU=4096 4. optionally do a service network restart –  Trevor Boyd Smith Oct 8 '12 at 15:57
    
The thing that is not clear to me is why you need a script for this - you need to do this on a limited number of interfaces just once. A script would be needed if you want to roll out dozens of servers with that setting or if you want to change the setting forward and backward (but there - why change it permanently any way?) –  Nils Oct 9 '12 at 21:03
    
Because it's going to be part of an installer script that will be used on many machines. –  Trevor Boyd Smith Oct 10 '12 at 16:26
    
ok - in that case - use Craig`s answer. Unless you know the name of the according interfaces will always be the same on all target machines. In that case there is a kickstart-option for setting the mtu. –  Nils Oct 11 '12 at 20:41
add comment

2 Answers

You can't set it globally for all devices in one command. You can examine the available devices and set them individually.

You can use the ip tool from the iproute package; its syntax is much more regular and expressive than that of the ifconfig tool. Example command line to set

ip link set mtu 4096 dev eth0

You can find available devices with

ip link list

and examine individual ones

ip link show dev eth0

That should be enough for you to get started.

share|improve this answer
    
does ip* set mtu 4096* require a service network restart for changes to take effect? –  Trevor Boyd Smith Oct 8 '12 at 16:00
1  
@TrevorBoydSmith no, it directly manipulates the interface settings. It does not persist the setting, e.g. after a reboot your system would have the old values. –  Ulrich Dangel Oct 8 '12 at 16:20
    
service network restart will (among other things) bring down all the interfaces, read the system configuration files and then bring up all the interfaces (using command lines similar to mine, above) with the settings there. If you want to make persistent changes, your script should insert the right values into those config files. –  itsbruce Oct 8 '12 at 16:43
    
@itsbruce, ah thanks for the clarification regarding your changes being non-persistent. I updated and further clarified the "persistent" aspect of the question. –  Trevor Boyd Smith Oct 8 '12 at 18:01
add comment

The following sed script first comments out any existing MTU setting, and then appends a new MTU setting of 4096 as the last line of the cfg file for each NIC.

sed -i -e 's/^MTU=/#&/' -e '$aMTU=4096' /etc/sysconfig/network-scripts/ifcfg-eth*

This makes the persistent changes to the config files, but does not activate the changes.

You'll need to run service network restart to activate the change...or if you just want to change the MTU without restarting the network services, something like the following will do the job:

awk -F: '/eth/ {print $1}' /proc/net/dev | xargs -r -i{} ifconfig {} mtu 4096

or using ip rather than ifconfig, as in itsbruce's answer:

ip link list | awk -F: '/eth[0-9]/ {print $2}' | xargs -r -n 1 ip link set mtu 4096 dev
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.