How to do port redirection with Debian (and doing so, securing Tomcat)
Pubblicato da Luigi il 23 Giugno 2005 in Java, Internet, Linux, WebFirst of all, I have to precise that I'm not a sysadmin, and you may not assume that the things as explained here are best way to handle the problem, or just that this is a correct one. This worked for me yesterday night, and I share my notes here so that I'll find this easier next time, and hoping that someone will find this useful and time saving.
The problem
Why whould you want to do port redirection?
If you want to put a web server on internet it would be great to have it run on standard http port (80), as many company proxies just refuse
to connect on other ports. You can think to just configure tomcat's server.xml to run on port 80 instead of the default one, at 8080. Using unix,
this would work only if you run tomcat as priviledged user (root) because of common users cannot bind port under 1024.
"What's the problem?" could you say, but - believe me - leving a server online with tomcat running with root priviledges can be a very bad idea.
So, resuming, the problem is:
- you would like to bind tomcat on http port
- you want to run tomcat with a restricted user, to avoid hackers to gain root privileges
- ...but restricted users cannot bind http port (1 & 2 are in conflict)
Possible Solutions
There may be many solutions to this problem. Searching the net I've found an article titled "Running JSP Through Apache with mod_jk2" explaining how to work around this limtation. There are three possibilities:
- Accessing Tomcat on Port 80 (running tomcat as root)
- Forwarding Incoming Port 80 requests to Port 8080 using iptables
- Using Apache to forward incoming requests on http port to tomcat on port 8080 (mod_jk2)
Iptables howto
Being a windows user, and not knowing much about unix administration, the first thing I searched over the net was the command to be typed to
do port forwarding. I ever didn't know anything about iptables.
After some tries I find this:
hal9000:~# iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 hal9000:~# iptables-save
I tried at command line and it worked: request on http port were forwarded to tomcat! So, iptables is the solution. But after a reboot those
rules were lost: the problem now is "how to make those rules persistent thru reboots?". Before continuing the search over the net I tried "man iptables"
then looking on my own hard disk I found a little howto in /usr/share/doc/iptables/README.Debian.gz.
Reading it (Chap "3. running iptables"), I've met a new utility: "The closest to standard is the ipmasq package". I like to do things close to standards.
Things to do now:
- prepare a "self-written or acquired scripts to run at system startup" into /etc/init.d
- use update-rc.d to update the SysV run level processes
Then I typed at command line:
hal9000:~# ipmasq -v #: Interfaces found: ...ipmasq displayed lot of default settings here...
Good: ipmasq is there.
Now it's time for the scripts. To configure ipmasq you have to create a file <filename>.rul in /etc/ipmasq/rules path. Here's
the mine:
hal9000:~# cat /etc/ipmasq/rules/F00chain.rul #: #: ********************************************************** #: *** FORWARD CHAIN *** #: ********************************************************** #: $IPTABLES -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
Now we've to configure SysV init system to run ipmasq on startup. Taking example from /etc/init.d/skeleton file, I've written following script:
hal9000:/etc/init.d# cat /etc/init.d/ipmasq
#! /bin/sh
#
# ipmasq.init Set up IP Masquerading for Debian systems
#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Firewall"
NAME=ipmasq
DAEMON=/usr/sbin/$NAME
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
#
# Function that starts the daemon/service.
#
d_start() {
$DAEMON
}
case "$1" in
start|restart|force-reload)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
hal9000:/etc/init.d# chmod 755 /etc/init.d/ipmasq
Notice that you have to chmod 755 the file (as shown upon) as it needs to be executable.
Then we've to update SysV runlevels procedures with following command (and its output):
hal9000:/etc/init.d# update-rc.d ipmasq defaults 10 Adding system startup for /etc/init.d/ipmasq ... /etc/rc0.d/K10ipmasq -> ../init.d/ipmasq /etc/rc1.d/K10ipmasq -> ../init.d/ipmasq /etc/rc6.d/K10ipmasq -> ../init.d/ipmasq /etc/rc2.d/S10ipmasq -> ../init.d/ipmasq /etc/rc3.d/S10ipmasq -> ../init.d/ipmasq /etc/rc4.d/S10ipmasq -> ../init.d/ipmasq /etc/rc5.d/S10ipmasq -> ../init.d/ipmasq
This command says to create links to /etc/init.d/ipmasq script for all runlevels at position 10: you see that created files contains "K10" and "S10" in their name, they are the startup and kill links for the service. Runlevels links are executing in order by their name, so 10 means that our script will be executed quite early.
We've done. At next reboot we should see "Starting Firewall: ipmasq" message before the login prompt. Now - if all went right - your firewall should forward http request to port 8080. And you can run tomcat on port 8080 with a restricted user, and access it from internet on standard http port.
...and now you know it
Cerca
Archivi
- Gennaio 2010 (2)
- Dicembre 2009 (1)
- Novembre 2009 (3)
- Settembre 2009 (2)
- Agosto 2009 (4)
- Luglio 2009 (1)
- Giugno 2009 (2)
- Maggio 2009 (4)
- Aprile 2009 (2)
- Marzo 2009 (7)
- Febbraio 2009 (5)
- Gennaio 2009 (2)
- Dicembre 2008 (1)
- Novembre 2008 (8)
- Ottobre 2008 (12)
- Settembre 2008 (3)
- Agosto 2008 (2)
- Luglio 2008 (6)
- Giugno 2008 (16)
- Maggio 2008 (2)
- Aprile 2008 (3)
- Marzo 2008 (6)
- Ottobre 2007 (1)
- Settembre 2007 (1)
- Agosto 2007 (5)
- Luglio 2007 (6)
- Giugno 2007 (6)
- Maggio 2007 (1)
- Marzo 2007 (1)
- Febbraio 2007 (2)
- Gennaio 2007 (1)
- Dicembre 2006 (2)
- Novembre 2006 (4)
- Ottobre 2006 (7)
- Settembre 2006 (1)
- Agosto 2006 (2)
- Luglio 2006 (6)
- Giugno 2006 (3)
- Febbraio 2006 (1)
- Gennaio 2006 (1)
- Dicembre 2005 (5)
- Novembre 2005 (2)
- Ottobre 2005 (2)
- Settembre 2005 (7)
- Agosto 2005 (2)
- Luglio 2005 (8)
- Giugno 2005 (12)
Categorie
- Books (7)
- Eclipse (10)
- Errors (2)
- Firefox (7)
- Hardware (14)
- Horror Code (8)
- Internet (17)
- Java (85)
- JavaScript (8)
- Life, universe and everything (29)
- Linux (44)
- Mac (18)
- Software (25)
- Speeches and Conferences (8)
- Web (19)
- Windows (16)
Ultimi Post
- Syntactic sugar and Java arrays.
- 3G USB Stick on Ubuntu
- Ipod touch with Linux
- Karmic and Luks: USB drive encryption made (almost) easy
- Suspend/Resume in Karmic /2
- Suspend/Resume problem in Ubuntu Karmic 9.10 running on MacBook Pro 5.1
- MacBook International Keyboard and Linux
- Mighty Mouse: reverse horizontal scrolling workaround on Ubuntu Linux 9.04
- Skype 2.1.0.47 beta released, and amd64 packages available!
- Linux RAM Disks
My open source projects
Blog License
Blogs I like
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Filippo Diotalevi
- JavaJournal.it Blog
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links








Hi,
I would like to use iptables to do a port forward on unix. Iptables isnt already installed on my unix machine. Using searches all i seem to be able to find are install instructions for linux. Where can i find install instructions for iptables for unix?
Cheers,
Joe.
it depends on which distribution you are using.
on debian it should be:
# apt-get install iptables
check it out: Debian Package Managing Survival Guide.
Otherwise you can download iptables source and install it from the sources as usual:
./config
make
make install
Thank you so much for posting this. This made everything a lot easier. BTW, this page is the top Google result for ‘debian tomcat port 80′
Thanks for the news William. Google rocks!
It works great, but in /etc/ipmasq/rules/F00chain.rul i had to modify the string:
#: $IPTABLES -t nat -I PREROUTING -p tcp –dport 80 -j REDIRECT –to-port 8080
with
iptables -t nat -I PREROUTING -p tcp –dport 80 -j REDIRECT –to-port 8080
:-)
Anyway thanks a lot, simple and working.
@Joe Quinn
Iptables is a Linux thing - other unixes may offer something alike though.
Although chances are you aren’t running UNIX, but a modern commercial unix offering or BSD.