How to enable and disable IPv4 in linux system programmatically?

5,089

you could just delete the ipv4 configured addresses with the default ip tools.

Something like:

ip address delete ip.ad.dr.ess dev ethx

or enable it:

ip address add ip.ad.dr.ess dev ethx

you should know what the address is and the interface to which it is assigned, of course, but that is left as an exercise for the OP.

Share:
5,089

Related videos on Youtube

Anon
Author by

Anon

I just know how to get things done.

Updated on September 18, 2022

Comments

  • Anon
    Anon almost 2 years

    I am new to linux programming and based on some configuration parameter I want to disable and enable IPv4 on linux system programatically. The Pseudo-code of the solution would be like:

    if(is_ipv4_enabled)
    {
        // enable IPv4 and IPv6
    }
    else
    {
        // disable IPv4 and enable IPv6
    }
    

    So IPv6 will be enabled in both the cases, but the only thing is to disable/enable IPv4.

    Any help will be appreciated. Thanks in Advance!

    • MohammadReza moeini
      MohammadReza moeini over 4 years
      You can just disable some ipv4 functions with if-cfg command for disable complete ipv4 you need recompile kernel beacuse system use 127.0.0.1 loop back interface
    • Jasen
      Jasen over 4 years
      just becuse 127.0.0.1 is loopback doesn't mean that it's needed. localhost can be ::1 instead.
    • Jasen
      Jasen over 4 years
      do you want to effect the whole system or just the current application?
    • Anon
      Anon over 4 years
      The current application and further, it will be in hand of the user to disable and enable IPv4. So I guess the option to build kernel again will not work here. The configuration needs to be applied on-the-go or maximum after reboot, but not the recompilation
    • Anon
      Anon over 4 years
      @Jansen: The intention is block any IPv4 communication from outside the system. All the internal application communication shall still work (on local unix socket -> but it shall not impacted by blocking the IPv4)