1

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!

Anon
  • 25
  • 2
  • 7
  • 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 – MohammadReza moeini Feb 13 '20 at 09:16
  • just becuse 127.0.0.1 is loopback doesn't mean that it's needed. localhost can be ::1 instead. – Jasen Feb 13 '20 at 09:21
  • do you want to effect the whole system or just the current application? – Jasen Feb 13 '20 at 09: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 '20 at 09: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 '20 at 09:49

2 Answers2

0

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.

John Mahowald
  • 32,050
  • 2
  • 19
  • 34
0

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.

natxo asenjo
  • 5,739
  • 2
  • 26
  • 27