또이리의 Server Engineer

우분투 멀티 IP - 다중 IP 설정 본문

Linux Engineer

우분투 멀티 IP - 다중 IP 설정

또이리 2020. 11. 17. 23:51

우분투 멀티 IP - 다중 IP 설정

Ubuntu18.04 multiple IP, 우분투 다중 IP

이번 스토리는 랜 포트 하나에 두 개 이상의 IP 주소를 사용할 수 있도록 설정하는 방법을 알아보겠습니다. 방법은 두 가지 압니다. netplan을 사용하는 방법과 interfaces를 사용하는 방법입니다.

 

/etc/netplan/01-netcfg.yaml

파일명은 다를 수도 있습니다. /etc/netpan/ 디렉터리 안에 있는 파일을 편집해 주면 됩니다. 매우 간단합니다.

일단 현재 IP를 확인하겠습니다.

root@localhost:~# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.231.129  netmask 255.255.255.0  broadcast 192.168.231.255
        inet6 fe80::20c:29ff:fe83:7e55  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:83:7e:55  txqueuelen 1000  (Ethernet)
        RX packets 76  bytes 6062 (6.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18  bytes 1381 (1.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.231.130  netmask 255.255.255.0  broadcast 192.168.231.255
        inet6 fe80::20c:29ff:fe83:7e5f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:83:7e:5f  txqueuelen 1000  (Ethernet)
        RX packets 161  bytes 44524 (44.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 117  bytes 15949 (15.9 KB)
        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 92  bytes 7128 (7.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 92  bytes 7128 (7.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@localhost:~#

ens33, ens34 디바이스와 해당 디바이스의 IP를 확인할 수 있습니다.

여러 이유로 LAN port 한 개에 두 개의 IP를 사용하는 경우가 있습니다.

 

ens33을 192.168.231.129 외에 192.168.231.131을 추가해 보겠습니다.

vim /etc/netplan/01-netcfg.yaml을 입력해서 네트워크 설정을 편집하겠습니다.

root@localhost:~# vim /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: yes
      addresses: [192.168.231.129/24, 192.168.231.131/24]
      gateway4: 192.168.231.2
      nameservers:
              addresses: [8.8.8.8, 8.8.4.4]
    ens34:
      dhcp4: yes
      addresses: [192.168.231.130/24]
      gateway4: 192.168.231.2
      nameservers:
              addresses: [8.8.8.8, 8.8.4.4]

편집 후 netplan apply를 입력해서 적용시킵니다.

root@localhost:~# netplan apply
root@localhost:~# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:83:7e:55 brd ff:ff:ff:ff:ff:ff
    inet 192.168.231.129/24 brd 192.168.231.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.231.131/24 brd 192.168.231.255 scope global secondary ens33:0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe83:7e55/64 scope link
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:83:7e:5f brd ff:ff:ff:ff:ff:ff
    inet 192.168.231.130/24 brd 192.168.231.255 scope global ens34
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe83:7e5f/64 scope link
       valid_lft forever preferred_lft forever
root@localhost:~#

ens33에 두 개의 IP가 적용된 것이 확인됩니다.

 

/etc/network/interfaces

user@ubuntu:~$ vim /etc/network/interfaces
# ifupdown has been replaced by netplan(5) on this system.  See
# /etc/netplan for current configuration.
# To re-enable ifupdown on this system, you can run:
#    sudo apt install ifupdown
auto lo
iface lo inet loopback

auto ens33
iface ens33 inet static
address 192.168.231.128
netmask 255.255.255.0
gateway 192.168.231.2
dns-nameservers 8.8.8.8

auto ens34
iface ens34 inet static
address 192.168.231.131
netmask 255.255.255.0
gateway 192.168.231.2
dns-nameservers 8.8.8.8

우분투의 예전 버전과 같이 interfaces를 사용하시려면 apt-get install ifupdown을 설치하셔야 합니다.

interfaces 파일에 진입하시면 위의 내용은 입력하셔야 합니다.

IP는 임의로 네트워크 상황에 맡게 입력하시고 중요한 것은 ens33에 IP를 추가하는 것입니다.

auto lo
iface lo inet loopback

auto ens33
iface ens33 inet static
address 192.168.231.128
netmask 255.255.255.0
gateway 192.168.231.2
dns-nameservers 8.8.8.8

auto ens33:0
iface ens33:0 inet static
address 192.168.231.129
netmask 255.255.255.0

auto ens34
iface ens34 inet static
address 192.168.231.131
netmask 255.255.255.0
gateway 192.168.231.2
dns-nameservers 8.8.8.8

위와 같이 ens33:0을 추가해 주시면 됩니다.

 

systemctl restart networking.service를 사용해서 네트워크 서비스를 재시작합니다.

root@ubuntu:~# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.231.128  netmask 255.255.255.0  broadcast 192.168.231.255
        inet6 fe80::20c:29ff:fe83:7e55  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:83:7e:55  txqueuelen 1000  (Ethernet)
        RX packets 767  bytes 68459 (68.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 673  bytes 104418 (104.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.231.129  netmask 255.255.255.0  broadcast 192.168.231.255
        ether 00:0c:29:83:7e:55  txqueuelen 1000  (Ethernet)

ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.231.131  netmask 255.255.255.0  broadcast 192.168.231.255
        inet6 fe80::20c:29ff:fe83:7e5f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:83:7e:5f  txqueuelen 1000  (Ethernet)
        RX packets 51  bytes 3380 (3.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21  bytes 1666 (1.6 KB)
        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 84  bytes 6084 (6.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 84  bytes 6084 (6.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@ubuntu:~# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:83:7e:55 brd ff:ff:ff:ff:ff:ff
    inet 192.168.231.128/24 brd 192.168.231.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.231.129/24 brd 192.168.231.255 scope global secondary ens33:0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe83:7e55/64 scope link
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:83:7e:5f brd ff:ff:ff:ff:ff:ff
    inet 192.168.231.131/24 brd 192.168.231.255 scope global ens34
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe83:7e5f/64 scope link
       valid_lft forever preferred_lft forever
root@ubuntu:~#

위와 같이 적용된 것을 확인할 수 있습니다.

multiple IP

Comments