I'm creating a network testing tool (educational project) at work which is to be used by non technical staff. I have created a simple GUI with buttons for preconfigured IP addresses, of which I can manipulate the static IP address and the subnet mask of the Pi.

The only problem is that I can't find a way to change the default gateway in the same command (or at all using Python).

I'm currently using:

def setIP_XXX():

    os.system('sudo ifconfig eth0 down')

    os.system('sudo ifconfig eth0 192.xxx.xxx.xx/24')

    os.system('sudo ifconfig eth0 up')

Any help in this area would be great as it's starting to become a very stressful experience.

  • Do these commands work if you issue them directly on the command line (i.e. without python)? If not, they are not going to work from python since you are just making a system call here anyway. If yes, then you probably have a path issue where the system call from python does not get the same environment as you get on your command line. (That might be an issue with python itself or with the user / environment that started python via your GUI.)
    – Brick
    Mar 17, 2018 at 14:49

3 Answers 3


If your network doesn't provide you gateway for some reason, then your route table will be empty. Use below command to check it after connecting to a network

$ route -n

If, it's empty then do this

$ route add default gw {gateway-ip} {interface-name}

You can also set netmask along with IP


$ sudo ifconfig {interface-name} 192.168.1.XX netmask

Try out the pyroute2 library that has the functions you are looking for.



Firstly, you do not specify what OS you are using, I assume Raspbian.

Secondly, your script may or may not modify Debian networking settings, but unless you have configured Debian networking (or you are running a VERY old OS) it is unlikely to be.

I suggest you take 2 steps backward, and first figure out HOW to configure networking, BEFORE trying to script it.

See How to set up networking/WiFi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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