60ネットワークの状態確認(CentOS7)

60ネットワークの状態確認(CentOS7)

外部からサーバにアクセスするためにはサーバ側で接続用のポートが開いている必要があります。

サーバに出入りするネットワークパケットに対して指定の条件を満たすものだけ、
開いているポートを通過させる仕組みがファイヤウォールの機能となります。

この機能の実現にはfirewalldと呼ばれるデーモンプロセスが行います。


ちなみに現在開いているポート番号と使用しているプログラム名を確認するには管理者権限で netstat (あるいは ss)コマンドを用います。

[root@aso ~]# netstat -pant      
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      1211/postgres
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1417/master
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1171/sshd
tcp        0     96 192.168.56.10:22        192.168.56.1:64305      ESTABLISHED 1451/sshd: guest [p
tcp6       0      0 :::5432                 :::*                    LISTEN      1211/postgres
tcp6       0      0 ::1:25                  :::*                    LISTEN      1417/master
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::22                   :::*                    LISTEN      1171/sshd
[root@aso ~]#

上記の例ではポート番号111,22,25,5432がすべてのipアドレス(0.0.0.0)に対して開いており、

25番(SMTP)は自分に対してのみ開いています。


lsofというコマンドでより詳しい情報も取得できます

[root@aso ~]# lsof -i -nP
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
systemd     1     root   39u  IPv4  14666      0t0  TCP *:111 (LISTEN)
systemd     1     root   41u  IPv4  14667      0t0  UDP *:111
systemd     1     root   42u  IPv6  14668      0t0  TCP *:111 (LISTEN)
systemd     1     root   43u  IPv6  14669      0t0  UDP *:111
rpcbind   707      rpc    4u  IPv4  14666      0t0  TCP *:111 (LISTEN)
rpcbind   707      rpc    5u  IPv4  14667      0t0  UDP *:111
rpcbind   707      rpc    6u  IPv6  14668      0t0  TCP *:111 (LISTEN)
rpcbind   707      rpc    7u  IPv6  14669      0t0  UDP *:111
rpcbind   707      rpc   10u  IPv4  14971      0t0  UDP *:877
rpcbind   707      rpc   11u  IPv6  14972      0t0  UDP *:877
chronyd   724   chrony    5u  IPv4  15652      0t0  UDP 127.0.0.1:323
chronyd   724   chrony    6u  IPv6  15653      0t0  UDP [::1]:323
sshd     1171     root    3u  IPv4  19077      0t0  TCP *:22 (LISTEN)
sshd     1171     root    4u  IPv6  19087      0t0  TCP *:22 (LISTEN)
postgres 1211 postgres    3u  IPv4  19319      0t0  TCP *:5432 (LISTEN)
postgres 1211 postgres    4u  IPv6  19320      0t0  TCP *:5432 (LISTEN)
postgres 1211 postgres   10u  IPv6  19356      0t0  UDP [::1]:44218->[::1]:44218
postgres 1228 postgres   10u  IPv6  19356      0t0  UDP [::1]:44218->[::1]:44218
postgres 1229 postgres   10u  IPv6  19356      0t0  UDP [::1]:44218->[::1]:44218
postgres 1230 postgres   10u  IPv6  19356      0t0  UDP [::1]:44218->[::1]:44218
postgres 1231 postgres   10u  IPv6  19356      0t0  UDP [::1]:44218->[::1]:44218
postgres 1232 postgres   10u  IPv6  19356      0t0  UDP [::1]:44218->[::1]:44218
master   1417     root   13u  IPv4  19858      0t0  TCP 127.0.0.1:25 (LISTEN)
master   1417     root   14u  IPv6  19859      0t0  TCP [::1]:25 (LISTEN)
sshd     1451     root    3u  IPv4  20126      0t0  TCP 192.168.56.10:22->192.168.56.1:64305 (ESTABLISHED)
sshd     1472    guest    3u  IPv4  20126      0t0  TCP 192.168.56.10:22->192.168.56.1:64305 (ESTABLISHED)
[root@aso ~]#

基本はssコマンドを使いましょう。

[root@aso ~]# ss -pant
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port     
LISTEN     0      128          *:5432                     *:*                   users:(("postgres",pid=1009,fd=3))
LISTEN     0      100    127.0.0.1:25                       *:*                   users:(("master",pid=1238,fd=13))
LISTEN     0      128          *:111                      *:*                   users:(("rpcbind",pid=713,fd=4),("systemd",pid=1,fd=40))
LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=971,fd=3))
ESTAB      0      0      192.168.56.10:22                 192.168.56.1:56414               users:(("sshd",pid=2502,fd=3))
LISTEN     0      128       [::]:5432                  [::]:*                   users:(("postgres",pid=1009,fd=4))
LISTEN     0      100      [::1]:25                    [::]:*                   users:(("master",pid=1238,fd=14))
LISTEN     0      128       [::]:111                   [::]:*                   users:(("rpcbind",pid=713,fd=6),("systemd",pid=1,fd=43))
LISTEN     0      128       [::]:22                    [::]:*                   users:(("sshd",pid=971,fd=4))
[root@aso ~]#


