2

How to change the IP settings [mainly IP address, subnet mask and default gateway] in Linux using C/C++ ? I've seen examples that uses sockets but there are not much [besides they don't work]. I've tried this question but it does not work.

Whenever I try the code I use ifconfig to see my network configuration but they are still the same. I have openSUSE 13.2 and have both wireless and ethernet available connections.

I think another solution would be is to change the corresponding script file for the interface but I can't find these script files' locations.

Community
  • 1
  • 1
3bdalla
  • 407
  • 1
  • 10
  • 28

1 Answers1

2

In most cases, such kind of operations requires root access. Run your application with root rights and it'll make it work.

VP.
  • 15,509
  • 17
  • 91
  • 161
  • Thanks for the help. I have two questions; Number one is how to set the default gateway ? And number two in the question I mentioned why there is a suggestion to use 'struct sockaddr_in*' instead of 'struct ifreq' ? I mean the second one worls for me. – 3bdalla Jan 28 '15 at 09:16
  • 1
    Setting default gateway I believe is explained [here](http://www.unix.com/programming/142516-set-host-default-gateway-c-program.html). Second question - don't know. Comment a post there and ask a guy who proposed that. Good luck. – VP. Jan 28 '15 at 09:34
  • the code you provided works fine but it adds another route. Can I change the current default gateway not adding a new one ? – 3bdalla Jan 28 '15 at 10:08
  • Maybe you have to remove default gateway first then? Also, here is another [link](http://www.linuxquestions.org/questions/programming-9/problem-to-set-gateway-using-c-program-846692/). Don't shy to google it, it can be easily googled. – VP. Jan 28 '15 at 10:16