| 1 | Advanced Network setting for Linux[[BR]] |
| 2 | Contributed by Jean-Jacques Sarton, 2007/03/21 |
| 3 | |
| 4 | The virtual machine may be fully integrated into the network and you may have access between all virtual machine and the host if you do a little bit more. |
| 5 | |
| 6 | A bridge can contain only one physical/virtual device. So you can create your bridge as follow: |
| 7 | |
| 8 | {{{ |
| 9 | #!/bin/sh |
| 10 | # set PATH for the case we are called via sudo or su root |
| 11 | |
| 12 | PATH=/sbin:/usr/bin:/bin:/usr/bin |
| 13 | |
| 14 | # create a tap |
| 15 | tunctl -t tap1 -u <user> |
| 16 | ip link set up dev tap1 |
| 17 | |
| 18 | # create the bridge |
| 19 | brctl addbr br0 |
| 20 | brctl addif br0 tap1 |
| 21 | |
| 22 | # set the IP address and routing |
| 23 | ip link set up dev br0 |
| 24 | ip addr add 10.1.1.1/24 dev br0 |
| 25 | ip route add 10.1.1.0/24 dev br0 |
| 26 | }}} |
| 27 | |
| 28 | With this code you will be able to contact the virtual machine attached to the Host Interface tap1 from the host and the host from the virtual machine. |
| 29 | The IP adress should not conflict with the main IP address of your PC which will probably been within the range 192.168.0.0/16. |
| 30 | |
| 31 | With these settings we will not have an access to the external world from the virtual machine. How to do this will be explained later. |
| 32 | |
| 33 | If we plan to use more as one virtual machine we can add further tap devices to the bridge. The script can be modified as follow: |
| 34 | |
| 35 | {{{ |
| 36 | #!/bin/sh |
| 37 | # set PATH for the case we are called via sudo or su root |
| 38 | |
| 39 | PATH=/sbin:/usr/bin:/bin:/usr/bin |
| 40 | USER=<name of the vm user> |
| 41 | |
| 42 | NUMBER_OF_VM |
| 43 | # create the bridge |
| 44 | brctl addbr br0 |
| 45 | |
| 46 | # create the taps and insert them into the bridge |
| 47 | |
| 48 | NB=1 |
| 49 | while [ $NB -lt $NUMBER_OF_VM |
| 50 | do |
| 51 | tunctl -t tap$NB -u $USER |
| 52 | ip link set up dev tap$NB |
| 53 | brctl addif br0 tap$NB |
| 54 | let NB=$NB+1 |
| 55 | done |
| 56 | |
| 57 | # set the IP address and routing |
| 58 | ip link set up dev br0 |
| 59 | ip addr add 10.1.1.1/24 dev br0 |
| 60 | ip route add 10.1.1.0/24 dev br0 |
| 61 | }}} |
| 62 | |
| 63 | Now we will be able to start the virtual machines 1 to n, the virtual machine |
| 64 | are to be attached to tap1, tap2, ...tapn. |
| 65 | |
| 66 | Settings within the virtual machines. You may use the tools provided by the guest system in order to configure the device used for the network connection or set the ip address manually or via a script. On linux the commands which are to be called manually are: |
| 67 | |
| 68 | {{{ |
| 69 | ip link set up dev eth0 |
| 70 | ip addr add 10.1.1.2/24 dev eth0 |
| 71 | ip route add default via 10.1.1.1 dev eth0 |
| 72 | }}} |
| 73 | |
| 74 | You must also edit the file /etc/resolv.conf in order to be able to resolve network names such as www.virtualbox.org or local names. The content of this file can be the same as for the resolv.conf file on your computer. |
| 75 | |
| 76 | You may also assign the address via DHCP, in this case the dhcpd daemon must work on the host machine. |
| 77 | A simple configuration shall look as follow: |
| 78 | |
| 79 | {{{ |
| 80 | ddns-update-style interim; |
| 81 | ignore client-updates; |
| 82 | |
| 83 | subnet 10.1.1.0 netmask 255.255.255.0 { |
| 84 | |
| 85 | # --- default gateway |
| 86 | option routers 10.1.1.1; |
| 87 | option subnet-mask 255.255.255.0; |
| 88 | |
| 89 | option domain-name "domain.org"; |
| 90 | option domain-name-servers 10.1.1.1; |
| 91 | |
| 92 | # option ntp-servers 10.1.1.1; |
| 93 | |
| 94 | range dynamic-bootp 10.1.1.2 10.1.1.254; |
| 95 | default-lease-time 21600; |
| 96 | max-lease-time 43200; |
| 97 | } |
| 98 | }}} |
| 99 | |
| 100 | If you want to use zeroConf rendez-vous/Bonjour for automatic setting of the IP address for the guests you shout use an addres in the range 169.254.0.0/16 for the host eg 169.254.0.1. |
| 101 | |
| 102 | Connecting the internal network to the world. |
| 103 | |
| 104 | With the above scenario we don't have access to the wide world and will not |
| 105 | ne able to update a guest system or download anythings. In order ot get this working we must configure the main system so that it do NAT. |
| 106 | |
| 107 | The simplest way should be to insert the interface used for the connection to the internet and using the dhcp server provided by the router (if you are attached to the internet via a DSL router). I we do so, all systems can reach the web and you may surf or download files within your virtual machine. |
| 108 | |
| 109 | If you want that the machine are not normally connected to the wide world |
| 110 | you can set you host machine (and unset it) for a temporary connection |
| 111 | to the world. |
| 112 | |
| 113 | Setting NAT can be do with the following code |
| 114 | |
| 115 | {{{ |
| 116 | INTIF="br0" |
| 117 | EXTIF="eth0" |
| 118 | echo 1 > /proc/sys/net/ipv4/ip_forward |
| 119 | |
| 120 | # clear existing iptable rules, set a default policy |
| 121 | iptables -P INPUT ACCEPT |
| 122 | iptables -F INPUT |
| 123 | iptables -P OUTPUT ACCEPT |
| 124 | iptables -F OUTPUT |
| 125 | iptables -P FORWARD DROP |
| 126 | iptables -F FORWARD |
| 127 | iptables -t nat -F |
| 128 | |
| 129 | # set forwarding and nat rules |
| 130 | iptables -A FORWARD -i $EXTIF -o $INTIF -j ACCEPT |
| 131 | iptables -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT |
| 132 | iptables -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE |
| 133 | |
| 134 | Setting will be do with |
| 135 | |
| 136 | # clear existing iptable rules, set a default policy |
| 137 | iptables -P INPUT ACCEPT |
| 138 | iptables -F INPUT |
| 139 | iptables -P OUTPUT ACCEPT |
| 140 | iptables -F OUTPUT |
| 141 | iptables -P FORWARD DROP |
| 142 | iptables -F FORWARD |
| 143 | iptables -t nat -F |
| 144 | |
| 145 | # disable forwarding |
| 146 | echo 0 > /proc/sys/net/ipv4/ip_forward |
| 147 | echo 1 > /proc/sys/net/ipv4/ip_dynaddr |
| 148 | }}} |
| 149 | |
| 150 | If a firewall is already installed you may also enable/disable the access to the internet calling: |
| 151 | |
| 152 | {{{ |
| 153 | # insert NAT rule |
| 154 | iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE |
| 155 | # enable forwarding |
| 156 | echo 1 > /proc/sys/net/ipv4/ip_forward |
| 157 | }}} |
| 158 | |
| 159 | and disabling the internet access with: |
| 160 | |
| 161 | {{{ |
| 162 | # remove NAT rule |
| 163 | iptables -t nat -F |
| 164 | # disable forwarding |
| 165 | echo 0 > /proc/sys/net/ipv4/ip_forward |
| 166 | }}} |
| 167 | |
| 168 | Connection via Ipv6 |
| 169 | |
| 170 | Since the Ipv6 Address range is limited to approximately 3.5^9 addresses and most of them are already used, the next generation of the Internet protocol was developed. A major advantage of Ipv6 is that there are enough room for providing all systems with unique and world wide valid without the need of special thinks as NAT or STUN. With the advance of technology and use of IP based services for computers, IP-based telephones and so on the need of an extended range of IP address increase considerably. |
| 171 | |
| 172 | With IPv6 the full connectivity to the net will become real and the communication between different systems will be easier. |
| 173 | |
| 174 | All major OS (BSD, Linux MacOS X and other UNIX like systems support actually IPv6. This is also the case for Windows Vista and Windows XP (on XP you may need some supplements from Microsoft). |
| 175 | |
| 176 | If you have a connection to the IPv6 world via a provider as SIXXS.NET or |
| 177 | use 6to4 (Protocol 41) you will get an IPv6 main Address and have the possibility to use own segments for your local network. Each of the systems will be connected to the IPv6 network without the need of NAT and so on. |
| 178 | |
| 179 | For this case we assume that you will try IPv6 on a virtual machine and |
| 180 | get your first experiences with IPv6 based network. |
| 181 | |
| 182 | A further assumption is that you have a fix IPv6 address from sixxs.net or an other supplier and also the ability to configure your own segment. |
| 183 | |
| 184 | The address you will get may look as 2001:XXXX:YYYY:ZZZZ::2 (XXXX, YYYY ans ZZZZ are hexadecimal coded values). This will be the main address for connection to IPv6 via a tunnel. If your supplier provide you with the ability to use segments you will get the main part of the IPv6 Address you may use (Prefix) and this should be 2001:XXXX:SSSS::/48 XXXX. You can with this use the prefixes 2001:XXXX:SSSS:0000::/64 up to 2001:XXXX:SSSS:ffff::/64 within your IPv6 network. |
| 185 | |
| 186 | For the bridge you will need an own interface or tap device, it shall not be connected to your main interface port. |
| 187 | |
| 188 | A script for setting the bridge look as for the example above, there are only a fews differences. |
| 189 | |
| 190 | {{{ |
| 191 | #!/bin/sh |
| 192 | # set PATH for the case we are called via sudo or su root |
| 193 | |
| 194 | PATH=/sbin:/usr/bin:/bin:/usr/bin |
| 195 | |
| 196 | # create a tap |
| 197 | tunctl -t tap1 -u <user> |
| 198 | ip link set up dev tap1 |
| 199 | |
| 200 | # create the bridge |
| 201 | brctl addbr br0 |
| 202 | brctl addif br0 tap1 |
| 203 | |
| 204 | # set the IP address and routing |
| 205 | ip link set up dev br0 |
| 206 | ip -6 addr add 2001:XXXX:SSSS:1::1/64 dev br0 |
| 207 | ip -6 route add 2001:XXXX:SSSS:1:/64 dev br0 |
| 208 | }}} |
| 209 | |
| 210 | On the virtual machine you can use the automatic router and ip setting feature provided by IPv6. In this case you should install radvd (provided by most Linux distributions) and modify the file /etc/radvd.conf: |
| 211 | |
| 212 | {{{ |
| 213 | interface br0 |
| 214 | { |
| 215 | AdvSendAdvert on; |
| 216 | MinRtrAdvInterval 30; |
| 217 | MaxRtrAdvInterval 100; |
| 218 | prefix 2001:XXXX:SSSS:1::/64 |
| 219 | { |
| 220 | AdvOnLink on; |
| 221 | AdvAutonomous on; |
| 222 | AdvRouterAddr on; |
| 223 | }; |
| 224 | }; |
| 225 | }}} |
| 226 | |
| 227 | The interface (br0) and the IPv6 prefix must be modified, the other values are normally OK. |
| 228 | |
| 229 | On the virtual host you must not do anythings, the IPv6 Address and the routing will be set automatically. The only problem is that DNS will not work. |
| 230 | You may reach other host within the IPv6 net if you use the IPv6 Address directly. |
| 231 | On Linux and UNIX like systems you can solve the DNS problem if you put into the file /etc/resolv.conf the IPv6 address of the name server. Your DNS server will probably not have an IPv6 Address and your virtual host will not |
| 232 | deal with Ipv4 Addresses. |
| 233 | In order to solve this problem you can download ptrtd and totd. After compiling, configuring then and starting them you must only edit the /etc/resolv.conf file and put there the line nameserver 2001:XXXX:SSSS:1::1 |
| 234 | to this file and you will have full connection to the Ipv4 and Ipv6 worlds. |
| 235 | Totd (Trick or Treat Daemon) ftp://ftp.pasta.cs.uit.no/pub/Vermicelli is a caching nameserver which look first fo IPv6 addresses and if there is no such |
| 236 | address look for the IPv4 address and build an IPv6 address which will have the configured prefix. |
| 237 | Ptrtd (Portable Transport Relay Translator Daemon) hear recponize such address and translate the IPv6 ethernet frames to IPv4 Frames and for frames returned from the outside convert them to IPv6. |
| 238 | |
| 239 | Compiling Totd may fail, but you can fix this if you edit the file Makefile and delete the option -Werror. |
| 240 | Totd is normally installed under /usr/local/sbin and expect the configurations file totd.conf under the directory /usr/local/etc. |
| 241 | This file shall contain the following: |
| 242 | |
| 243 | {{{ |
| 244 | forwarder YOUR_IPv4_ADDRESS |
| 245 | prefix 2001:XXX:SSS:N:: |
| 246 | pidfile /var/run/totd.pid |
| 247 | }}} |
| 248 | |
| 249 | YOUR_IPv4_ADDRESS and 2001:XXX:SSS:N:: are to be replaced with proper values, N is the segment number for translated IPv4 addresses and may be for example 4. |
| 250 | |
| 251 | For ptrtd there is no configuration file, so you must start it as follow: |
| 252 | {{{ |
| 253 | ptrtd -p 2001:XXX:SSS:N:: |
| 254 | }}} |
| 255 | |
| 256 | Name resolution for you IPv6 Network |
| 257 | |
| 258 | The easiest way is to run avahi on your host and on the guest systems the corresponding service. Avahi is the Zeroconf implementation on Linux and is available on the most recent distributions. ZeroConf was originally developed by Apple and is available under Mac OSX. |
| 259 | Under Linux you shall edit the file /etc/nsswitch.conf and modify the hosts line according the following: |
| 260 | hosts: files dns mdns6 |
| 261 | This shall be done for the host system and the Linux virtual machine. Please refer also to the avahi documentation. |