一、基本网络配置
1、配置网络
2、实现网络组
3、测试网络
4、网络工具
二、网络配置
1)静态指定
ifcfg:ifconfig,route,netstat ip:object {link,addr,route},ss,setup
centos7新增实用工具:nmcli nmtui
2)动态分配
DHCP: Dynamic Host Configuration Protocol
ifconfig命令:
ifconfig [interface]
ifconfig -a #显示所有网卡信息
ifconfig {eth0|eth1} [up|down] #关闭或开启某网卡
ifconfig interface [aftype] options | address ...
ifconfig IFACE IP/mask [up] #临时添加一个ip地址 如:ifconfig eth0:0 10.1.1.1/16
ifconfig IFACE IP netmask MASK #临时添加一个ip地址 如:ifconfig eth0:0 10.1.1.1 255.255.0.0
注意:此命令立即生效
代码演示:
[email protected] ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:7C:55:97 inet addr:192.168.226.133 Bcast:192.168.226.255 Mask:255.255.255.0 BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:2922 errors:0 dropped:0 overruns:0 frame:0 TX packets:726 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:201495 (196.7 KiB) TX bytes:71868 (70.1 KiB) [[email protected] ~]# ifconfig eth1 down [[email protected] ~]# ifconfig eth1 up [[email protected] ~]# ifconfig eth0:1 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:7C:55:8D inet addr:10.1.1.2 Bcast:10.1.255.255 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 [[email protected] ~]#
三、网路配置常用命令
route命令:
查看路由:route -n
添加路由:route add
route add [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]
示例:route add -net 192.168.1.10/24 gw 10.1.0.1 dev eth0
route add -net 192.168.0.2 netmask 255.255.255.0 gw 172.16.0.1 dev eth0
route add -net 192.168.0.2/24 gw 172.16.0.1 dev eth0
默认路由:route add -net 0.0.0.2 netmask 0.0.0.0 gw 172.16.0.1
route add default gw 172.16.0.1
删除路由:route del
route del 192.168.1.3
route del -net 192.168.0.1 netmask 255.255.255.0
演示:
[[email protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.1.249.1 0.0.0.0 UG 100 0 0 eth0 0.0.0.0 192.168.226.2 0.0.0.0 UG 101 0 0 eth1 10.1.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eth0 192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1 [[email protected] ~]# route add -net 192.168.12.0/24 gw 10.1.249.1 [[email protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.1.249.1 0.0.0.0 UG 100 0 0 eth0 0.0.0.0 192.168.226.2 0.0.0.0 UG 101 0 0 eth1 10.1.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eth0 192.168.12.0 10.1.249.1 255.255.255.0 UG 0 0 0 eth0 192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1 [[email protected] ~]# route add -host 10.1.250.1 gw 10.1.249.1 [[email protected] ~]# route add -host 10.1.24.23 gw 10.1.0.1 [[email protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.1.249.1 0.0.0.0 UG 100 0 0 eth0 0.0.0.0 192.168.226.2 0.0.0.0 UG 101 0 0 eth1 10.1.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eth0 10.1.24.23 10.1.0.1 255.255.255.255 UGH 0 0 0 eth0 10.1.250.1 10.1.249.1 255.255.255.255 UGH 0 0 0 eth0 192.168.12.0 10.1.249.1 255.255.255.0 UG 0 0 0 eth0 192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1 [[email protected] ~]# route del -net 192.168.12.0 netmask 255.255.255.0 [[email protected] ~]# route del -host 10.1.24.23 [[email protected] ~]# route del -host 10.1.250.1 [[email protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.1.249.1 0.0.0.0 UG 100 0 0 eth0 0.0.0.0 192.168.226.2 0.0.0.0 UG 101 0 0 eth1 10.1.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eth0 192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1 [[email protected] ~]#
netstat命令:
netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
选项:
-t:tcp协议相关
-u:udp协议相关
-w:raw socket
-l:处于监听状态listen
-a:所有状态
-n:以数字显示ip和端口
-e:扩展格式
-p:显示相关进程及其pid
常用组合: -tan, -uan, -tnl, -unl
ss命令:
ss [OPTION]... [FILTER]
netstat通过遍历proc来获取socket信息,ss使用netlink与内核tcp_diag 模块通信获取socket信息,效率比netstat高。
选项:
-t:tcp相关协议
-u:udp协议相关
-x:unix sock相关
-l:listen监听状态
-a:所有
-n:数字格式
-p:相关的程序及pid
-e:扩展信息
-m:内存信息
ip命令:
配置linux网络属性:ip - show / manipulate routing, devices, policy routing and tunnels
ip link - network device configuration
set dev IFACE 如:ip link set dev eth0 up/down
ip addr { add | del } IFADDR dev STRING [label LABEL]
ip address flush #清除ip地址 ip address flush dev eth0
ip addr add 192.168.100.1/24 dev eth0 label eth1:1 #添加一条临时ip地址
ip addr del 172.16.100.13/16 dev eth0 label eth0:0 #删除临时添加的ip地址
ip addr flush dev eth0 label eth0:0 #删除eth0这条ip记录
ip route - routing table management
添加路由:ip route add
ip route add TARGET via GW dev IFACE src SOURCE_IP
ip route add 192.168.0.0/24 via 172.16.0.1
ip route add 192.168.1.2 via 172.16.0.1
删除路由:ip route delete target
显示路由:ip route show|list
清空路由:ip route flush # ip route flush dev eth0
四、网络配置文件
1)IP、MASK、GW、DNS相关配置文件: /etc/sysconfig/network-scripts/ifcfg-IFACE
2)路由相关的配置文件: /etc/sysconfig/network-scripts/route-IFACE
/etc/sysconfig/network-scripts/ifcfg-IFACE: 配置文件说明如下
DEVICE:此配置文件应用到的设备
BOOTPROTO:{staic|dhcp|none}激活此设备时使用的地址配置协议
ONBOOT:在系统引导时是否激活此设备
TYPE:接口类型
UUID:设备的惟一标识
HWADDR:mac地址
IPADDR:指明IP地址
NETMASK:子网掩码
GATEWAY: 默认网关
DNS1:第一个DNS服务器指向
DNS2:第二个DNS服务器指向
PEERDNS:如果BOOTPROTO的值为“dhcp”,是否允许 dhcp server分配的dns服务器指向信息直接覆盖至 /etc/resolv.conf文件中
3)本地解析器
解析器执行正向和逆向查询 :/etc/hosts 本地主机名数据库和IP地址的映像 对小型独立网络有用通常,在使用DNS前检查
getent hosts #查看/etc/hosts 内容
4)dns域名解析:/etc/resolv.conf
nameserver dns_server_ip1
nameserver dns_server_ip2
nameserver dns_server_ip3
配置本地解析及域名解析优先级:/etc/nsswitch.conf
5)网卡别名
ifconfig命令: ifconfig eth0:0 192.168.1.100/24 up
ip命令:
ip addr add 192.168.1.2/24 dev eth0
ip addr add 192.168.1.3/24 dev eth0 label eth0:0
ip addr del 192.168.1.3/24 dev eth0 label eth0:0
ip addr flush dev eth0 label eth0:0
6)配置主机名
Linux网络属性配置的tui(text user interface): system-config-network-tui setup 注意:记得重启网络服务方能生效
配置当前主机的主机名: hostname [HOSTNAME] /etc/sysconfig/network HOSTNAME=
7)网卡名称
网络接口识别并命名相关的udev配置文件
/etc/udev/rules.d/70-persistent-net.rules 卸载网卡驱动: modprobe -r e1000 装载网卡驱动: modprobe e1000 (centos6.x)
# PCI device 0x8086:0x100f (e1000) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7c:55:97", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1" # PCI device 0x8086:0x100f (e1000) (custom name provided by external tool) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7c:55:8d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
使用传统命名方式: 在centos7.x中需使用eth0,1,2...表示可修改/etc/boot/grub2/grub.cfg配置文件
五、网络客服端工具
1)文件传输工具及其下载工具
lftp, ftp, lftpget, wget
lftp [-p port] [-u user[,password]] SERVER
子命令: get、 mget、ls、help
2)lftpget URL # lftp 192.168.1.1
3)wget [option]... [URL]...
-q: 静默模式
-c: 断点续传
-O: 保存位置
--limit-rates=: 指定传输速率
如:wget ftp://10.1.0.1/pub/alren/xiaoerduowget
http://www.xiaoerduo/pub/alren/xiaoerduo