○ iptablesのインストール 4 Router

・サイトの入口でファイアウォールをつくり、パケットを制御します
・iptables-1.4.12.1を使用しています
・ADSLスクリプト(firewall-masq)で設定します
・既存を削除し、オリジナルソースからインストールします
・make はメンテナンスマシンで行っています

・ソースの取得、削除は省略します
・エディタ、ファイラーの操作は省略します
・ソースを展開したら説明文を読みましょう
・Enterキーを省略します

・ログイン端末で "#" はroot、"$" は一般ユーザのプロンプトの記号です
・設定ファイルで "#" で始まる行はコメントとして解釈されます

○関連サイト

:iptables-1.4.12.1.tar.bz2  (http://www.netfilter.org/)

○参照ファイル

パッケージの追加
最初の設定 (Router)

○インストール環境

:OS CentOS 5.6
:selinux permissive
:インストール先 
:作業 /usr/src
:言語 LANG=en_US.UTF-8
:ユーザー rootで実行

・メンテナンスの場合は一般ユーザから行います



○iptables-1.4.12.1

○旧iptablesの削除

・自動起動スクリプトも削除されます
・必要な場合は、名前を変えて保存します

# cd /etc/init.d
[root@venus:/etc/init.d]
# cp iptables iptables-ORG && chmod 644 iptables-ORG
# cp ip6tables ip6tables-ORG && chmod 644 ip6tables-ORG
# rpm -e iptables iptables-ipv6

○iptablesのインストール

# cd /usr/src
[root@venus:/usr/src]
# tar jxvf iptables-1.4.12.1.tar.bz2
# chown -R root:root iptables-1.4.12.1
# cd iptables-1.4.12.1
[root@venus:/usr/src/iptables-1.4.12.1]
# make distclean
# ./configure CFLAGS="-O2 -march=pentium3" \
CPPFLAGS="-O2 -march=pentium3" \
--enable-devel --enable-libipq

・CFLAGS="-O2 -march=pentium3" (CPU最適化オプション)
・CPPFLAGS="-O2 -march=pentium3" (CPU最適化オプション)
・該当しない場合は指定しない

# make
# make install
# cd ..
[root@venus:/usr/src]
# tar jcvf iptables-1.4.12.1-rt-110904.tar.bz2 iptables-1.4.12.1/

○キャッシュ情報の更新と確認

・/usr/local/libの設定がされているなら、ldconfigのみ実行します

# cd ~
[root@venus:~]
# echo /usr/local/lib >> /etc/ld.so.conf.d/usrlocal.conf
# ldconfig
# ldconfig -p | grep -e libip -e libxtable

○/sbinにシンボリックリンクを作ります

# cd /sbin
[root@venus:/sbin]
# ln -s /usr/local/sbin/ip6tables ip6tables
# ln -s /usr/local/sbin/ip6tables-restore ip6tables-restore
# ln -s /usr/local/sbin/ip6tables-save ip6tables-save
# ln -s /usr/local/sbin/iptables iptables
# ln -s /usr/local/sbin/iptables-restore iptables-restore
# ln -s /usr/local/sbin/iptables-save iptables-save

○selinuxヘの対応 (selinuxを有効にしている場合)

:新規インストールファイルのselinuxラベルを修正

[root@venus:~]
# restorecon -Rv /usr/local



○Firewallingの作業工程

rp-pppoeで制御 iptablesスクリプトで制御
rp-pppoeの設定

firewall-masqの作製

(iptablesの解除)

rp-pppoeの起動
firewall-masqの作製

firewall-masqの実行

iptablesスクリプトでsave

iptables-configの修正

iptablesスクリプトの実行

(rp-pppoeの設定)

○フォワーディング設定

:/etc/sysctl.conf ファイルの修正

・フォワーディング設定

net.ipv4.ip_forward = 1

・指定されたファイルから読み込んだ内容を sysctl 設定にロードします

[root@venus:~]
# sysctl -p



○iptablesの設定 (pppoeで制御する)

rp-pppoeのインストールを参照

○/etc/ppp/firewall-masqの保存

[root@venus:~]
# cp /etc/ppp/firewall-masq /etc/ppp/firewall-masq-ORG

○/etc/ppp/firewall-masqの修正

・ユーザ・グループ・パーミッション (root root 644)
・スクリプトですが、権限がなくてもpppoeスクリプトが読み込んで実行します

firewall-masq (詳細別記)

○iptablesの停止

・iptablesをrpmパッケージでインストールしている場合です

[root@venus:~]
# service iptables stop
# chkconfig --del iptables

○/etc/init.d/pppoeスクリプトを再起動します

[root@venus:~]
# service pppoe restart

○iptablesのチェックをします

・チェーンが組まれたかどうかのチェックです

[root@venus:~] # iptables -L -n -v | less # iptables -t nat -L -n -v | less

○selinuxのラベルを修正 (selinuxが有効の場合)

[root@venus:~]
# restorecon -Rv /etc
# restorecon -Rv /usr/local



○iptablesの設定 (rpmパッケージの起動スクリプトを使う)

・iptablesを新規インストールした場合は、保存したファイルをもどします

# cd /etc/init.d
[root@venus:/etc/init.d]
# mv iptables-ORG iptables
# chmod 755 iptables
# cd ../sysconfig
# mv iptables-config.rpmsave iptables-config

○firewall-masqの実行と確認

firewall-masq (詳細別記)

・適当なディレクトリでfirewall-masqスクリプトを実行します

# cd ~
[root@venus:~]
# source firewall-masq

・iptablesのチェック

# service iptables status

○設定の保存

・iptablesの設定を保存します

[root@venus:~]
# service iptables save

・/etc/sysconfig/iptablesファイルとして保存されます


○/etc/sysconfig/iptables-configの修正

・マシンの再起動後、自動でロードされないモデュールを既述します

IPTABLES_MODULES="ip_conntrack_ftp ip_nat_ftp"

○iptablesスクリプトの実行

[root@venus:~]
# service iptables start
# chkconfig --add iptables

○rp-pppoeの設定

・iptablesが先に起動するので設定を書き換えないようにFirewallingを解除します

[root@venus:~]
# pppoe-setup

・省略

FIREWALLING
>>> choose a type of firewall (0-2):0

** Summary of what you enterd **
Ethernet Interface:eth1
User name:xxxxxxxxxxxx@ka.il24.net
Active-on-demand:NO
DNS: Do not adjust
Firewalling:NONE

>>>Accept these settings and adjust configuration files(y/N)?y

○selinuxラベルを修正 (selinuxが有効の場合)

[root@venus:~]
# restorecon -Rv /etc
# restorecon -Rv /usr/local



○開発版のインストール(参考)

・gitで開発版をダウンロードしてインストールします

○gitのインストールと実行

# cd /usr/src
[root@venus:/usr/src]
# tar jxf git-1.7.6.1.tar.bz2
# chown -R root:root git-1.7.6.1
# cd git-1.7.6.1
[root@venus:/usr/src/git-1.7.6.1]
# ./configure
# make
# make install

・iptablesのダウンロード
・iptablesディレクトリが作られます

# cd /usr/src
[root@venus:/usr/src]
# git clone git://git.netfilter.org/iptables.git

・アップデートする場合

# cd /usr/src/iptables
[root@venus:/usr/src/iptables]
# git pull -v

○iptablesのインストール

# cd /usr/src/iptables
[root@venus:/usr/src/iptables]
# ./autogen.sh
# ./configure CFLAGS="-O2 -march=pentium3" \
CPPFLAGS="-O2 -march=pentium3" \
--enable-devel --enable-libipq
# make
# make install

○ Copyright(c) 528p.com All Rights Reserved.