Linux 修改IP Address

以前都是使用自动IP动态分配获取IP的,虽然每次获得的ip都是相同的,但我还是决定自己设置一个IP。输入命令:
[root@localhost ~]# ifconfig eth0 219.246.177.160 netmask 255.255.255.0 up


ping
以下177网段的ip是可以ping通的,但是ping别的网段无法ping通
[root@localhost ~]# ping 219.246.183.1
connect: Network is unreachable

Java代码 复制代码  收藏代码
  1. [root@localhost ~]# route   
  2. Kernel IP routing table   
  3. Destination     Gateway         Genmask         Flags Metric Ref    Use     Iface   
  4. 219.246.177.0   *               255.255.255.0   U     0      0        0      eth0  
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use     Iface
219.246.177.0   *               255.255.255.0   U     0      0        0      eth0

 

没有设置默认网关。设置网关
[root@localhost ~]# route add default gw 219.246.177.1

route add default gw 61.144.47.225


再查看路由表:

Java代码 复制代码  收藏代码
  1. [root@localhost ~]# route   
  2. Kernel IP routing table   
  3. Destination     Gateway         Genmask         Flags Metric Ref    Use     Iface   
  4. 219.246.177.0   *               255.255.255.0   U     0      0        0     eth0   
  5. default         219.246.177.1   0.0.0.0         UG    0      0        0     eth0  
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use     Iface
219.246.177.0   *               255.255.255.0   U     0      0        0     eth0
default         219.246.177.1   0.0.0.0         UG    0      0        0     eth0

 添加上了默认网关。此时,再ping一下别的网段就可以ping通了。
但是这样的修改,重新启动后就无效了。要让修改的ip永久生效就必须修改配置文件/etc/sysconfig/network-scrips/ifcfg-eth0。因为机子启动的时候加载的就是这个文件的配置参数。对这个文件进行修改:

Java代码 复制代码  收藏代码
  1. [root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0   
  2. DEVICE=eth0                    #网络设备名称,与ifcfg-eth0对应   
  3. #BOOTPROTO=dhcp                #启动eth0接口时使用动态IP地址分配                                                                     
  4.                                #这里使用的是静态IP地址,因此将这句屏蔽掉   
  5. BOOTPROTO=static               #启动eth0接口时使用静态IP地址分配   
  6. HWADDR=00:E0:4C:61:71:E3       #MAC地址   
  7. IPADDR=219.246.177.160         #设置IP地址   
  8. NETMASK=255.255.255.0          #设置子王掩码   
  9. GATEWAY=219.246.177.1          #设置网关   
  10. ONBOOT=yes                     #开机时启动   
  11.    
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0                    #网络设备名称,与ifcfg-eth0对应
#BOOTPROTO=dhcp                #启动eth0接口时使用动态IP地址分配                                                                  
                               #这里使用的是静态IP地址,因此将这句屏蔽掉
BOOTPROTO=static               #启动eth0接口时使用静态IP地址分配
HWADDR=00:E0:4C:61:71:E3       #MAC地址
IPADDR=219.246.177.160         #设置IP地址
NETMASK=255.255.255.0          #设置子王掩码
GATEWAY=219.246.177.1          #设置网关
ONBOOT=yes                     #开机时启动
 

 

Java代码 复制代码  收藏代码
  1. DEVICE=eth0   
  2.   
  3. BOOTPROTO=none   
  4.   
  5. HWADDR=00:13:D3:90:95:4D   
  6.   
  7. ONBOOT=yes   
  8.   
  9. TYPE=Ethernet   
  10.   
  11. NETMASK=255.255.255.248  
  12.   
  13. IPADDR=61.144.47.228  
  14.   
  15. USERCTL=no   
  16.   
  17. IPV6INIT=no   
  18.   
  19. PEERDNS=yes   
  20.   
  21. IGATEWAY=61.144.47.225  
  22.   
  23. GATEWAY=61.144.47.225  
DEVICE=eth0

BOOTPROTO=none

HWADDR=00:13:D3:90:95:4D

ONBOOT=yes

TYPE=Ethernet

NETMASK=255.255.255.248

IPADDR=61.144.47.228

USERCTL=no

IPV6INIT=no

PEERDNS=yes

IGATEWAY=61.144.47.225

GATEWAY=61.144.47.225

 

修改好后,要想将修改的配置不用重启而生效。输入命令
[root@localhost ~]# ifdown eth0


将eth0网络接口关闭,再输入下面的命令将eth0接口重新启动
[root@localhost ~]# ifup eth0

这个命令会到 /etc/sysconfig/network-scripts/下读取相应参数,使刚配置内容生效。

或者输入
[root@localhost ~]# /etc/init.d/network restart


重新启动整个网络的参数。/etc/init.d/network这个文件也是开机时加载的文件。

这样就可以解决IP设置的问题拉。如果需要设置DNS,则编辑/etc/resolv.conf文件

Java代码 复制代码  收藏代码
  1. [root@localhost ~]# vi /etc/resolv.conf   
  2. nameserver 202.201.0.131  
  3. nameserver 202.201.0.132  
  4. nameserver 202.201.89.131  
  5. search lzu.edu.cn  
[root@localhost ~]# vi /etc/resolv.conf
nameserver 202.201.0.131
nameserver 202.201.0.132
nameserver 202.201.89.131
search lzu.edu.cn

 配置好后当然还得输入[root@localhost ~]# /etc/init.d/network restart,重新启动整个网络的参数。才能生效。

猜你喜欢

转载自ponymwt.iteye.com/blog/1638122
今日推荐