Wednesday, September 21, 2011

linux: enable ipv4 forwarding

to enable ipv4 forwarding on a linux box, set ip_forward file to 1
# echo "1" > /proc/sys/net/ipv4/ip_forward
this enables forwarding till you reboot your box

to make ip forwarding available upon boot, you can set forwarding in /etc/sysconfig/network by adding
FORWARD_IPV4="TRUE"
i have a note that this doesn't work on CentOS, though.

another way is to create a start-up script to set a value in the ip_forward file
# vi /etc/init.d/ip_forward

then add the following lines:
#!/bin/bash
# ip_forward
#
# chkconfig: 235 90 25
# description:  enable ip_forward

PATH=/usr/bin:/sbin:/bin:/usr/sbin
export PATH

# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network

start() {
        echo "1" > /proc/sys/net/ipv4/ip_forward
}

stop() {
        echo "0" > /proc/sys/net/ipv4/ip_forward
}

restart() {
    stop
    start
}

status() {
        cat /proc/sys/net/ipv4/ip_forward
}

case "$1" in
start)
    start
    ;;

stop)
    stop
    ;;

restart)
    restart
    ;;

status)
    status
    ;;
*)
    echo $"Usage: $0 {start|stop|status}"
    exit 1
esac

exit $?


after creating the start-up file, add it in chkconfig as a service
# chkconfig --add ip_forward

ps. you can also just add this to your network start-up script
echo "1" > /proc/sys/net/ipv4/ip_forward

happy forwarding!!

ps. ps. one more way to do this in CentOS (tried it in 6.3) is by editing your /etc/sysctl.conf
# Controls IP packet forwarding
net.ipv4.ip_forward = 1

No comments:

Post a Comment

SSH : No matching host key type found. Their offer: ssh-rsa,ssh-dss

Got this while connecting to my mikrotik router via ssh   Unable to negotiate with <ip address> port <ssh port>: no matching hos...