重庆思庄Oracle、Redhat认证学习论坛
标题: Linux IP 修改 (临时 + 永久) [打印本页]
作者: jiawang 时间: 2017-12-19 10:43
标题: Linux IP 修改 (临时 + 永久)
本帖最后由 jiawang 于 2017-12-21 14:34 编辑
[oracle@strong ~]$ ifconfig (查看当前ip地址)
eth0 Link encap:Ethernet HWaddr 00:0C:29:15:72:37
inet addr:192.168.133.144 Bcast:192.168.133.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe15:7237/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3949439 errors:6 dropped:6 overruns:0 frame:0
TX packets:2121967 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1171501105 (1.0 GiB) TX bytes:318589224 (303.8 MiB)
Interrupt:19 Base address:0x2000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:557391 errors:0 dropped:0 overruns:0 frame:0
TX packets:557391 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:135841168 (129.5 MiB) TX bytes:135841168 (129.5 MiB)
这里的是eth0,可以看到详细的网卡信息,包括网卡类型(以太网网卡),硬件地址,IP地址,IP广播地址,子网掩码等。
我们要注意两个内容,第一:inet addr:xxx.xxx.xxx.xxx(这是IPV4地址)inet6 addr:xxxxxxxxxxxxxxx(这是IPV6地址),第二我们要注意mask后面的内容:xxx.xxx.xxx.xxx这是子网掩码。
临时修改ip
例如我们要修改IP地址为192.168.133.131,子网掩码为255.255.255.0
那么我们只需要ifconfig eth0 192.168.133.131 netmask 255.255.255.0即可
[root@strong ~]# ifconfig eth0 192.168.133.131 netmask 255.255.255.0
验证:
[root@strong ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:15:72:37
inet addr:192.168.133.131 Bcast:192.168.133.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe15:7237/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3949616 errors:6 dropped:0 overruns:0 frame:0
TX packets:2122296 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1171518744 (1.0 GiB) TX bytes:318617416 (303.8 MiB)
Interrupt:19 Base address:0x2000
注意:直接用ifconfig ip 这种方式修改的话是临时的,重启系统或者重启服务之后失效。
[root@strong ~]# reboot
Broadcast message from root@strong
(/dev/pts/4) at 14:31 ...
The system is going down for reboot NOW!
[root@strong ~]#
[root@strong ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:15:72:37
inet addr:192.168.133.144 Bcast:192.168.133.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe15:7237/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:62 errors:0 dropped:0 overruns:0 frame:0
TX packets:54 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:7418 (7.2 KiB) TX bytes:7489 (7.3 KiB)
Interrupt:19 Base address:0x2000
永久修改:
如果永久修改的话,修改以下配置文件:vim /etc/sysconfig/network-scripts/ifcfg-eth0
修改配置文件信息:进入配置文件之后按"i" 进行编辑
[root@strong ~]# cd /etc/sysconfig/network-scripts/
[root@strong network-scripts]# ls
ifcfg-eth0 ifdown-ippp ifdown-routes ifup-bnep ifup-plip ifup-sit network-functions
ifcfg-lo ifdown-ipv6 ifdown-sit ifup-eth ifup-plusb ifup-tunnel network-functions-ipv6
ifdown ifdown-isdn ifdown-tunnel ifup-ippp ifup-post ifup-wireless
ifdown-bnep ifdown-post ifup ifup-ipv6 ifup-ppp init.ipv6-global
ifdown-eth ifdown-ppp ifup-aliases ifup-isdn ifup-routes net.hotplug
[root@sztech network-scripts]# vi ifcfg-eth0
DEVICE=eth0 /网卡名字 不用改
HWADDR=00:0C:29:15:72:37
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static /默认为dhcp 修改为static
IPADDR=192.168.133.131 /ip 地址
NETMASK=225.255.255.0 /子网掩码
GATEWAY=192.168.133.2 /网关地址