0

On Ubuntu 16.04, the default route for IPv4 is an interface other than eth0.

If I try to

curl -vvv --interface eth0 v4.ifconfig.co

I get

* SO_BINDTODEVICE eth0 failed with errno 1: Operation not permitted; will do regular bind

But if I sudo it works fine.

How do I change the config so that services running under this non privileged user can use eth0 for IPv4? I don't think I can just change sudo config to allow this because I don't want the service to run anything as root. I also want to maintain the default route for IPv4 on the current interface.

Thomas
  • 4,155
  • 5
  • 21
  • 28
Gaia
  • 1,777
  • 4
  • 32
  • 58

2 Answers2

1

See Per-process routing. Assuming the user is foo, the IP address of eth0 is 10.1.1.1, and the router is 10.1.1.254 :

iptables -t mangle -A OUTPUT -m owner --uid-owner foo -j MARK --set-mark 42
iptables -t nat -A POSTROUTING -o eth0 -m mark --mark 42 -j SNAT --to-source 10.1.1.1
ip rule add fwmark 42 table 42
ip route add default via 10.1.1.254 dev eth0 table 42

You'll also need sysctl net.ipv4.conf.eth0.rp_filter=0.

Mark Wagner
  • 17,764
  • 2
  • 30
  • 47
0

Try to use getcap/setcap and check out man 7 capabilities. In your case some NET_CAP_* should do the job.

hargut
  • 3,848
  • 6
  • 10