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.

3
  • 1
    Did you try to run your application with root rights? There is no other way in my opinion to change it through C/C++ but use linux/unix headers and libs. Commented Jan 28, 2015 at 9:03
  • mmm, good question. Let me try it. Commented Jan 28, 2015 at 9:03
  • Initially it worked! I will do further testing to make sure. Thanks man! Commented Jan 28, 2015 at 9:08

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Setting default gateway I believe is explained here. Second question - don't know. Comment a post there and ask a guy who proposed that. Good luck.
the code you provided works fine but it adds another route. Can I change the current default gateway not adding a new one ?
Maybe you have to remove default gateway first then? Also, here is another link. Don't shy to google it, it can be easily googled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.