linux网络管理基础
一.网络管理基础
Network interface names
Traditionally, network interfaces in Linux are enumerated as eth0, eth1, eth2, and so on. However, the mechanism which
sets these names can cause changes to which interface gets which name as devices are added and removed.
The default naming behavior in Red Hat Enterprise Linux 7 is to assign fixed names based on firmware, device topology,
and device type.
Interface names have the following characters:
Ethernet interfaces begin with en(以太网接口), WLAN interfaces begin with wl(wifi接口) and WWAN interfaces begin with ww(WAN口).
Not used by default but also available to administrators, an x is used to incorporate a MAC address.
Finally, a number N is used to represent an index, ID, or port.
If the fixed name cannot be determined, the traditional names such as ethN will be used.
For example, the first embedded network interface may be named eno1 (。。。)and a PCI card network interface may be named enp2s0.(。。。)
The new names make it easier to distinguish the relationship between a port and its name if the user knows both, but the trade off is that users cannot assume a system with one interface calls that interface eth0.
使用NetworkManager管理网络
网络管理器(NetworkManager)是一个动态网络的控制器与配置系统,它用于当网络设备可用时保持设备和连接开启并激活
默认情况下,CentOS/RHEL 7 已安装网络管理器,并处于启用状态。
device 设备,物理设备 例如enp2s0,virbr0,team0
connection 连接,逻辑设置 指的是一套具体配置方案
多个connection可以应用到同一个device,但同一时间只能启用其中一个connection。
这样的好处是针对一个网络接口,可以设置多个网络连接,比如静态IP和动态IP,再根据需要up相应的connection
NetworkManager提供的工具
nmcli, nmtui
-
方法一: 修改ip/netmask/dns/gateway
[root@localhost ~]# nmcli connection add con-name enp2s0-auto autoconnect yes ifname enp2s0 type ethernet //ifname 设备要提前存在 [root@localhost ~]# nmcli con up esn33-auto //启动设备 [root@localhost ~]# nmcli connection add con-name enp2s0-gun autoconnect yes ifname enp2s0 type ethernet ip4 10.10.10.10/24 gw4 10.10.10.254 [root@localhost ~]# nmcli connection show enp2s0-auto |grep ipv4.me ipv4.method: auto [root@localhost ~]# nmcli connection show enp2s0-gun |grep ipv4.me ipv4.method: manual [root@localhost ~]# ls /etc/sysconfig/network-scripts/ ifcfg-enp2s0 ifdown-ippp ifdown-TeamPort ifup-ipv6 ifup-Team ifcfg-enp2s0-auto ifdown-ipv6 ifdown-tunnel ifup-isdn ifup-TeamPort ifcfg-enp2s0-gun ifdown-isdn ifup ifup-plip ifup-tunnel [root@localhost ~]# nmcli connection up enp2s0-auto [root@localhost ~]# nmcli connection delete enp2s0-auto [root@localhost ~]# nmcli con NAME UUID TYPE DEVICE docker0 11993836-f097-43cb-9793-224d5d034539 bridge docker0 ens33 1c64f1da-39d9-4280-9a04-916189241340 802-3-ethernet ens33 ens33-auto ed37c13d-b964-4190-84d3-e853aa58828d 802-3-ethernet -- [root@localhost ~]# nmcli connection add con-name enp2s0-gun autoconnect no ifname enp2s0 type ethernet ip4 192.168.62.251/24 gw4 192.168.62.2 [root@localhost ~]# nmcli con show enp2s0-gun|grep ipv4.method ipv4.method: manual [root@localhost ~]# nmcli connection modify enp2s0-gun ipv4.method a //auto [root@localhost ~]# nmcli con show ens33-gun|grep ipv4.method ipv4.method: auto [root@localhost ~]# nmcli connection modify enp2s0-gun ipv4.method m //manul [root@localhost ~]# nmcli con show ens33-gun|grep ipv4.method ipv4.method: manual [root@localhost ~]# nmcli connection modify enp2s0-gun +ipv4.addresses 20.20.20.20/24 [root@localhost ~]# nmcli con show ens33-gun|grep ipv4.address [root@localhost ~]# nmcli connection modify enp2s0-gun +ipv4.dns 114.114.114.114 [root@localhost ~]# nmcli con show ens33-gun|grep dns
-
方法二:修改ip/netmask/dns/gateway
[root@youngfit ~]# vim /etc/sysconfig/network-scripts/ifcfg-enp2s0 NAME="enp2s0" //名称 DEVICE="enp2s0" //设备名称 ONBOOT=yes //开机启动 NETBOOT=yes BOOTPROTO=none //手动设置(dhcp为自动获取) IPADDR=172.16.120.246 PREFIX=24 GATEWAY=172.16.120.254 DNS1=8.8.8.8 [root@youngfit ~]# nmcli con reload [root@youngfit ~]# nmcli con down enp2s0; nmcli con up enp2s0
注:删除自建的连接connection
不使用NetworkManager管理网络 aliyun
[root@aliyun ~]# nmcli connection delete enp2s0-auto [root@aliyun ~]# systemctl disable NetworkManager [root@aliyun ~]# systemctl stop NetworkManager [root@aliyun ~]# systemctl status NetworkManager [root@aliyun ~]# nmcli con Error: NetworkManager is not running.
[root@aliyun ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 NAME=eth0 DEVICE=eth0 ONBOOT=yes NETBOOT=yes BOOTPROTO=none IPADDR=192.168.122.169 PREFIX=24 TYPE=Ethernet [root@aliyun ~]# systemctl restart network.service
以下配置跟NetworkManager无关:
-
修改主机名/etc/hostname
# hostnamectl set-hostname youngfit
-
1. # vim /etc/hostname # reboot
-
配置名字解析Configuring name resolution
a. hosts[root@localhost ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 119.75.218.70 www.baidu.com [root@localhost ~]# getent hosts www.baidu.com 119.75.218.70 www.baidu.com
b. /etc/resolv.conf //DNS配置文件 [root@youngfit ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 192.168.62.2
二.Generated by NetworkManager
nameserver 114.114.114.114
[root@localhost ~]# yum -y install bind-utils //bind是linux系统下的一个DNS服务程序.bind-utils是bind软件提供的一组DNS工具包,里面有一些DNS相关的工具.主要有:dig,host,nslookup,nsupdate.使用这些工具可以进行域名解析和DNS调试工作. [root@youngfit ~]# host www.sina.com www.sina.com is an alias for us.sina.com.cn. us.sina.com.cn is an alias for wwwus.sina.com. wwwus.sina.com has address 66.102.251.33
基本的网络测试工具
[root@localhost ~]# ip a //查看主机ip地址 [root@localhost ~]# ip a s eth0 //查看某个网卡的信息(ip a show eth0) [root@localhost ~]# ip route //查看路由表条目 [root@localhost ~]# ip neigh //查看邻居的mac地址(同一网段所有主机的mac地址) [root@localhost ~]# hostname //查看主机名 [root@localhost ~]# ping www.baidu.com //检测是否能连通互联网 [root@localhost ~]# ping -c2 www.baidu.com //只ping两次 [root@localhost ~]# traceroute www.baidu.com [root@localhost ~]# tracepath www.baidu.com
ports and services(端口和服务)
以下为示例服务:
[root@aliyun ~]# yum -y install httpd [root@aliyun ~]# systemctl start httpd
http 80/tcp
ssh 22/tcp
ftp 21/tcp
ss命令 -n 不显示服务名称 -a 显示所有信息 -t 显示tcp链接服务 l 显示监听的端口 [root@localhost # ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:111 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::111 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
[root@aliyun ~]# ss -tnl |grep :80
:
[root@aliyun ~]# ss -tnl |grep :21
[root@localhost]# ss -atn //a表示所有状态。可能是listen,也可能是已连接的 State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:111 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* ESTAB 0 0 192.168.62.136:22 192.168.62.1:5925 ESTAB 0 36 192.168.62.136:22 192.168.62.1:6464 ESTAB 0 0 192.168.62.136:22 192.168.62.1:6466 ESTAB 0 0 192.168.62.136:22 192.168.62.1:5258 ESTAB 0 0 192.168.62.136:22 192.168.62.1:5926 ESTAB 0 0 192.168.62.136:22 192.168.62.1:6467 LISTEN 0 128 :::111 :::* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
[root@aliyun ~]# ss -atn |grep :22 LISTEN 0 128 *:22 *:* ESTAB 0 52 114.215.71.214:22 123.120.38.233:49603 [root@aliyun ~]# ss -atn |grep :80 LISTEN 0 128 *:80 *:* ESTAB 0 0 114.215.71.214:40962 140.205.140.205:80 a 所有状态,包括 LISTEN
[root@localhost ~]# netstat -antlp|grep httpd tcp6 0 0 :::80 :::* LISTEN 3281/httpd
临时配置网络信息(了解) ip/netmask(ip地址/子网掩码) ip addr add dev eth1 3.3.3.3/24 ip addr del dev eth1 3.3.3.3/24 gateway(网关) ip route del default ip route add default via 192.168.122.3 ip route add 10.10.10.0/24 via 192.168.122.5 hostname(主机名称) hostname youngfit.cc.com
配置文件: # vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 设备名称 【NAME="System eth0" 设备名称】 可以不存在 BOOTPROTO=none 启动协议 获取ip地址的方式 static、none ==>静态获取 dhcp 动态 NM_CONTROLLED=no 默认是yes 关闭network-manager ONBOOT=yes 开机启动 TYPE=Ethernet 以太网类型 HWADDR=00:0c:29:8e:a5:d3 mac地址 IPADDR=172.16.80.252 ip地址 NETMASK=255.255.0.0 掩码 #PREFIX =24 掩码 GATEWAY=172.16.80.1 网关 DNS=114.114.114.114 //dns domain name server 域名解析
帮助文档:
#vim /usr/share/doc/initscripts-9.49.39/sysconfig.txt
网关:网络的关卡(出口),一般设置到路由器上
IP转发:
临时
echo 1 > /proc/sys/net/ipv4/ip_forward
永久
vim /etc/sysctl.conf 修改内核参数
net.ipv4.ip_forward = 1
sysctl -p //立即生效
dns : 域名服务器,其中一种是解析域名到Ip地址
dns配置文件(客户端)
# vim /etc/resolv.conf nameserver 172.16.70.250
扫描ip
# nmap -v -sP 172.16.70.0/24
扫描端口
# nmap -v -A 172.16.70.0/24
重启网络服务:
# systemctl restart network //rhel7 #/etc/init.d/network restart //rhel5/6 #service network restart //rhel5/6
三. 配置静态路由
linux下静态路由修改命令
方法一: 添加路由 route add -net 192.168.0.0/24 gw 192.168.0.1 route add -host 192.168.1.1 dev ens33 删除路由 route del -net 192.168.0.0/24 gw 192.168.0.1 route del -host 192.168.1.1 dev ens33 add 增加路由 del 删除路由 -net 设置到某个网段的路由 -host 设置到某台主机的路由 gw 出口网关 IP地址 dev 出口网关 物理设备名 增 加默认路由 route add default gw 192.168.0.1 默认路由一条就够了 route -n 查看路由表
方法二: 添加路由 ip route add 192.168.0.0/24 via 192.168.0.1 ip route add 192.168.1.1 dev ens33 删除路由 ip route del 192.168.0.0/24 via 192.168.0.1 ip route del 192.168.1.1 dev ens33 add 增加路由 del 删除路由 via 网关出口 IP地址 dev 网关出口 物理设备名 增加默认路由 ip route add default via 192.168.0.1 dev eth0 via 192.168.0.1 是我的默认路由器 查看路由信息 ip route
四.静态路由表
使用下面的 route 命令可以查看路由表。
# route Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 * 255.255.255.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 default 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
route 命令的输出项说明
输出项 说明
Destination 目标网段或者主机
Gateway 网关地址,”*” 表示目标是本主机所属的网络,不需要路由
Genmask 网络掩码
Flags 标记。一些可能的标记如下:
U — 路由是活动的
H — 目标是一个主机
G — 路由指向网关
R — 恢复动态路由产生的表项
D — 由路由的后台程序动态地安装
M — 由路由的后台程序修改
! — 拒绝路由
3 种路由类型
主机路由
[root@qf-1 ~]# route add -host 192.168.1.1 dev ens33
主机路由是路由选择表中指向单个IP地址或主机名的路由记录。主机路由的Flags字段为H。例如,在下面的示例中,本地主机通过IP地址192.168.1.1的路由器到达IP地址为10.0.0.10的主机。
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.10 192.168.1.1 255.255.255.255 UH 0 0 0 eth0
网络路由
网络路由是代表主机可以到达的网络。网络路由的Flags字段为U。例如,在下面的示例中,本地主机将发送到网络192.19.12的数据包转发到IP地址为192.168.1.1的路由器。
Destination Gateway Genmask Flags Metric Ref Use Iface
192.19.12.0 192.168.1.1 255.255.255.0 U 0 0 0 eth0
默认路由
当主机不能在路由表中查找到目标主机的IP地址或网络路由时,数据包就被发送到默认路由(默认网关)上。默认路由的Flags字段为G。例如,在下面的示例中,默认路由是IP地址为192.168.1.1的路由器。
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
配置静态路由
route 命令
设置和查看路由表都可以用 route 命令,设置内核路由表的命令格式是:
route [add|del][-net|-host] target [netmask Nm][gw Gw] [[dev] If]
其中:
add : 添加一条路由规则
del : 删除一条路由规则
-net : 目的地址是一个网络
-host : 目的地址是一个主机
target : 目的网络或主机
netmask : 目的地址的网络掩码
gw : 路由数据包通过的网关
dev : 为路由指定的网络接口
route 命令使用举例
添加到主机的路由 route add -host 192.168.1.2 dev eth0 route add -host 10.20.30.148 gw 192.168.62.2
添加到网络的路由 route add -net 10.20.30.0 netmask 255.255.255.0 eth0 route add -net 10.20.30.0 netmask 255.255.255.0 gw 192.168.62.2 route add -net 192.168.1.0/24 eth1
添加默认路由(此步骤,需要添加或者删除之后才能生效) route add default gw 192.168.1.1
删除路由 route del -host 192.168.1.2 dev eth0 route del -host 10.20.30.148 gw 10.20.30.40 route del -net 10.20.30.0 netmask 255.255.255.0 eth0 route del -net 10.20.30.0 netmask 255.255.255.248 0 10.20.30.41 route del -net 192.168.1.0/24 eth1 route del default gw 192.168.1.1
设置包转发
在 CentOS 中默认的内核配置已经包含了路由功能,但默认并没有在系统启动时启用此功能。开启Linux的路由功能可以通过调整内核的网络参数来实现。要配置和调整内核参数可以使用 sysctl 命令。例如:要开启Linux内核的数据包转发功能可以使用如下的命令。
#sysctl -w net.ipv4.ip_forward=1
这样设置之后,当前系统就能实现包转发,但下次启动计算机时将失效。为了使在下次启动计算机时仍然有效,需要将下面的行写入配置文件/etc/sysctl.conf。
#vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
用户还可以使用如下的命令查看当前系统是否支持包转发。
#sysctl net.ipv4.ip_forward
- Linux基础六(网络管理)
- Linux中的网络管理——计算机网络基础
- linux网络管理----网络基础
- Linux基础:网络管理
- redhat-linux基础网络管理
- 网络基础知识、linux路由管理命令(route) 、ip命令、ifconfig命令的使用、linux网卡配置
- Oracle 管理之 Linux 网络基础
- linux 网络管理基础 OSI ISO IOS的区别
- Linux基础学习笔记(Linux网络管理)
- Spark修炼之道(基础篇)——Linux大数据开发基础:第八节:网络管理
- Linux基础入门——网络管理与进程管理(九)
- linux基础增强:查找命令、用户组、权限、系统服务、网络管理
- linux网络管理基础
- Spark修炼之道(基础篇)——Linux大数据开发基础:第八节:网络管理
- linux基础之管理网络
- linux网络基础-远程管理
- Linux操作基础命令(2)-用户管理、文件权限、磁盘管理、网络设置、文件的打包、python软件安装
- Linux用户管理(九)Linux网络基础
- linux网络管理基础知识
- linux基础(31)--网络管理命令--RHEL6.5