본문 바로가기
IT 지식정리/운영체제

리눅스 라우팅(route)관련 설정

by G. Hong 2018. 3. 25.
728x90
반응형

리눅스에서 route와 관련된 설정을 확인 해 보도록 하겠습니다.

라우팅 정보를 확인 할 수 있는 가장 기본적인 커맨드는 route -n과 netstat -r이 있습니다. 아래는 제 VM에 설정된 내용들입니다. 네트워크 enp0s3과 enp0s8 이 각각 다른 게이트웨이를 사용하고 있습니다. 

enp0s3의 경우는 10.0.2.2 게이트웨이를 통해서 연결이 이루어 집니다.

[root@centos test1]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 enp0s3
0.0.0.0         10.0.0.1        0.0.0.0         UG    101    0        0 enp0s8
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s8
10.0.2.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
[root@centos test1]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 enp0s3
default         gateway         0.0.0.0         UG        0 0          0 enp0s8
10.0.0.0        0.0.0.0         255.255.255.0   U         0 0          0 enp0s8
10.0.2.0        0.0.0.0         255.255.255.0   U         0 0          0 enp0s3
[root@centos test1]# ifconfig -a
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        ...생략...

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.20  netmask 255.255.255.0  broadcast 10.0.0.255
        ...생략...

아래의 커맨드들을 통해서 라우팅테이블에서 게이트웨이를 삭제 할 수 있습니다.

[root@centos test1]# route del default gw 10.0.2.2
[root@centos test1]# route del -net 10.0.2.0 netmask 255.255.255.0 enp0s3
[root@centos test1]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.1        0.0.0.0         UG    100    0        0 enp0s8
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s8

그리고 다시 route add 커맨드를 통해서 라우팅테이블에 게이트웨이를 추가해 보도록 하겠습니다.

[root@centos test1]# route add -net 10.0.2.0 netmask 255.255.255.0 enp0s3
[root@centos test1]# route add default gw 10.0.2.2

아직은 정상적으로 업데이트 된 것 같지 않습니다. 네트워크 서비스를 다시 시작한 뒤에는 라우팅테이블이 다시 원복이 되었습니다.

[root@centos test1]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.1        0.0.0.0         UG    100    0        0 enp0s8
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s8
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
[root@centos test1]# service network restart
Restarting network (via systemctl):                        [  OK  ]
[root@centos test1]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 enp0s3
0.0.0.0         10.0.0.1        0.0.0.0         UG    101    0        0 enp0s8
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s8
10.0.2.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s3


그리고 아래와 같이 route-interface 파일 수정을 통해서 항상 라우팅테이블 정보가 유지되도록 설정이 가능합니다.

[root@centos test1]# cat /etc/sysconfig/network-scripts/route-enp0s3
ADDRESS0=10.0.2.15
NETMASK0=255.255.255.0
GATEWAY0=10.0.2.2


728x90
반응형