CentOS 7固定IP地址
CentOS 7默认是动态的IP地址,重新开机后,局域网内其它电脑可能就无法连接上。为了方便使用,可以将局域网内CentOS的IP地址固定
使用 ifconfig 命令查看网卡的IP信息
如果出现以下提示,则需要先安装 net-tools
[root@localhost ~]# ifconfig -bash: /usr/sbin/ifconfig: No such file or directory
执行yum install net-tools.x86_64 -y
[root@localhost ~]# yum install net-tools.x86_64 -y Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.cn99.com * updates: mirrors.cn99.com Resolving Dependencies --> Running transaction check ---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================================================================================================================================= Package Arch Version Repository Size ================================================================================================================================================================================================================================================= Installing: net-tools x86_64 2.0-0.25.20131004git.el7 base 306 k Transaction Summary ================================================================================================================================================================================================================================================= Install 1 Package Total download size: 306 k Installed size: 917 k Downloading packages: net-tools-2.0-0.25.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.25.20131004git.el7.x86_64 1/1 Verifying : net-tools-2.0-0.25.20131004git.el7.x86_64 1/1 Installed: net-tools.x86_64 0:2.0-0.25.20131004git.el7 Complete!
再次执行使用 ifconfig
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.24.60.126 netmask 255.255.240.0 broadcast 172.24.63.255
inet6 fe80::28d4:8a44:27e3:145e prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:02:f7:00 txqueuelen 1000 (Ethernet)
RX packets 70332 bytes 90125570 (85.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 25809 bytes 2427083 (2.3 MiB)
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 68 bytes 5916 (5.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68 bytes 5916 (5.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:c0:e1:09 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
确认需要固定IP的网卡名称为eth0。
输入命令 netstat -rn 查看Gateway
[root@localhost ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 172.24.48.1 0.0.0.0 UG 0 0 0 eth0 172.24.48.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
确定Gateway为 172.24.48.1。
执行vi /etc/sysconfig/network-scripts/ifcfg-[网卡名] ,编辑网卡配置文件
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 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="eth0" UUID="bb6ba495-1126-44e5-9ea3-09d07edd2d1c" DEVICE="eth0" ONBOOT="yes"
按 i 键开始编辑。
修改为以下配置
TYPE="Ethernet" PROXY_METHOD="none" BROWSER_ONLY="no" BOOTPROTO="static" 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="eth0" UUID="bb6ba495-1126-44e5-9ea3-09d07edd2d1c" DEVICE="eth0" ONBOOT="yes" IPADDR="172.24.60.126" NETMASK="255.255.240.0" GATEWAY="172.24.48.1" DNS1="223.5.5.5" DNS2="114.114.114.114"
按下 Esc 键结束编辑,输入 :wq ,回车键保存并退出

输入systemctl restart network.service 命令,重启网络
[root@localhost ~]# systemctl restart network.service
ping命令测试一下网络可用
[root@localhost ~]# ping www.baidu.com PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data. 64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=1 ttl=51 time=5.86 ms 64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=2 ttl=51 time=7.86 ms 64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=3 ttl=51 time=7.15 ms 64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=4 ttl=51 time=6.41 ms
至此固定IP地址已经设置完成了

浙公网安备 33010602011771号