ボールを蹴りたいシステムエンジニア

ボール蹴りが大好きなシステムエンジニア、ボールを蹴る時間確保の為に時間がある時には勉強する。

CentOS7でfirewalldの代わりにiptablesを使用する

CentOS7ではファイアウォールiptablesからfirewalldに変更されたようだ。
7系でもyumインストールすれば従来のiptablesも利用事は出来るとの事なので、iptablesを使用する事にした。

環境

firewolldの起動状態を確認。
Activeとなっており起動してる。

[root@localhost httpd-2.4.25]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 木 2017-01-12 22:09:24 JST; 2 days ago
     Docs: man:firewalld(1)
 Main PID: 609 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─609 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

 1月 12 22:09:18 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
 1月 12 22:09:24 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.

firewalldを停止する

systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld

停止を確認
inactiveとなってる。

[root@localhost httpd-2.4.25]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

 1月 12 22:09:18 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
 1月 12 22:09:24 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
 1月 15 12:21:57 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
 1月 15 12:21:57 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.

iptablesのインストール

yum -y install iptables-services


インストール確認

[root@localhost ~]# iptables --version
iptables v1.4.21

iptablesの起動と自動起動の設定

systemctl start iptables
systemctl enable iptables

起動してる事を確認

[root@localhost httpd-2.4.25]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
   Active: active (exited) since 日 2017-01-15 12:23:41 JST; 8s ago
 Main PID: 85483 (code=exited, status=0/SUCCESS)

 1月 15 12:23:40 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables...
 1月 15 12:23:41 localhost.localdomain iptables.init[85483]: iptables: Applying firewall rules: [  OK  ]
 1月 15 12:23:41 localhost.localdomain systemd[1]: Started IPv4 firewall with iptables.

iptables設定ファイルが生成されてる

cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

80ポートを開放する為、下記を追記
※22番ポート開放の後ろの行に追加

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT


iptablesをサービス登録

[root@localhost ~]# systemctl enable iptables.service
[root@localhost ~]# systemctl list-unit-files | grep iptables
iptables.service                            enabled 

iptables起動

[root@localhost ~]# systemctl start iptables.service

設定が反映されてる事を確認

