您的位置:首页 > 运维架构 > Linux

Linux IP和网关配置

2017-09-17 08:30 597 查看

操作环境

SuSE11/SuSE10

配置方法一<永久有效,重启不失效>

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

1、确定配置网卡。通常有几种情况:

(1) 修改IP。这种情况下通常已配置IP。这可以直接确定配置网卡。

(2) 新服务器只有一张网卡。可以直接确定。比如云服务器,通常直接为eth0。

(3)使用ethtool命令查找可用的网卡。ethtool ethX | grep 'Link detected',查看链接状态为Link detected: yes。

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

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


3、网关配置,修改配置文件/etc/sysconfig/netwrok/routes,内容如下

default 192.168.1.1 - -


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

Linux:~ # service network restart


5、ifconfig -a查看eth0网卡的IP,如果正常,继续下一步,否则检查网卡是否正常

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

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

使用ifconfig命令进行配置,该方法在系统重启后配置失效,临时使用IP。常用于配置浮动IP,不建议配置固定IP

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

ifconfig eht0 192.168.0.1 netmask 255.255.255.0 up


2、网关配置

route add default gw 192.168.1.1


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

知识扩展

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

1、查看主机所有网卡信息

ifconfig -a


2、启动和关闭网卡

ifconcifg eth0 up
ifconfig eth0 down


3、配置IP地址

ifconfig eth0 192.168.0.1

ifconfig eth0 192.168.0.1 netmask 255.255.255.0 broadcast 192.168.1.255


4、在一张网卡上配置多个IP。常用语配置浮动IP。其中示例中的eth0:0也可以是eth0:1、eth0:2。

ifconfig eth0:0 192.168.0.2 netmask 255.255.255.0


ethtool常用命令,常用语查询和设置网卡参数

1、查询网卡参数

# 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


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

ethtool –S eth0


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

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


route命令

1、打印路由表

$ 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出去,未找到,分析原因是否配置错误进行修改。

2、增加路由信息

route add -net 192.168.0.1 netmask 255.255.255.0 dev eth0


3、删除路由信息

route del -net 192.168.0.1 netmask 255.255.0.0 dev eth0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: