Linux IP和网关配置

   本文主要描述Linux服务器上IP配置方法以及一些经常使用指令的介绍。包括:服务器

  一、Linux服务器永久IP配置,经过修改配置实现,包括默认网关配置。服务器或网络服务重启不失效。网络

  二、Linux临时IP配置,经过ipconfig命令实现,服务器或网络服务重启失效。测试

  三、ifconfig命令经常使用方法介绍,如查看网卡和IP、浮动IP配置、网卡重启等。spa

  四、route、ethtool等与IP、路由配置有关指令的使用方法介绍。
rest

操做环境

  SuSE11/SuSE10code

永久IP配置<永久有效,重启不失效>

   经过修改/etc/sysconfig/network/ifcfg-eth*文件直接配置,服务器重启不失效,建议使用。说明:SuSE10的配置文件名称为ifcfg-${MAC ADDRESS}blog

  一、使用root用户登陆服务器,肯定配置网卡。一般有几种状况:接口

     (1) 若是是修改服务器IP。这种状况下服务器上已经配置了IP。执行ifconfig查看IP配置在哪一个网卡上。ip

     (2) 若是是新装系统的新服务器而且只有一张网卡。那就直接配吧。好比云服务器,一般为eth0。ci

     (3)若是是新装系统多个网卡呢,可使用ethtool命令查找可用的网卡。ethtool ethX | grep 'Link detected',查看连接状态为Link detected: yes。若是不是很肯定,经过配置多试试问题也不大。

  二、进入/etc/sysconfig/netwrok目录,这里假设是eth0网卡,修改ifcfg-eth0文件。设置内容以下:

BOOTPROTO='static' IPADDR='192.168.0.1' NETMASK='255.255.255.0' STARTMODE='auto'

  三、配置默认网关,经过修改配置文件/etc/sysconfig/netwrok/routes实现,添加内容以下

default 192.168.1.1 - -

  四、执行service network restart重启网卡服务。使上述修改生效。

Linux:~ # service network restart

  五、ifconfig -a查看eth0网卡的IP,若是正常,继续下一步,不然检查网卡是否正常

  六、测试。从本地ping IP或者登陆系统,成功则配置完成。

临时IP配置方法二<临时生效,重启失效>

  使用ifconfig命令进行配置,临时IP有个问题就是在系统重启后配置失效,因此该方法一般用于临时使用IP。经常使用于配置浮动IP,不建议配置固定IP。

  一、查找网卡方法同 配置方法一中的步骤1,假设须要配置的网卡为eth0,配置IP为192.168.0.1,子网掩码为255.255.255.0

ifconfig eht0 192.168.0.1 netmask 255.255.255.0 up

  二、默认网关配置

route add default gw 192.168.1.1

  三、测试。从本地ping IP或者登陆系统,成功则配置完成。

IP配置相关知识扩展

ifconfig命令使用方法

  ifconfig命令经常使用方法(非root用户执行使用全路径/sbin/ifconfig)

  一、查看主机全部网卡信息

ifconfig -a

  二、启动和关闭网卡。说明:关闭网卡能够清除使用ifconfig配置的临时IP。

ifconcifg eth0 up ifconfig eth0 down

  三、使用ifconfig配置IP地址

 ifconfig eth0 192.168.0.1
 
 ifconfig eth0 192.168.0.1 netmask 255.255.255.0 broadcast 192.168.1.255

 四、在一张网卡上配置多个IP。经常使用语配置浮动IP。其中示例中的eth0:0也能够是eth0:一、eth0:2。

 ifconfig eth0:0 192.168.0.2 netmask 255.255.255.0

ethtool命令使用方法

  ethtool命令经常使用于查询和设置网卡参数

一、查询网卡参数

# ethtool eth0 Settings for eth0: Supported ports: [ TP ] Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Supports auto-negotiation: Yes Advertised link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Advertised auto-negotiation: Yes Speed: 1000Mb/s Duplex: Full Port: Twisted Pair PHYAD: 0 Transceiver: internal Auto-negotiation: on Supports Wake-on: umbg Wake-on: d Link detected: yes

  二、查询eth0网口收发包统计

ethtool –S eth0

  三、设置网口速率、网口半/全双工、是否自协商

ethtool –s eth0 [speed 10|100|1000] [duplex half|full]  [autoneg on|off]

 route命令使用方法

  一、打印路由表

$ route -n <--参数n表示输出信息不打印主机名而是直接打印IP地址 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface10.135.0.0      0.0.0.0         255.255.192.0   U     0      0        0 eth0 169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth1 173.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth2

  输出说明:

Destination 目标网络或目标主机。 Gateway 网关地址,若是没有就显示* Genmask 网络掩码,'0.0.0.0'表示默认路由 Flags:标志,表明的含义以下: U (route is up):该路由是启动的; H (target is a host):目标是一部主机 (IP) 而非网域; G (use gateway):须要透过外部的主机 (gateway) 来转递封包; R (reinstate route for dynamic routing):使用动态路由时,恢复路由资讯的旗标; D (dynamically installed by daemon or redirect):已经由服务或转 port 功能设定为动态路由 M (modified from routing daemon or redirect):路由已经被修改了; Metric 距离、跳数 Ref 不用管,恒为0。 Use 该路由被使用的次数,能够粗略估计通向指定网络地址的网络流量。 Iface 接口,即eth0,eth0等网络接口名

  路由顺序(查看输出路由打印输出,小网域到大网域,最后是默认路由):

  (1)判断10.135.0.0/18,找到,经过eth0出去,未找到,下一步

  (2)判断169.254.0.0/16,找到,经过eth1出去,未找到,下一步

  (3)判断173.18.0.0/16,找到,经过eth2出去,未找到,分析缘由是否配置错误进行修改。

   二、增长到主机路由信息

route add -host 192.16.128.1 dev eth1 route add -host 192.16.128.1 gw 192.16.128.254

  三、添加到网络的路由

route add -net 192.16.128.1 netmask 255.255.0.0 dev eth1 route add -net 192.16.128.1 gw 192.16.128.254 route add -net 192.16.128.1/23 gw 192.16.128.254

  四、添加默认网关

route add default gw 192.16.128.254

  五、删除路由信息

 route del -net 192.168.0.1 netmask 255.255.0.0 dev eth0
相关文章
相关标签/搜索