Keywords : Transform Ubuntu in a Router, How to invert your desktop in a Router, IP Forwarding and Masquerading, Router Ubuntu
Why to Transform Ubuntu Desktop in a Router ? The answer is simple: For fun as well as learn new things. 😉
In this tutorial I’ll show How to invert your desktop in a Router.
This can be helpful to utilize your old desktop as it won’t require much hardware. You can setup your own router, can implement much secure firewall, setup different services and list go on. Once you learn basics, you can utilize this as you want. 🙂
In this tutorial, I’ll use Ubuntu Desktop edition as operating system. People may argue on using desktop edition instead of Server edition, so my reasons to choose desktop edition are:
-
Many users may feel difficulty to use server edition.
-
Home users mostly install desktop edition.
-
This post also address those users who are newbies and have less experience with Linux’s command prompt.
-
Advance users can move with server edition later as basics will be same.
Note that you will require a desktop/laptop with :
-
Ubuntu desktop edition istalled in it
-
Two NIC cards, one for external interface(internet) another one for internal interface(local network)
Users who have one WiFi NIC and one ether-net NIC , can also follow this post. Please check:
-
If they are having internet connection on wi-fi and their ethernet NIC is free to use in local network, that’s fine.
-
If they have internet on ethernet port and want to use wi-fi as AP for local network then
Check if their WiFi card support “master” mode, if yes , then we have no problem
-
Open a terminal and give command
$ ip addr $ iwconfig $ sudo iplink set dev down $ sudo iwconfig mode master |
First command is to know what interfaces you have.
Second command “iwconfig” will tell you, which interface is your wi-fi interface.
Rest of the commands to set your wi-fi card in master mode.
-
If no, then they have to use “hostapd” package to move on.
Those who don’t have Ubuntu installed already, can make their system dual-boot and install Ubuntu. [A separate post will be uodated soon regarding making a dual-boot system.. till then Google for it.. ;)]
Well, everything is set to go? Ok. Follow these steps:
-
Know what are your internet settings i.e ip-address etc. Using ifconfig. See this post for more refernce:
How to do URL Blocking / Content Filtering / ParentalLock
-
Now decide what ip-address you will keep for your router. It shold not match/come in your internet ip-address range. Choose only from private ip-addresses. For this tutorial I am taking following:
Settings
Value
IP-Address
192.168.1.1
Net-Mask
255.255.255.0
Broadcast Address
192.168.1.255
-
Know your avilable interfaces and see which interface is being used for which purpose(inteanal or external). Here I am assuming following, you check it with your seetings and use the same.
Interface
Type and use
eth0
External- connected to a router or modem for internet
eth1/wlan0
Internal- connected to a switch/hub or another computer
-
If you have more than two NIC and all are in use then we have to use bridge-utility. This will be covered later in this tutorial.
-
Now take a backup of your old network settings in case anything goes wrong. Type this command in terminal:
1<span style="font-family: UbuntuMono, courier, monospace;">sudo cp /etc/network/interfaces /etc/network/interfaces.bak</span> -
Open ‘/etc/network/interfaces’ file and replace text of this file with the text given below:
-
If you have Dynamic ip-address (DHCP)
# Set up the local loopback interface
auto lo
iface lo inet loopback
# Set up the External interface, I am taking eth0 replace it
#with your interface
auto eth0
iface eth0 inet dhcp
-
If you have static ip-address
-
# Set up the local loopback interface
auto lo
iface lo inet loopback
# Set up the External interface, I am taking eth0 replace it
#with your interface
#Change xxx.xxx.xxx.xxx with Numeric settings provided by your Internet provider
auto eth0
iface eth0 inet static
address xxx.xxx.xxx.xxx
netmask xxx.xxx.xxx.xxx
gateway xxx.xxx.xxx.xxx
nameserver xxx.xxx.xxx.xxx
nameserver xxx.xxx.xxx.xxx
-
Restart the network services and test whether this is working or not.
-
$ sudo /etc/init.d/networking restart
$ ping techawarey.wordpress.com
You should see output like this:
-
Open “/etc/network/interfaces” file again and append following lines:
-
If your going to use wired NIC as an internal interface then append following lines
-
# Set up the Internal interface, I am taking eth1 replace it
#with your interface
auto eth1
iface eth0 inet static
address 192.168.1.1
netmask 255.255.255.0
gateway 192.168.1.1
nameserver 8.8.8.8
nameserver 8.8.4.4
-
If you are going to use wi-fi as internal interface then append following:
# Set up internal interface using wi-fi
auto wlan0
iface wlan0 inet static
wireless-mode master
wireless-essid “Techawarey”
wireless-channel 1
address 192.168.1.1
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
You have set-up “eth1” as static as your machine has DHCP Server to assign ip’s to client machines. If you don’t have DHCP server installed yet, please follow this post as it’s necessary to proceed further.
-
As both interfaces has configured, now we have to connect them in order to make a router. For this we are going to use “IP forwarding and Masquerading” technique. Open a file using gedit, name it as “nat.sh” and copy and paste following in it. This is a script from
-
echo -e “\n\nLoading simple rc.firewall-iptables version $FWVER..\n”
DEPMOD=/sbin/depmod
MODPROBE=/sbin/modprobe
EXTIF=”eth0″
INTIF=”eth1″
#INTIF2=”eth0″
echo ” External Interface: $EXTIF”
echo ” Internal Interface: $INTIF”
#======================================================================
#== No editing beyond this line is required for initial MASQ testing ==
echo -en ” loading modules: “
echo ” – Verifying that all kernel modules are ok”
$DEPMOD -a
echo “———————————————————————-“
echo -en “ip_tables, “
$MODPROBE ip_tables
echo -en “nf_conntrack, “
$MODPROBE nf_conntrack
echo -en “nf_conntrack_ftp, “
$MODPROBE nf_conntrack_ftp
echo -en “nf_conntrack_irc, “
$MODPROBE nf_conntrack_irc
echo -en “iptable_nat, “
$MODPROBE iptable_nat
echo -en “nf_nat_ftp, “
$MODPROBE nf_nat_ftp
echo “———————————————————————-“
echo -e ” Done loading modules.\n”
echo ” Enabling forwarding..”
echo “1” > /proc/sys/net/ipv4/ip_forward
echo ” Enabling DynamicAddr..”
echo “1” > /proc/sys/net/ipv4/ip_dynaddr
echo ” Clearing any existing rules and setting default policy..”
iptables-restore <<-EOF
*nat
-A POSTROUTING -o “$EXTIF” -j MASQUERADE
COMMIT
*filter
:INPUT ACCEPT [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A FORWARD -i “$EXTIF” -o “$INTIF” -m conntrack –ctstate ESTABLISHED,RELATED -j ACCEPT
-A FORWARD -i “$INTIF” -o “$EXTIF” -j ACCEPT
-A FORWARD -j LOG
COMMIT
EOF
echo -e “\nrc.firewall-iptables v$FWVER done.\n”
-
Save it and change it’s read write attribute:
-
$ chmod a+x nat.sh
-
Test the script using root access
-
$ sudo sh nat.sh
$ ping techawarey.wordpress.com
If you get same result as you got earlier, your script is working. Connect with another system in the network and see if you can access internet from there or not. If yes, Congratulations…
you have Transform Ubuntu Desktop in a Router and have learnt How to invert your desktop in a Router.
If your script working well then make it auto-start when Ubuntu boots:
$ sudo cp nat.sh /etc/init.d/ $ sudo ln -s /etc/init.d/nat.sh /etc/rc2,d/s95techawarey |
Now restart your system and see after restart you can still access internet from another system. If no, see you make that script executable properly.
For more information on this tutorial please visit:
https://help.ubuntu.com/community/Router
If you liked this post please like and share it.. 😉
If have any query please leave a comment. 🙂
Transform Ubuntu Desktop in a Router or How to invert your desktop in a Router – You have learnt basics, so you can add your firewall rules using iptables. I’ll post a tutotial on that soon.
Also you can run a web-server on this router and mangae/control your router and other services. For more on this topic, please see this Host a Local web-site on Linux/Ubuntu
iptv server
Hello,nice share.
[…] Transform Ubuntu Desktop in a Router : How to invert your desktop in a Router […]