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:

    // enable IPv4 and IPv6
    // 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!

  • You can just disable some ipv4 functions with if-cfg command for disable complete ipv4 you need recompile kernel beacuse system use loop back interface – mohammad moeini Feb 13 at 9:16
  • just becuse is loopback doesn't mean that it's needed. localhost can be ::1 instead. – Jasen Feb 13 at 9:21
  • do you want to effect the whole system or just the current application? – Jasen Feb 13 at 9:24
  • 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 Feb 13 at 9:37
  • @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) – Anon Feb 13 at 9:49

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.

| improve this answer | |

Integrate with a network configuration scheme compatible with your operating system of choice.

Ideally, one with a well-defined API for doing this disable IPv4 task. For example, NetworkManager has many ways to set ipv4.method to disabled. Some are even user friendly.

Deploying config files is also an option. That tends to require root privilege. And when templating out an entire interface file, not as easy to toggle a single setting, but not touch anything else.

| improve this answer | |

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.