Ubuntu 12.04 IPv4 NAT Gateway and DHCP Server

Before I begin this post, I want to thank Internet Connection Sharing – Ubuntu 10.04 NAT Gateway Setup (Abridged Version) for providing the bulk of the tutorial. I have made some modifications for Ubuntu 12.04.

The setup is simple: a single Ubuntu server will act as a gateway and DHCP server for a local network. All other machines on the local network will receive their IPs from the DHCP server. To make things easier, I’ll call this Ubuntu server “Skyray” for the rest of the post.

Skyray has two network interfaces, eth0 and eth1. eth0 is on the 10.20.30.0/24 subnet and this is the Internet facing interface. eth1 is on the 172.22.22.0/24 subnet, where all other machines are also present. Basically, eth0 will connect to the Internet and eth1 will serve DHCP requests and act as the gateway.

/etc/network/interfaces

First you need to configure eth0 and eth1 for Skyray. Edit the file and make sure it has at least the following settings (or whatever settings are appropriate for your environment).

sudo vim /etc/network/interfaces

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address 10.20.30.77
    netmask 255.255.255.0
    gateway 10.20.30.1
    network 10.20.30.0
    broadcast 10.20.30.255
    dns-nameservers 10.20.30.15 10.20.30.16
    dns-search codeghar.com

auto eth1
iface eth1 inet static
    address 172.22.22.1
    netmask 255.255.255.0
    network 172.22.22.0
    broadcast 172.22.22.255

/etc/sysctl.conf

You need to enable IPv4 forwarding. To do so, edit this file.

sudo vim /etc/sysctl.conf

And uncomment the line

# net.ipv4.ip_forward=1

so that it now appears as

net.ipv4.ip_forward=1

Save the file and run the following command to make the change effective without a reboot.

sudo sysctl -w net.ipv4.ip_forward=1

/etc/rc.local

You’ll need to allow iptables rules for NAT to work. Edit the file and save it.

sudo vim /etc/rc.local

Make sure the following two lines appear before the exit 0 line in the file.

/sbin/iptables -P FORWARD ACCEPT
/sbin/iptables --table nat -A POSTROUTING -o eth0 -j MASQUERADE

To make these iptables rules active without rebooting, run the following commands:

sudo iptables -P FORWARD ACCEPT

sudo iptables –-table nat -A POSTROUTING -o eth0 -j MASQUERADE

Install DHCP server

sudo aptitude install isc-dhcp-server

/etc/dhcp/dhcpd.conf

Configure your newly installed DHCP server. Edit the file and save.

sudo vim /etc/dhcp/dhcpd.conf

The file is very well commented and you can learn a lot reading it. Just make sure it has at least the following configuration.

ddns-update-style none;

# option definitions common to all supported networks...
option domain-name "codeghar.com";
option domain-name-servers 10.20.30.15, 10.20.30.16;

default-lease-time 3600;
max-lease-time 7200;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

# This is a very basic subnet declaration.

subnet 172.22.22.0 netmask 255.255.255.0 {
  range 172.22.22.21 172.22.22.250;
  option routers 172.22.22.1;
}

/etc/default/isc-dhcp-server

We want to serve DHCP only on eth1 interface to we need to configure it that way. Edit the file and save it.

sudo vim /etc/default/isc-dhcp-server

The line will look like this before you change it

INTERFACES=""

And after you change it, it will look like this:

INTERFACES="eth1"

Now you should stop and start the DHCP server.

sudo service isc-dhcp-server stop

sudo service isc-dhcp-server start

Conclusion

Now any machines you have on the 172.22.22.0/24 network will get their IP address from Skyray if they are set to DHCP. And Skyray will also serve as their gateway.

Mouse Scroll Wheel Doesn’t Work in Fedora 17 in VirtualBox 4.1.10

I installed Fedora 17 alpha (KDE as my desktop environment) in VirtualBox but the mouse scroll wheel didn’t work. It was a regular two-button mouse with a scroll wheel. I had also installed VirtualBox Guest Additions. Thanks to Scroll wheel in debian guest after updating virtualbox from 3.x to 4.x, I was able to get the scroll wheel to work.

su -
vim /usr/share/X11/xorg.conf.d/50-vmmouse.conf

Now make sure 50-vmmouse.conf looks like the following.


Section "InputClass"
        Identifier      "vmmouse"
        MatchIsPointer  "on"
        MatchTag        "vmmouse"
        Driver          "vmmouse"
        Option          "Buttons" "5"
        Option          "ZAxisMapping" "4 5"
EndSection