[root@localhost ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

適用後は設定したポートが開いてるか他のサーバーから確認。
※nmapが無い場合はyumインストール

[root@localhost ~]# nmap 192.168.10.111

Starting Nmap 5.51 ( http://nmap.org ) at 2017-01-15 12:29 JST
Nmap scan report for 192.168.10.111
Host is up (0.0000030s latency).
Not shown: 997 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
8008/tcp open  http

Nmap done: 1 IP address (1 host up) scanned in 13.04 seconds

参考

qiita.com

CentOSに日本時間設定する

CentOSに日本時間設定する

環境

CentOS6

手順

現在の日時確認

[root@lb01 ~]# date
2017年  1月 15日 日曜日 01:50:05 PST

実際は「2017年 1月 15日 日曜日 09:54:05」
約8時間ずれてる。

CentOSタイムゾーンを設定しているファイルlocaltimeファイルに日本時間用ファイルを適用

[root@lb01 ~]# cp -p /usr/share/zoneinfo/Japan /etc/localtime

NTPサーバーのインストール

[root@lb01 ~]# yum -y install ntp

NTPサーバーの日付を設定

[root@lb01 ~]# ntpdate ntp.nict.jp
15 Jan 09:57:44 ntpdate[2389]: step time server 133.243.238.163 offset -32182.482955 sec

ntp.confの編集

[root@lb01 ~]# vi /etc/ntp.conf

centos.pool.ntp.org」の行をコメントアウトし、「server -4 ntp 〜」の行を追加する。

コメントアウト

#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst                                                                               
#server 2.centos.pool.ntp.org iburst                                                                               
#server 3.centos.pool.ntp.org iburst

追記

server -4 ntp.nict.jp
server -4 ntp1.jst.mfeed.ad.jp
server -4 ntp2.jst.mfeed.ad.jp
server -4 ntp3.jst.mfeed.ad.jp

ntpdデーモン起動

[root@lb01 ~]# /etc/init.d/ntpd start  

設定後は、日本時間がずれなく表示されてる

[root@lb01 ~]# date
2017年  1月 15日 日曜日 10:00:36 JST

※2017年1月29日追記

上記設定は簡易版なので、NTPの設定は下記記事より。

toriaezu-engineer.hatenablog.com

CentOSでホスト名を設定する

環境

CentOS6

手順

現在の設定確認

[root@localhost ~]# hostname
localhost.localdomain

設定変更

[root@localhost ~]# vi /etc/sysconfig/network

HOSTNAMEの設定を変更
今回はlb01というホスト名にする。

HOSTNAME=lb01

ネットワークサービスを再起動

[root@localhost ~]# /etc/rc.d/init.d/network restart

OS再起動

reboot

再起動後はホスト名が反映される

Last login: Sun Jan 15 00:44:10 2017 from 192.168.10.103
[root@lb01 ~]# 

VMwareの環境コピーでMACアドレスが変わった場合の対処方法

VMwareの仮想環境をコピー後、IPを変更しようとするとエラー発生。

ネットワーク再起動時にエラー・・

[root@localhost Desktop]# /etc/init.d/network restart
ループバックインターフェースを終了中                       [  OK  ]
ループバックインターフェイスを呼び込み中                   [  OK  ]
インターフェース eth0 を活性化中:  エラー: 適切なデバイスが見付かりません: 接続 'System eth0'用のデバイスが見付かりません
                                                           [失敗]

VMWare 側で認識していた NICMAC アドレスと、CentOS 側で認識していた eth0 のMAC アドレスの値に相違がある事が原因らしい。

ifconfigでetc0が無く、etc1がある。
コピー元の仮想環境ではetc0を設定していたのに。
ちなみに、HWaddrがMACアドレスですが、コピー元と異なってました。

[root@localhost Desktop]# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:0C:29:52:42:AF  
          inet addr:192.168.10.119  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe52:42af/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:36 errors:0 dropped:0 overruns:0 frame:0
          TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:6162 (6.0 KiB)  TX bytes:2109 (2.0 KiB)

eth0の設定ファイル見ると、MACアドレスの値(HWADDR)が古いまま。

[root@localhost Desktop]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE="eth0"
BOOTPROTO="none"
HWADDR="00:0C:29:B2:B1:23"

この違いが原因かな。
インストール済みのHDDを移動したりLANカードを差し替えるとMACアドレスが変更になるが、VWwareでも同様にMACアドレスが変わるみたい。

70-persistent-net.rulesの設定を見ても、新しくetc1が追加されており、etc1の方に新しいMACアドレスが設定されてる。

[root@localhost Desktop]# cat /etc/udev/rules.d/70-persistent-net.rules

# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:b2:b1:23", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:52:42:af", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

対処方法

70-persistent-net.rulesを修正。

[root@localhost Desktop]# vi /etc/udev/rules.d/70-persistent-net.rules

etc1の設定を削除し、etc0のATTRを適切なMACアドレスに変更。

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:52:42:af", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

etc0の設定

[root@localhost Desktop]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

HWADDRを適切なMACアドレスに変更

HWADDR="00:0c:29:52:42:af"

ネットワーク再起動
エラー発生せず成功。

[root@localhost Desktop]# /etc/init.d/network restart
ループバックインターフェースを終了中                       [  OK  ]
ループバックインターフェイスを呼び込み中                   [  OK  ]
インターフェース eth0 を活性化中:  アクティブ接続の状態: アクティベート中
アクティブ接続のパス: /org/freedesktop/NetworkManager/ActiveConnection/1
状態: アクティベート済み
接続はアクティベート済み
                                                           [  OK  ]

OS再起動

reboot

ネットワークにetc0が認識されている事を確認。

[root@localhost Desktop]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:52:42:AF  
          inet addr:192.168.10.20  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe52:42af/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:31 errors:0 dropped:0 overruns:0 frame:0
          TX packets:28 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:4433 (4.3 KiB)  TX bytes:2024 (1.9 KiB)

解決

CentOSで固定のローカルIPを設定する

CentOSで固定のローカルIPを設定する。

環境

CentOS6

手順

デフォルトの設定。
192.168.10.118が割り振られている

[root@localhost ~]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:B2:B1:23  
          inet addr:192.168.10.118  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:feb2:b123/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:230 errors:0 dropped:0 overruns:0 frame:0
          TX packets:157 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:22925 (22.3 KiB)  TX bytes:23444 (22.8 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:240 (240.0 b)  TX bytes:240 (240.0 b)

ネットワークインターフェイスeth0を192.168.10.10に設定

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 

DEVICE="eth0"
BOOTPROTO="none"                                                                                                   
HWADDR="00:0C:29:B2:B1:23"
IPV6INIT="yes"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
UUID="dbfb5e23-424f-42ea-ade1-de65051ddb4b"
IPADDR=192.168.10.10                                                                                               
PREFIX=24
GATEWAY=192.168.10.1                                                                                               
NETMASK=255.255.255.0
DNS1=192.168.10.1
DNS2=192.168.10.2 

変更・追記した点

  • ONBOOT="yes"

ブート時にアクティブにされる

  • BOOTPROTO="none"

固定IPにする為none。
動的ならdhcp

  • IPADDR

固定IP

  • NETMASK

IPアドレスに対するネットマスクの指定

デフォルトのゲートウエイアドレス

デフォルトのゲートウエイアドレス

  • DNS1、DNS2

使用するDNS、resolv.confのIPと合わせましょう。
このファイルに記述しないと、network再起動時に、NetworkManagerによりresolv.confが初期化されてしまう事があります。
ハマった・・

DNSも設定する必要あり

[root@localhost ~]# vi /etc/resolv.conf

とりあえずデフォルトゲートウェイのIP設定

nameserve 192.168.10.1

ゲートウェイの設定

[root@localhost ~]# vi /etc/sysconfig/network

IPを設定

NETWORKING=yes
HOSTNAME=localhost.localdomain
GATEWAY=192.168.10.1

ネットワーク再起動

[root@localhost ~]# service network restart

変更が反映されてる事を確認

[root@localhost ~]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:B2:B1:23  
          inet addr:192.168.10.10  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:feb2:b123/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:852 errors:0 dropped:0 overruns:0 frame:0
          TX packets:489 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:83002 (81.0 KiB)  TX bytes:77341 (75.5 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:588 (588.0 b)  TX bytes:588 (588.0 b)||<

外部ネットワークへ接続できる事を確認。
接続できなければ何かミスがあるかも。

[root@localhost ~]# curl www.yahoo.co.jp