configure net namespace on ubuntu 12.04

来源:互联网 发布:淘宝债权转让车被骗 编辑:程序博客网 时间:2024/06/13 06:56
configure net namespace on ubuntu 12.04

1. necessary tools:
VETH device
iptables
linux bridge

2. create a namespace
   ip netns add fib_1

3. create veth device
   ip link add name fib_1-nic type veth peer name fib_1-vnic

4. insert device to fib_1
   ip link set fib_1-vnic netns fib_1

5. enter fib_1 namespace
   ip netns exec fib_1 bash

   modify the name to eth0 and configure ip address, and ping itself
   ip link set fib_1-vnic name eth0
   ip addr add 10.0.0.100/24 dev eth0
   ip link set eth0 up
   ip link set lo up
   root@ubuntu:~# ping 10.0.0.100
   PING 10.0.0.100 (10.0.0.100) 56(84) bytes of data.
   64 bytes from 10.0.0.100: icmp_req=1 ttl=64 time=0.072 ms
   64 bytes from 10.0.0.100: icmp_req=2 ttl=64 time=0.040 ms

6. quit namespace

   exit

7. add a linux bridge, and configure it.

   brctl addbr fibbr
   brctl addif fibbr fib_1-nic
   ip link set fib_1-nic up
   ip addr add 10.0.0.1/24 dev fibbr
   ip link set fibbr up

8. test the connection to namespace
   ping 10.0.0.9
   PING 10.0.0.9 (10.0.0.9) 56(84) bytes of data.
   64 bytes from 10.0.0.9: icmp_req=1 ttl=64 time=0.200 ms
   64 bytes from 10.0.0.9: icmp_req=2 ttl=64 time=0.042 ms

   ip netns exec fib_1 ping 10.0.0.1
   PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
   64 bytes from 10.0.0.1: icmp_req=1 ttl=64 time=0.057 ms
   64 bytes from 10.0.0.1: icmp_req=2 ttl=64 time=0.063 ms

9.let the namespace can access internet
   echo 1 > /proc/sys/net/ipv4/ip_forward
   ifconfig fibbr 128.224.162.253/24 up
   route del -n 128.224.162.0/24 dev fibbr

   ifconfig fibbr add 10.0.0.1/24 up

   ip netns exec fib_1 route add default gw 10.0.0.1


   iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
   iptables -A POSTROUTING -s 10.0.0.0/24 -j SNAT --to-source 128.224.162.253

Then net namespace can access the world.

0 0
原创粉丝点击