Today’s tip from Steenbe.nl
The first step is to make sure that your internal network functions. You should setup your second Ethernet wired or wireless card and set its IP address to something like “192.168.10.1? via “ifconfig” utility as follows:
$ ifconfig eth1 192.168.10.1 netmask 255.255.255.0
This setup will be forgotten after a reboot, its better to add these lines to /etc/network/interfaces (replacing any previous declarations of eth1):
# The extended interfaces
auto eth1
iface eth1 inet static
address 192.168.10.1
netmask 255.255.255.0
Check if the previous command worked by typing the following:
$ ifconfig
The result will look like this
ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:6E:8A:BD:ED
inet addr:192.168.1.64 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:6eff:fe8a:bded/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:80071 errors:0 dropped:0 overruns:0 frame:0
TX packets:44847 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:119404142 (113.8 MB) TX bytes:3332468 (3.1 MB)
Interrupt:20
eth1 Link encap:Ethernet HWaddr 00:20:18:3A:4E:AE
inet addr:192.168.10.1 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::220:18ff:fe3a:4eae/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2092 errors:0 dropped:0 overruns:0 frame:17
TX packets:1998 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:158939 (155.2 KB) TX bytes:348453 (340.2 KB)
Interrupt:16 Base address:0xa800
Next edit the following file to add the DHCP support on eth1:
$ nano /etc/dhcp3/dhcpd.conf
To save & close the file press Ctrl+X and then Y
Edit these values (add if missing)
# The ddns-updates-style parameter controls whether or not the server will
# attempt to do a DNS update when a lease is confirmed. We default to the
# behavior of the version 2 packages (’none’, since DHCP v2 didn’t
# have support for DDNS.)
ddns-update-style ad-hoc;# option definitions common to all supported networks…
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.10.255;
option routers 192.168.10.1;
option domain-name “SOMENAME”;
option domain-name-servers 192.168.10.1;default-lease-time 600;
max-lease-time 7200;# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;subnet 192.168.10.0 netmask 255.255.255.0 {
range 192.168.10.10 192.168.10.100;
}
Explanation:
eth1 has the IP address 192.168.10.1 and the dhcp server now uses it as its home ip address.
One more thing needs to be configured before we can run the DHCP server
Open the file /etc/default/dhcp3-server
$ nano /etc/default/dhcp3-server
# Defaults for dhcp initscript
# sourced by /etc/init.d/dhcp
# installed at /etc/default/dhcp3-server by the maintainer scripts
#
# This is a POSIX shell fragment
#
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. “eth0 eth1?.
INTERFACES=”eth1?
Explanation:
Like the comment it the file says we are specifying the network card which must be used to handle dhcp requests
Finally the setup is done!
So lets run the DHCP server with:
$ /etc/init.d/dhcp3-server start
/etc/init.d/dhcp3-server start
* Starting DHCP server dhcpd3
…done.
If it report fails then look at the error log file ($ less /var/log/syslog and press END to view the last events)
Now you can test the connection to the server from any client connected to the server by:
$ ping 192.168.10.1 (to check if you can see the server)
$sudo dhclient eth0 (where eth0 is the ethernet port used by the client)
The last command is to get DHCP information from the server to the client, which will be reported in the following manner:
$ dhclient eth0
Internet Systems Consortium DHCP Client V3.0.5
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/
Listening on LPF/eth0/00:0c:6e:8a:bd:ed
Sending on LPF/eth0/00:0c:6e:8a:bd:ed
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 4
DHCPOFFER from 192.168.10.1
DHCPREQUEST on eth0 to 255.255.255.255 port 67
DHCPACK from 192.168.10.1
bound to 192.168.10.64 — renewal in 41658 seconds.
Now that we have a connection to the server we want to have internet on the client pc. The internet connection of the server needs to be shared with the clients. The actual sharing component in Linux is done via the firewall (iptables or ipchains depending on the Kernel version)
First enable forwarding of the ipv4 layer
$ nano /etc/sysctl.conf
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See sysctl.conf (5) for information.
#
#kernel.domainname = example.com
#net/ipv4/icmp_echo_ignore_broadcasts=1
# the following stops low-level messages on console
kernel.printk = 4 4 1 7
# enable /proc/$pid/maps privacy so that memory relocations are not
# visible to other users.
kernel.maps_protect = 1
##############################################################3
# Functions previously found in netbase
#
# Uncomment the next line to enable Spoof protection (reverse-path filter)
#net.ipv4.conf.default.rp_filter=1
# Uncomment the next line to enable TCP/IP SYN cookies
#net.ipv4.tcp_syncookies=1
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.conf.default.forwarding=1
# Uncomment the next line to enable packet forwarding for IPv6
#net.ipv6.conf.default.forwarding=1
Now that we can do forwarding lets do it:
$ iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE (the forwarding)
$ echo 1 > /proc/sys/net/ipv4/ip_forward (the code to enable forwarding without reboot)
But like all programs the live configuration will be forgotten on reboot, so lets save it:
$ iptables-save > /etc/iptables.rules
Now that it’s saved we need to load the iptables.rules on starting the network:
$ nano /etc/network/interfaces (replacing any previous declarations of eth0):
# The extended interfaces
auto eth0
iface eth0 inet dhcp
pre-up iptables-restore < /etc/iptables.rules
post-down iptables-restore < /etc/iptables.rules
The complete file /etc/network/interfaces should look like this after all the modifications we applied:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).# The loopback network interface
auto lo
iface lo inet loopback# The primary network interface
auto eth0
iface eth0 inet dhcp
pre-up iptables-restore < /etc/iptables.rules
post-down iptables-save > /etc/iptables.rules# The extended interfaces
auto eth1
iface eth1 inet static
address 192.168.10.1
netmask 255.255.255.0
This blog is brought to you by CLiCK Computer Recycling
Would you like to buy me a beer?If you enjoyed this post, make sure you subscribe to my RSS feed!





















