본문 바로가기

Linux/CentOS

[CentOS 7]yum 및 ping 실행이 안될때 (NAT, NAT네트워크 설정)

2023.09.24

 

Network 관련된 것을 하려고 yum을 사용해서 패키지를 설치하려고 했는데, 오류가 뜨는 것이다..!!

그래서 찾아봤는데 네트워크 연결에서 문제가 난 것이었다. 혹시나 외부망으로 안가나는 것인가 싶어서 ping을 했더니 역시나 ping이 안나는 것이었다..

 

[root@localhost ~]# yum  -y install net-tool
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"


 One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

 

 

환경 : Virtualbox

OS: Centos7

[user@localhost ~]$ ping 8.8.8.8
connect: Network is unreachable

ping이 밖으로 나가지 않는다. 분명 network 설정 시 포트도 제대로 만든것같은데, 이상해서 확인해보았다.

 

[user@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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:1a:24:4c brd ff:ff:ff:ff:ff:ff
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:38:68:32 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.102/24 brd 192.168.56.255 scope global noprefixroute dynamic enp0s8
       valid_lft 478sec preferred_lft 478sec
    inet6 fe80::bd8b:2bd0:4a52:10bb/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:87:cd:99 brd ff:ff:ff:ff:ff:ff

가상어댑터 1 (enp0s3) : NAT

가상어댑터 2 (enp0s8) : 호스트 어댑터

가상어댑터 3 (enp0s9) : NAT 네트워크

 

문제는 NAT를 인터페이스만 설정 해놨지 연결해두지 않았더랬다.

 

IP설정을 해두기로 했다. 그런데, IP 설정을 할때 수동으로 설정하는게 머리가 아프다 (사실 쉬운데 하기 싫은거다).

간단하게 DHCP로 설정하기로 했다.

 

Network 설정 시 사용자 'user'에 권한을 주지 않았기때문에 실행이 안되서 미리 su - root로 해서 root로 변경하고 시작한다

[root@localhost sysconfig]# cd /etc/sysconfig/network-scripts/

경로는 다음과 같다. 

[root@localhost network-scripts]# cat ifcfg-enp0s3
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=20213144-7568-458b-a764-720eec94d342
DEVICE=enp0s3
ONBOOT=yes

 

 

맨 마지막 ONBOOT 부분을 yes로 변경하고 데몬을 재시작한다.

 

[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# systemctl status network
● network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
   Active: active (exited) since Sun 2023-09-24 11:49:35 KST; 1s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 4163 ExecStop=/etc/rc.d/init.d/network stop (code=exited, status=0/SUCCESS)
  Process: 4414 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)

Sep 24 11:49:34 localhost.localdomain systemd[1]: Stopped LSB: Bring up/down networking.
Sep 24 11:49:34 localhost.localdomain systemd[1]: Starting LSB: Bring up/down networking...
Sep 24 11:49:35 localhost.localdomain network[4414]: Bringing up loopback interface:  [  OK  ]
Sep 24 11:49:35 localhost.localdomain network[4414]: Bringing up interface enp0s3:  Connection successfully activ.../14)
Sep 24 11:49:35 localhost.localdomain network[4414]: [  OK  ]
Sep 24 11:49:35 localhost.localdomain network[4414]: Bringing up interface enp0s8:  Connection successfully activ.../15)
Sep 24 11:49:35 localhost.localdomain network[4414]: [  OK  ]
Sep 24 11:49:35 localhost.localdomain systemd[1]: Started LSB: Bring up/down networking.
Hint: Some lines were ellipsized, use -l to show in full.

데몬을 재시작하고 잘 올라와있는지 확인한다 (필자는 다른 것도 이것저것 건드려서 다음과 같은 상태이다).

 

그리고서 IP를 확인해보면..

[root@localhost network-scripts]# 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:1a:24:4c brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic enp0s3
       valid_lft 86387sec preferred_lft 86387sec
    inet6 fe80::8028:af81:5871:a48e/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:38:68:32 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.102/24 brd 192.168.56.255 scope global noprefixroute dynamic enp0s8
       valid_lft 587sec preferred_lft 587sec
    inet6 fe80::bd8b:2bd0:4a52:10bb/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:87:cd:99 brd ff:ff:ff:ff:ff:ff

이전과 다르게 enp0s3 (NAT)에 IP 가 생성된 것을 알 수 있다.

 

 

그리고 나서 ping을 했을때 

[root@localhost network-scripts]# ping -c 5 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=55 time=34.7 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=55 time=35.7 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=55 time=33.9 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=55 time=36.8 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=55 time=35.7 ms

--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4010ms
rtt min/avg/max/mdev = 33.903/35.410/36.885/1.021 ms

 


더보기

Q. NAT과 NAT 네트워크 차이가 무엇인가요?

NAT

NAT(Network Address Translation) 는 VM이 외부 네트워크(인터넷)에 접근할 수 있는 가장 간단한 방법입니다. Host / Guest 에 어떠한 설정을 할 필요가 없기 때문에, Virtualbox 에서 VM을 생성하면 기본적으로 attach 되는 네트워크이기도 합니다.

NAT Network

NAT Network는 공유기와 같은 환경을 만드는 것으로 이해하면 매우 쉽습니다.

여러 VM을 같은 네트워크에 속하게 하고 싶은데, 모두 인터넷이 가능하게 하고 싶을 때 사용하면 매우 유용합니다.

(보통 VM을 같은 네트워크로 묶고, 인터넷이 가능하게 하려면 VM 에 Host-only, NAT 두 개의 네트워크 인터페이스를 붙입니다)

 

 

참고

https://hg2lee.tistory.com/entry/Network-%EA%B0%80%EC%83%81%EB%A8%B8%EC%8B%A0%EC%97%90%EC%84%9C-%EB%84%A4%ED%8A%B8%EC%9B%8C%ED%81%AC%EC%9D%98-%EC%A2%85%EB%A5%98-Bridged-NAT-Host-onl