iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 7
0
自我挑戰組

30天學 LAMP 安裝系列 第 7

Day07 CentOS 7 文字模式下更改為固定 IP

  • 在 Windows 命令提示字元底下,看 IP 設定第一個想到的指令是 ipconfig,它在 Linux 的對應指令為 ifconfig,可是在 CentOS 7 Minimal 下,這個指令被拿掉了,需要藉由安裝 net-tools 套件才能使用。

      [root@centos7-cli ~]# ifconfig
      -bash: ifconfig: command not found
      [root@centos7-cli ~]# yum install net-tools -y
      Loaded plugins: fastestmirror
      Loading mirror speeds from cached hostfile
       * base: ftp.yzu.edu.tw
       * extras: ftp.yzu.edu.tw
       * updates: ftp.yzu.edu.tw
      Resolving Dependencies
      --> Running transaction check
      ---> Package net-tools.x86_64 0:2.0-0.24.20131004git.el7 will be installed
      --> Finished Dependency Resolution
    
      Dependencies Resolved
    
      ==========================================================================================================================
       Package                    Arch                    Version                                   Repository             Size
      ==========================================================================================================================
      Installing:
       net-tools                  x86_64                  2.0-0.24.20131004git.el7                  base                  306 k
    
      Transaction Summary
      ==========================================================================================================================
      Install  1 Package
    
      Total download size: 306 k
      Installed size: 918 k
      Downloading packages:
      net-tools-2.0-0.24.20131004git.el7.x86_64.rpm                                                      | 306 kB  00:00:00
      Running transaction check
      Running transaction test
      Transaction test succeeded
      Running transaction
        Installing : net-tools-2.0-0.24.20131004git.el7.x86_64                                                              1/1
        Verifying  : net-tools-2.0-0.24.20131004git.el7.x86_64                                                              1/1
    
      Installed:
        net-tools.x86_64 0:2.0-0.24.20131004git.el7
    
      Complete!
      [root@centos7-cli ~]# ifconfig
      ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
              inet 192.168.250.102  netmask 255.255.255.0  broadcast 192.168.250.255
              inet6 fe80::b683:4793:d132:95b1  prefixlen 64  scopeid 0x20<link>
              ether 00:0c:29:98:e7:92  txqueuelen 1000  (Ethernet)
              RX packets 9866  bytes 14050976 (13.4 MiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 1829  bytes 130448 (127.3 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 64  bytes 5696 (5.5 KiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 64  bytes 5696 (5.5 KiB)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
      [root@centos7-cli ~]#
    

    由此可看到目前有一張 NIC 網卡 ens32,目前 IP 為 192.168.250.102/24

  • 另外亦可藉由查看檔案,得知目前 IP 設定。

      [root@centos7-cli ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32
      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="ens32"
      UUID="2ddf3f8e-f627-4fe7-9b05-bb8a58f506a4"
      DEVICE="ens32"
      ONBOOT="yes"
      [root@centos7-cli ~]#
    

    可看出 ens32 這張網卡,是由 DHCP 取得 IP,開機會自動啟用連線

  • 使用 nmcli device show 指令,我們可以查到更多資訊,包括 Default Gateway 預設閘道與 DNS。

      [root@centos7-cli ~]# nmcli device show
      GENERAL.DEVICE:                         ens32
      GENERAL.TYPE:                           ethernet
      GENERAL.HWADDR:                         00:0C:29:98:E7:92
      GENERAL.MTU:                            1500
      GENERAL.STATE:                          100 (connected)
      GENERAL.CONNECTION:                     ens32
      GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
      WIRED-PROPERTIES.CARRIER:               on
      IP4.ADDRESS[1]:                         192.168.250.102/24
      IP4.GATEWAY:                            192.168.250.254
      IP4.ROUTE[1]:                           dst = 0.0.0.0/0, nh = 192.168.250.254, mt = 100
      IP4.ROUTE[2]:                           dst = 192.168.250.0/24, nh = 0.0.0.0, mt = 100
      IP4.DNS[1]:                             192.168.250.254
      IP4.DOMAIN[1]:                          localdomain
      IP6.ADDRESS[1]:                         fe80::b683:4793:d132:95b1/64
      IP6.GATEWAY:                            --
      IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 100
      IP6.ROUTE[2]:                           dst = ff00::/8, nh = ::, mt = 256, table=255
    
      GENERAL.DEVICE:                         lo
      GENERAL.TYPE:                           loopback
      GENERAL.HWADDR:                         00:00:00:00:00:00
      GENERAL.MTU:                            65536
      GENERAL.STATE:                          10 (unmanaged)
      GENERAL.CONNECTION:                     --
      GENERAL.CON-PATH:                       --
      IP4.ADDRESS[1]:                         127.0.0.1/8
      IP4.GATEWAY:                            --
      IP6.ADDRESS[1]:                         ::1/128
      IP6.GATEWAY:                            --
      [root@centos7-cli ~]#
    
  • 還有一個 nmcli connection show ens32 指令,可以查看更多資訊,這裡就先不 Demo 了(篇幅站太多了)。

  • 整理一下網卡 ens32 資訊,等下要用:

    • IP:將要改為 192.168.250.92/24
    • Default Gateway 與 DNS:192.168.250.254
  • 使用 nmcli connection modify ens32 修改網卡 ens32 為固定 IP 192.168.250.92

      [root@centos7-cli ~]# nmcli connection modify ens32 ipv4.addresses 192.168.250.92/24 ipv4.gateway 192.168.250.254 ipv4.dns 192.168.250.254 ipv4.method manual connection.autoconnect yes
      [root@centos7-cli ~]#
    
  • 接著使用 nmcli connection reload 與 systemctl restart network 指令,重啟網卡,以及 systemctl reboot 重開機。

  • 重開機後,仿照 Day05 使用 ping 指令測試,以及使用以上指令,查看設定是否正確。

  • 其實還有個 nmtui 指令,檢視、設定會更直覺:

    https://ithelp.ithome.com.tw/upload/images/20190909/20119707dmSzQonH5c.png
    https://ithelp.ithome.com.tw/upload/images/20190909/20119707YBBt4xGHl4.png
    https://ithelp.ithome.com.tw/upload/images/20190909/20119707wsMmAcSyvT.png


上一篇
Day06 以 Minimal 進行 CentOS 7 文字模式的基本安裝
下一篇
Day08 安裝完 CentOS 7 後的建議設定
系列文
30天學 LAMP 安裝33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言