●自分のIPアドレスを調べる

[root@aso ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:dc:64:e7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.101/24 brd 192.168.56.255 scope global noprefixroute dynamic enp0s3
       valid_lft 382sec preferred_lft 382sec
    inet 192.168.56.10/24 brd 192.168.56.255 scope global secondary noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::bec7:9feb:ce9b:5113/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
[root@aso ~]#

または

[root@aso ~]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.101  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::bec7:9feb:ce9b:5113  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:dc:64:e7  txqueuelen 1000  (Ethernet)
        RX packets 206  bytes 26884 (26.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 146  bytes 29671 (28.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 705  bytes 106541 (104.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 705  bytes 106541 (104.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@aso ~]#


●疎通試験の定番コマンド

[root@aso ~]# ping 192.168.56.10
PING 192.168.56.10 (192.168.56.10) 56(84) bytes of data.
64 bytes from 192.168.56.10: icmp_seq=1 ttl=64 time=0.040 ms
64 bytes from 192.168.56.10: icmp_seq=2 ttl=64 time=0.070 ms
64 bytes from 192.168.56.10: icmp_seq=3 ttl=64 time=0.058 ms
64 bytes from 192.168.56.10: icmp_seq=4 ttl=64 time=0.056 ms
64 bytes from 192.168.56.10: icmp_seq=5 ttl=64 time=0.064 ms
^C
--- 192.168.56.10 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.040/0.057/0.070/0.013 ms
[root@aso ~]#


●電源投入時の起動デーモン確認コマンド

[root@aso ~]# systemctl list-unit-files
UNIT FILE                                     STATE
proc-sys-fs-binfmt_misc.automount             static
dev-hugepages.mount                           static
dev-mqueue.mount                              static
proc-sys-fs-binfmt_misc.mount                 static
sys-fs-fuse-connections.mount                 static
sys-kernel-config.mount                       static
sys-kernel-debug.mount                        static
tmp.mount                                     disabled
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

●Networkの設定ファイル

ネットワークの設定ファイル     /etc/sysconfig/network
                                   /etc/sysconfig/network-scripts/ifconfig-eth0
                                   /etc/hosts
                                   /etc/resolv.conf


●SELinuxの設定を解除

vi /etc/sysconfig/selinux

—(下記を変更)—————————

SELINUX=enforcing
SELINUX=disabled

——————————


設定反映のためにOSの再起動が必要です。

shutdown -r now

起動後
getenforce コマンドでdisabledを確認する


●sshのポート確認

# netstat -tanp


●firewallの状況確認(CentOS7)
# systemctl status firewalld
# firewall-cmd --list-all

●firewallの電源投入時起動確認(CentOS7)
# systemctl -a | grep firewalld

●firewallの状況確認とポート追加(CentOS7)

[root@inspiron ~]# firewall-cmd --permanent --add-port=2222/tcp
success
[root@inspiron ~]# firewall-cmd --reload
success
[root@inspiron ~]# firewall-cmd --list-all

public (default, active)
  interfaces: enp0s3
  sources:
  services: dhcpv6-client ssh
  ports: 8000/tcp 2222/tcp
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:


●scpコピー

scp seigen@192.168.112.7:~/id_ecdsa.pub /home/ictsec


●sshポートを変更して接続

ssh -p 2222 keyuser2@192.168.1.63



●ネットワーク環境の確認と設定(以下の環境設定ファイルの個々のパラメータは自身の環境に読み替えること)

[root@centos centosuser1]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.64 centos.kanatech31.ac.jp centos
[root@centos centosuser1]#


[root@centos centosuser1]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
UUID=8eb9b526-d142-41a8-86e8-37d182b20d85
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
HWADDR=08:00:27:AE:73:70
IPADDR=192.168.1.64
PREFIX=24
GATEWAY=192.168.1.1
DNS1=192.168.1.15
DOMAIN=kanatech31.ac.jp
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
[root@centos centosuser1]#


[root@centos centosuser1]# cat /etc/sysconfig/network
HOSTNAME=centos.kanatech31.ac.jp
NETWORKING=yes
GATEWAY=192.168.1.1
[root@centos centosuser1]#


[root@centos centosuser1]# cat /etc/resolv.conf
# Generated by NetworkManager
search kanatech31.ac.jp
nameserver 192.168.1.15
[root@centos centosuser1]#


※(CentOS7の場合) 自機のIPアドレス等はここで確認・変更します
 
  cat /etc/sysconfig/network-scripts/ifcfg-enp0s3

※(CentOS7の場合) hostnameはここで確認・変更します
 
  cat /etc/hostname