Ubuntu 18.04 Netplan을 사용한 Static IP 설정

ubuntu19.04 May 28, 2018

Ubuntu는 한 3년전부터인가 CentOS보단 개인적으로 더 주력으로 많이 사용했던거 같다.

16.04 LTS 나올때부터 꾸준히 쓰긴 했었는데 어느샌가 벌써 18.04가 LTS 버전으로 나왔다.

16.04로 바꿔서 쓸때도 systemctl 때문에 한동안 매우 힘들었는데 18.04 올라가고 벌써부터 바뀐 것들이 너무 많다. 뭐 이리 배울게 많아 ㅠㅠ

어찌됐든 또 잊어버리기 전에 다음에 헤매지 않게 기억해본다.

Static IP 설정

첫번째 난관은 네트워크 인터페이스 설정이다.

17.10부터 네트워크 인터페이스 설정이 NetPlan이라는 새로운 네트워크 설정 시스템으로 변경된 것 같다.

확실히 기존의 인터페이스 설정(/etc/network/interfaces)보다는 더 간결하고 쉽게 YAML을 사용하여 관리가 가능하다.

/etc/netplan/*.yaml

해당 위치에 인터페이스 설정이 가능하고 NetworkManagernetworkd 두개의 렌더러를 사용할 수 있다.

NetworkManger 렌더러를 사용하는 경우에는 X Window 환경에서만 사용하고 나머지 경우는 networkd 렌더러를 사용하면 된다.

네트워크 인터페이스 확인

우선 ifconfig -a 를 통해 설정할 네트워크 인터페이스를 확인한다.

hkwon@canary-test-server:~$ 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
        inet6 fe80::a00:27ff:fe57:92b1  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:57:92:b1  txqueuelen 1000  (Ethernet)
        RX packets 375  bytes 314494 (314.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 178  bytes 18901 (18.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 08:00:27:22:8f:0b  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        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 28  bytes 2208 (2.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 28  bytes 2208 (2.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8 인터페이스를 NetPlan을 통해 설정해보자.

netPlan을 통한 Static IP 설정

아마 디폴트로 세팅했다면 아래와 같이 기본 인터페이스 설정이 존재한다.

/etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        enp0s3:
            addresses: []
            dhcp4: true
            optional: true
    version: 2

dhcp로 설정된 enp0s3 인터페이스가 보일 것이다.

새로운 인터페이스인 enp0s8번을 설정해보자

$ sudo vi /etc/netplan/01-netcfg.yaml

나는 virtualbox 호스트네트워크를 사용한 인터페이스를 추가해서 ethernets 섹션의 ip192.168.56.100/24로 설정하고 기타 아래와 같이 gatewaydns 정보를 입력한다.

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
 version: 2
 renderer: networkd
 ethernets:
   enp0s8:
     dhcp4: no
     dhcp6: no
     addresses: [192.168.56.100/24]
     gateway4: 192.168.56.1
     nameservers:
       addresses: [8.8.8.8,8.8.4.4]

다른 섹션은 볼 필요 없고 ethernets 섹션만 정확하게 설정하고 저장한다.

  • dhcp4 : IPv4 dhcp 설정
  • dhcp6 : IPv6 dhcp 설정
  • addresses : ,로 구분한 IP 멀티로 가능
  • gateway4 : IPv4 gateway 설정
  • nameservers : dns 설정 ,로 구분 멀티로 설정 가능 [생략 가능]

설정 재적용

새 설정을 적용하려면 아래의 명령어를 실행한다.

$ sudo netplan apply

다시 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
        inet6 fe80::a00:27ff:fe57:92b1  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:57:92:b1  txqueuelen 1000  (Ethernet)
        RX packets 1566  bytes 400006 (400.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 980  bytes 153691 (153.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.100  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fe22:8f0b  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:22:8f:0b  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5  bytes 386 (386.0 B)
        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 40  bytes 3108 (3.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40  bytes 3108 (3.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

정상적으로 변경된 것을 확인 할 수 있다.

MacBook-Pro:~ hkwon$ ssh hkwon@192.168.56.100
The authenticity of host '192.168.56.100 (192.168.56.100)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.100' (ECDSA) to the list of known hosts.
hkwon@192.168.56.100's password:
Welcome to Ubuntu 18.04 LTS (GNU/Linux 4.15.0-22-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
...

외부에서도 정상적으로 세팅된 IP로 접속 가능한 것을 볼 수 있다.

Tags

권혁

백발까지 코딩하고 싶은 상부상조 프로그래머

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.