Save file and quit vim. Reboot the machine and your mouse wheel should now work.

Remember to re-install VirtualBox Guest Additions (VBGA) after updating your kernel and then reboot. Otherwise the wheel stops working until you re-install VBGA.

Install VirtualBox Guest Additions in Fedora

I was running Fedora 17 alpha guest on a Windows 7 host (VirtualBox version 4.1.10) and wanted to install VirtualBox Guest Additions. From the VM window menu click Devices and select Install Guest Additions. This will mount a CD under /media. Open a terminal and run the following commands.

codeghar@codeghar:~$ su -

root@codeghar:/root$ yum install gcc kernel-devel

root@codeghar:/root$ cd /media/VBOXADDITIONS_4.1.10_76836/

root@codeghar:/root$ ./VBoxLinuxAdditions.run

root@codeghar:/root$ reboot

Reboot the system and you should be good to go.

Remember to re-install VirtualBox Guest Additions (VBGA) after updating/upgrading your kernel and then reboot. Otherwise some features or custom settings might not work.

Fedora 17 Post Install Checklist

I installed Fedora 17 alpha recently to really learn the ins and outs of the Fedora/RHEL ecosystem. I installed the minimal version with no GUI.

Enable Network

By default Fedora 17 had network disabled. I ran the following commands to get it running.

su -c 'systemctl enable network.service'
su -c 'systemctl start network.service'

By default it’ll pick up an IP using DHCP. If you want to set static IP address, modify your /etc/sysconfig/network-scripts/ifcfg-eth0 (or equivalent) file.

Install Applications

There are some applications I use that are not installed by default. Here’s a list of those applications and how to install them.

  • presto su -c 'yum install yum-presto'
  • vim su -c 'yum install vim'
  • scp su -c 'yum install openssh-clients'
  • ntpd su -c 'yum install ntp'
  • nslookup su -c 'yum install bind-utils'
  • whois su -c 'yum install whois'

Disable SELinux

No matter how much I want to learn SELinux, I very seldom get a chance to do so. Meanwhile, SELinux can sometimes cause headaches so I like to change it from ‘enforcing’ to ‘permissive’. Remember that doing so may cause your system to be less secure.

su -c 'vim /etc/selinux/config'

Change the line “SELINUX=enforcing” to “SELINUX=permissive”, save the file, quit vim, and then reboot the server.

Full Path in bash Command Prompt

Fedora 17′s bash displays only the current directory in the prompt. For example,

[codeghar@codeghar curr_dir]$

This can be confusing sometimes so I prefer to show the whole path. For example,

[codeghar@codeghar /path/to/curr_dir]$

To make this change globally (regular users and root), change the /etc/bashrc file.

su -

vim /etc/bashrc

Find the line similar to the one below:

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "

Note the use of \W which indicates that only the current directory should be displayed. Change it to \w and it’ll show the full path. For example,

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \w]\\$ "

Save the file and quit vim. Log out and then log in again for the change to take effect. Now your prompt should show the full path.

If you only want this change for yourself, change the ~/.bashrc file.

vim /home/codeghar/.bashrc

In this file add the following line:

export PS1="[\u@\h \w]\\$ "

Save and quit vim. Log out and then log in again for the change to take effect. Now your prompt should show the full path.

KDE Session

If you’re using KDE and don’t want it to load the last session when you login, remember to go to System Settings, Session Management, and change the default to not remember the last session.

Generate HTML and PDF from DocBook in Debian

DocBook is a widely-used format for writing documentation, articles, books, etc. For my purposes, I needed to generate XHTML and PDF files from documentation in DocBook format on a Debian Wheezy server.

Install

You need to install the following packages.

sudo aptitude install xsltproc docbook-xsl-ns docbook5-xml

Convert single DocBook file to XHTML

Now comes the conversion. Run xsltproc as below and it will create an HTML file (mybook.html in this case) in the current directory.

xsltproc -o mybook.html /usr/share/xml/docbook/stylesheet/docbook-xsl-ns/xhtml-1_1/docbook.xsl mydocbook.xml

You can explore the /usr/share/xml/docbook/stylesheet/docbook-xsl-ns/ path for more options.

Convert modular DocBook file to XHTML

You can create a modular DocBook document (a book in my case) by separating out chapters of the book into separate files and including them in the main file. For example, there’s only one chapter in my book so I’ll have two files: docbook.book.xml and docbook.chapter.xml. These two files would look something like the following:

An example of file docbook.book.xml

