标签:linux 网络配置
Linux的IP配置主要分成这几部分
IP配置, 路由配置, 网络管理工具
IP配置
查看IP配置信息
ifconfig: 查看所有活动的网络接口
ifconfig -a: 查看所有活动和非活动的网络接口
ipconfig 接口: 查看指定接口信息
[root@centos ~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:06:5B:59 inet addr:192.168.16.208 Bcast:192.168.16.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe06:5b59/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:224 errors:0 dropped:0 overruns:0 frame:0 TX packets:141 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:19829 (19.3 KiB) TX bytes:18047 (17.6 KiB)
启用和禁用接口
关闭接口: ifdown 接口
打开接口: ifup 接口
配置ip地址
ipconfig 接口 指定地址, 指定地址的配置有两种格式, 1 地址 netmask 掩码 2. 地址/掩码
[root@centos ~]# ifconfig eth0 192.168.16.209 netmask 255.255.255.0 #第一种配置 [root@centos ~]# ifconfig eth0 192.168.16.208/24 #第二种配置
但是上面的配置方法只是当在系统或服务部重启的时候有效, 如果要配置永久有效,需要修改网络接口相应的配置文件/etc/sysconfig/network-scripts/ifcfg-接口名
下面的当前主机eth的接口配置文档信息/etc/sysconfig/network-scripts/ifcfg-eth0
[root@centos ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 配置文件关联设备, 要与ifcfg-后面名称一致 HWADDR=00:0C:29:06:5B:59 接口MAC可以没有 TYPE=Ethernet 接口类型 UUID=240d5656-bea9-4ea8-acd4-b1bcd715b724 接口设备的唯一id号 ONBOOT=yes 开机是否自动激活接口 NM_CONTROLLED=yes NetworkManager时候可以配置此接口 BOOTPROTO=static 接口获取ip类型可以是 dhcp, static, none IPADDR=192.168.16.208 ip地址 NETMASK=255.255.255.0 子网掩码 GATEWAY=192.168.16.2 网关 DNS1=8.8.8.8 dns,最多可以3个, DNS1, DNS2, DNS3 IPV6INIT=no 是否允许v6协议 USERCTL=no 时候允许普通用户配置此接口 PEERDNS=no 当dhcp服务打开时, 是否接口dhcp配置dns, 也就是修改/etc/resolv.conf文件
修改完此文件, 需要重启系统或服务(service network restart, systemctl restart network), 来使之生效.
单接口配置多个ip, 需要在物理接口上添加多个虚拟接口, 虚拟接口有固定格式, 例如eth0的虚拟接口是eth0:0, eth0:1, eth0:2等.
虚拟接口暂时生效
ifconfig 虚拟接口 网络地址
[root@centos ~]# ifconfig eth0:2 192.168.16.211/24 [root@centos ~]# ping 192.168.16.211 PING 192.168.16.211 (192.168.16.211) 56(84) bytes of data. 64 bytes from 192.168.16.211: icmp_seq=1 ttl=64 time=0.021 ms
永久有效需要创建新的配置文件, 再手动添加所有参数. 如上面例子, 需要创建接口/etc/sysconfig/network-scripts/ifcfg-eth0:2, 配置文件内的第一行DEVICE=eth0:2, 其他可以根据需要自己配置
路由配置
查看路由信息
route 或 route -n, -n的作用是不需要dns反解, 最好加上速度会快一些.
[root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.16.2 0.0.0.0 UG 100 0 0 eno16777736 192.168.16.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736D
Destination: 目标地址, 0.0.0.0表示任意地址, 也就是说这行是网关信息
Gateway: 网关地址, 0.0.0.0表示无网关, 也即是这行是本地网络地址,不需要走网关
Genmask: 目标地址的子网掩码
Flags: 标志符, U:up, G:网关, H:主机路由
Metric: 优先级
Iface: 通过的接口
添加主机路由
route add -host 目标主机地址 gw 通过网关 dev 通过接口, 如果只有一个接口 dev可以省略
[root@localhost ~]# route add -host 10.1.1.1 gw 192.168.16.100 dev eno16777736 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.16.2 0.0.0.0 UG 100 0 0 eno16777736 10.1.1.1 192.168.16.100 255.255.255.255 UGH 0 0 0 eno16777736 192.168.16.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
添加网络路由
route add -net 网络地址 gw 通过网关 dev 通过接口
[root@localhost ~]# route add -net 10.2.0.0/16 gw 192.168.16.101 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.16.2 0.0.0.0 UG 100 0 0 eno16777736 10.1.1.1 192.168.16.100 255.255.255.255 UGH 0 0 0 eno16777736 10.2.0.0 192.168.16.101 255.255.0.0 UG 0 0 0 eno16777736 192.168.16.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
添加网关
route add default gw 网关 或 route add -net 0.0.0.0 gw 网关, 如果已有网关需要先删除原来的.
[root@localhost ~]# route add default gw 192.168.16.112
删除主机路由
route del -host 主机ip
删除网络路由
route del -net 子网地址
删除网关
rounte del -net 0.0.0.0 或者 route del default gateway 网关
[root@centos ~]# route del default gw 192.168.16.2
如果要路由信息永久有效, 需要在/etc/sysconfig/network-scripts/下面对相应的接口创建文件,格式: route-接口名, 例如, route-eth0
配置文件格式, 一行一个路由,
目标地址 via 下一条
[root@centos network-scripts]# cat /etc/sysconfig/network-scripts/route-eth0 10.0.0.0/8 via 192.168.16.20 10.0.0.1 via 192.168.16.21
几个网络管理工具
ping:
-c #:指定发送报文的个数
-w #: 指定一个ping命令执行多长时间, 无论发送多少个报文, 单位是秒
-W #: 等待一个报文返回的时长, 单位是秒
[root@localhost ~]# ping -c 1 -w 1 192.168.16.16 PING 192.168.16.16 (192.168.16.16) 56(84) bytes of data. --- 192.168.16.16 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms [root@localhost ~]# ping -c 1 -w 1 192.168.16.2 PING 192.168.16.2 (192.168.16.2) 56(84) bytes of data. 64 bytes from 192.168.16.2: icmp_seq=1 ttl=128 time=0.358 ms 发送一个报文, 不管成功与否1秒后返回结果
traceroute: 查询到目标地址所经过的所有网关
tranceroute 目标ip
显示一个
netstat 显示网络状态信息
-t : tcp会话
-u: udp会话
-n: 数字格式显示
-l: 监听状态
-a: 所有状态, 监听和非监听状态
-p: 会话进程及程序编号
-r: 路由表
#显示左右监听进程 [root@localhost ~]# netstat -tln Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN
标签:linux 网络配置
原文地址:http://jzrobbie.blog.51cto.com/6535329/1699903