3bdalla
3bdalla

Reputation: 407

Change network settings in Linux without root privilege

The settings I'm talking about are IP, subnet mask and default gateway (set and get). For setting all of these I use the ioctl function along with options like SIOCSIFADDR and SIOCSIFNETMASK. For getting the IP and mask I use the same approach but with options like SIOCGIFADDR. For getting default gateway I parse the file /proc/net/route and read the corresponding default gateway address for the interface.

This whole thing works perfectly, but with root privilege. Is there any method to change these settings without giving root user ? This is a requirement which I could not resolve. Changing the ownership like this question may solve the problem by putting the code inside a separate executable and change ownership of that executable to root then calling it from the main application.

I just want to know if it is possible to change such settings without giving root access to the application.

Upvotes: 3

Views: 6646

Answers (1)

rasmusm
rasmusm

Reputation: 599

There are two ways i know of:

  1. Use sudo to run ifconfig and route as root. Sudo can be set up to work without password and only for some programs.

  2. Use capabilities see the answer to Is there a way for non-root processes to bind to "privileged" ports on Linux? and use CAP_NET_ADMIN insted of cap_net_bind_service.

Upvotes: 5

Related Questions