<?xml version="1.0" encoding="UTF-8"?>
<book xml:id="wikiply_doc" xmlns="http://docbook.org/ns/docbook" version="5.0" xmlns:xi="http://www.w3.org/2001/XInclude">
    <title>Sample Book</title>
    <bookinfo>
        <author>
            <personname><firstname>Code</firstname><surname>Ghar</surname></personname>
        </author>
        <legalnotice>
            <para>Copyright 2011-2012 Code Ghar. All rights reserved.</para>
            <para>Redistribution and use in source (SGML DocBook) and 'compiled' forms (SGML, HTML, PDF, PostScript, RTF and so forth) with or without modification, are permitted.</para>
        </legalnotice>
    <copyright><year>2012</year><holder>Code Ghar</holder></copyright>
    </bookinfo>
    <xi:include href="docbook.chapter.xml" />
</book>

An example of file docbook.chapter.xml

<?xml version="1.0" encoding="UTF-8"?>
<chapter xml:id="installation" xmlns="http://docbook.org/ns/docbook" version="5.0" >
<title>Sample Chapter</title>
    <section xml:id="sample_chapter">
        <title>Sample Chapter</title>
        <para>This is example text in sample chapter</para>
    </section>
</chapter>

Run xsltproc as below and it will create an HTML file (mybook.html in this case) in the current directory from both files.

xsltproc -xinclude -o mybook.html /usr/share/xml/docbook/stylesheet/docbook-xsl-ns/xhtml-1_1/docbook.xsl docbook.book.xml

Note the use of the -xinclude flag in the command and the xi:include XML tag in the docbook.book.xml file. These two things make the magic of modular DocBook possible.

bash alias

Since I work with a DocBook book often, I have created a bash alias as below:

alias dbtohtml="xsltproc -xinclude -o /home/codeghar/book/mybook.html /usr/share/xml/docbook/stylesheet/docbook-xsl-ns/xhtml-1_1/docbook.xsl /home/codeghar/book/docbook.book.xml; sed -e 's/</\n</g' -e 's/<meta name/\n<meta http-equiv="Content-Type" content="text\/html; charset=utf-8" \/> \n <meta name/g' -i /home/codeghar/book/mybook.html"

The generated file does not have the HTML meta tag to identify it as UTF-8 and so space characters display as the character  in the web browser. Therefore, sed is used to enter the appropriate meta tag in the file.

Convert DocBook to PDF

Using the same example files (docbook.book.xml and docbook.chapter.xml), we will create a PDF instead of an XHTML file.

You need to install Apache FOP.

sudo aptitude install fop

Next you need to create an intermediate file (mybook.fo) as below.

xsltproc -xinclude -o mybook.fo /usr/share/xml/docbook/stylesheet/docbook-xsl-ns/fo/docbook.xsl docbook.book.xml

Finally, run the following command to create the PDF file:

fop mybook.fo -pdf mybook.pdf

Hat Tips

DocBook Ubuntu Documentation; How to generate pdf from docbook 5.0; Getting Started with Docbook Book Authoring on Ubuntu; Writing Documentation; Playing With DocBook 5.0

Install Freeswitch on Ubuntu Server

I have started learning Freeswitch today and will document all the thing which I need to run the Freeswitch in this way I can keep track of my learning path and at the same time have reference guide for myself and others.

Freeswitch can be installed on Linux and Windows. It is up to the user which platform he is comfortable with. I personally prefer linux. I have choose Ubuntu Server 10.04 32-bit to run on my VirtualBox.

Let get start with the Installation.

Step 1.
Download Ubuntu Server 10.04 from “http://www.ubuntu.com/download/server/download” and and install the base system with ssh only to manage it remotely.

Step 2.
Install Freeswitch from repository.
For Ubuntu Server version 10+ run this command first.
2.a. freeswitch@localhost~: sudo apt-get install pkg-config
2.b. Run following command from linux prompt
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:freeswitch-drivers/freeswitch-nightly-drivers
sudo apt-get update
sudo apt-get install freeswitch freeswitch-lang-en freeswitch-sounds-en-us-callie-8000
Freeswitch installed at /opt/freeswitch/

Step 3.
Fix the permissions
chown -R user:group /opt/freeswitch

Step 4.
Enable the freeswitch by editing the file called freeswitch located at /etc/default to true
FREESWITCH_ENABLED=”true”

Step 5.
To run Freeswitch use this command.
Goto directory /opt/freeswitch/bin/ and run
./freeswitch
you will get the prompt
freeswitch@user>
Run some command to test such as
>version
>status

Thanks,
Sh
Reference: www.freeswitch.org

Follow

Get every new post delivered to your Inbox.