route命令
路由管理命令
查看:route -n
[root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.101.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
添加:route add
route add [-net|-host] target [netmask Nm] [gw Gw][[dev] If]
目标:192.168.1.3 网关:172.16.0.1
# route add -host 192.168.1.3 gw 172.16.0.1 dev eth0
目标:192.168.0.0 网关:172.16.0.1
# route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1 dev eth0
# route add -net 192.168.0.0/24 gw 172.16.0.1 dev eth0
默认路由,网关:172.16.0.1
# route add -net 0.0.0.0 netmask 0.0.0.0 gw 172.16.0.1
# route add default gw 172.16.0.1
删除:route del
route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
目标:192.168.1.3 网关:172.16.0.1
# route del -host 192.168.1.3
目标:192.168.0.0 网关:172.16.0.1
# route del -net 192.168.0.0 netmask 255.255.255.0
[root@localhost ~]# route add -net 10.0.0.0/24 gw 127.0.0.1 (添加路由表) [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.101.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0 10.0.0.0 127.0.0.1 255.255.255.0 UG 0 0 0 lo [root@localhost ~]# route del -net 10.0.0.0/24 gw 127.0.0.1 (删除路由表) [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.101.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
netstat 命令
netstat – Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
显示网络连接:
netstat [–tcp|-t] [–udp|-u] [–raw|-w] [–listening|-l] [–all|-a] [–numeric|-n] [–extend|-e[–
extend|-e]] [–program|-p]
-t: tcp 协议相关
-u: udp 协议相关
-w: raw socket 相关
-l:处于监听状态
-a:所有状态
-n:以数字显示IP 和端口;
-e:扩展格式
-p:显示相关进程及PID
常用组合:
-tan, -uan, -tnl, -unl
显示路由表:
netstat {–route|-r} [–numeric|-n]
-r: 显示内核路由表
-n: 数字格式
显示接口统计数据:
netstat {–interfaces|-I|-i} [iface] [–all|-a] [–extend|-e] [–program|-p] [–numeric|-n]
netstat -i
netstat –I=IFACE
ifconfig -s eno16777736
例: [root@localhost ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.101.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 [root@localhost ~]# netstat -tun Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 192.168.101.128:22 192.168.101.1:52170 ESTABLISHED tcp 0 52 192.168.101.128:22 192.168.101.1:54285 ESTABLISHED [root@localhost ~]# netstat -tunl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:44842 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN [root@localhost ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:44842 0.0.0.0:* LISTEN 1794/rpc.statd tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1737/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2016/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1832/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2102/master [root@localhost ~]# lsof -i :22 (查看22端口) COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 2016 root 3u IPv4 15379 0t0 TCP *:ssh (LISTEN) sshd 2016 root 4u IPv6 15381 0t0 TCP *:ssh (LISTEN) sshd 4151 root 3r IPv4 25697 0t0 TCP 192.168.101.128:ssh->192.168.101.1:52170 (ESTABLISHED) sshd 5784 root 3r IPv4 30652 0t0 TCP 192.168.101.128:ssh->192.168.101.1:54285 (ESTABLISHED) [root@localhost ~]# netstat -Ieth0 Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 4412 0 0 0 2818 0 0 0 BMRU
ip命令
配置Linux 网络属性:ip 命令
ip – show / manipulate routing, devices, policy routingand tunnels
ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT := { link | addr | route }
ip link – network device configuration
set dev IFACE
可设置属性:
up and down :激活或禁用指定接口
ifup/ifdown
show [dev IFACE] :指定接口
[up]:仅显示处于激活状态的接口
ip addr { add | del } IFADDR dev STRING
[label LABEL] :添加地址时指明网卡别名
[scope {global|link|host}] :指明作用域
global: 全局可用
link: 仅链接可用
host: 本机可用
[broadcast ADDRESS] :指明广播地址
ip address show – look at protocol addresses
[dev DEVICE]
[label PATTERN]
[primary and secondary]
ip address flush – 使用格式同show
ip addr add 172.16.100.100/16 dev eth0 label eth0:0
ip addr del 172.16.100.100/16 dev eth0 label eth0:0
ip addr flush dev eth0 label eth0:0
ip route – routing table management
添加路由:ip route add
ip route add TARGET via GW dev IFACE src SOURCE_IP
TARGET:
主机路由:IP
网络路由:NETWORK/MASK
ip route add 192.168.0.0/24 via 172.16.0.1
ip route add 192.168.1.13 via 172.16.0.1
添加网关:ip route add default via GW dev IFACE
ip route add default via 172.16.0.1
删除路由:ip route delete
ip route del TARGET
显示路由:ip route show|list
清空路由表:ip route flush [dev IFACE] [via PREFIX]
ip route flush dev eth0
例: [root@localhost ~]# ip a (显示IP地址) 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 5: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:e4:8e:e3 brd ff:ff:ff:ff:ff:ff inet 192.168.101.128/24 brd 192.168.101.255 scope global eth0 inet6 fe80::20c:29ff:fee4:8ee3/64 scope link valid_lft forever preferred_lft forever 6: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:e4:8e:ed brd ff:ff:ff:ff:ff:ff inet6 fe80::20c:29ff:fee4:8eed/64 scope link valid_lft forever preferred_lft forever [root@localhost ~]# ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 5: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:e4:8e:e3 brd ff:ff:ff:ff:ff:ff 6: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:e4:8e:ed brd ff:ff:ff:ff:ff:ff [root@localhost ~]# ip addr add 9.9.9.9/24 dev eth2 添加IP地址 [root@localhost ~]# ip a eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:e4:8e:ed brd ff:ff:ff:ff:ff:ff inet 9.9.9.9/24 scope global eth2 inet6 fe80::20c:29ff:fee4:8eed/64 scope link valid_lft forever preferred_lft forever [root@localhost ~]# ip a del 9.9.9.9/24 dev eth2 [root@localhost ~]# ip a eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:e4:8e:ed brd ff:ff:ff:ff:ff:ff inet6 fe80::20c:29ff:fee4:8eed/64 scope link valid_lft forever preferred_lft forever [root@localhost ~]# ip a flush dev eth2 [root@localhost ~]# ip a eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:e4:8e:ed brd ff:ff:ff:ff:ff:ff [root@localhost ~]# service network restart Shutting down interface eth0: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: Determining IP information for eth0... done. [ OK ]
ss 命令
格式:ss [OPTION]… [FILTER]
netstat 通过遍历proc 来获取socket 信息,ss 使用netlink 与内核tcp_diag模块通信获取socket 信息。
选项:
-t: tcp 协议相关
-u: udp 协议相关
-w:裸套接字相关
-x:unix sock 相关
-l:listen 状态的连接
-a:所有
-n:数字格式
-p:相关的程序及PID
-e:扩展的信息
-m:内存用量
-o:计时器信息
FILTER : [ state TCP-STATE ] [ EXPRESSION ]
TCP 的常见状态:
tcp finite state machine:
LISTEN: 监听
ESTABLISHED :已建立的连接
FIN_WAIT_1
FIN_WAIT_2
SYN_SENT
SYN_RECV
CLOSED
EXPRESSION:
dport =
sport =
示例:’( dport = :ssh or sport = :ssh )’
常用组合:
-tan, -tanl, -tanlp, -uan
常见用法:
ss -l 显示本地打开的所有端口
[root@localhost ~]# ss -l State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:44842 *:* LISTEN 0 128 :::sunrpc :::* LISTEN 0 128 *:sunrpc *:* LISTEN 0 128 :::47797 :::* LISTEN 0 128 :::ssh :::*
ss -pl 显示每个进程具体打开的socket
[root@localhost ~]# ss -pl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:44842 *:* users:(("rpc.statd",1794,8)) LISTEN 0 128 :::sunrpc :::* users:(("rpcbind",1737,11)) LISTEN 0 128 *:sunrpc *:*
ss -t -a 显示所有tcp socket
[root@localhost ~]# ss -t -a State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:44842 *:* LISTEN 0 128 :::sunrpc :::* LISTEN 0 128 *:sunrpc *:* LISTEN 0 128 :::47797 :::* LISTEN 0 128 :::ssh :::* LISTEN 0 128 *:ssh *:* LISTEN 0 128 127.0.0.1:ipp *:*
ss -u -a 显示所有的UDP Socekt
[root@localhost ~]# ss -u -a State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:bootpc *:* UNCONN 0 0 *:bootpc *:* UNCONN 0 0 *:49123 *:* UNCONN 0 0 *:sunrpc *:* UNCONN 0 0 *:ipp *:*
ss -o state established ‘( dport = :ssh or sport = :ssh)’ 显示所有已建立的ssh 连接
ss -o state established ‘( dport = :http or sport =:http )’ 显示所有已建立的HTTP 连接
ss -s 列出当前socket
[root@localhost ~]# ss -s Total: 911 (kernel 928) TCP: 16 (estab 2, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 8 Transport Total IP IPv6 * 928 - - RAW 0 0 0 UDP 10 7 3 TCP 16 9 7 INET 26 16 10 FRAG 0 0 0
例: [root@localhost ~]# ss -nalu State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:68 *:* UNCONN 0 0 *:68 *:* UNCONN 0 0 *:49123 *:* UNCONN 0 0 *:111 *:* root@localhost ~]# ss -tnalp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:44842 *:* users:(("rpc.statd",1794,8)) LISTEN 0 128 :::111 :::* users:(("rpcbind",1737,11)) LISTEN 0 128 *:111 *:* [root@localhost ~]# ss -nt State Recv-Q Send-Q Local Address:Port Peer Address:Port ESTAB 0 0 192.168.101.128:22 192.168.101.1:52170 ESTAB 0 52 192.168.101.128:22 192.168.101.1:54285
nmcli命令
地址配置工具:nmcli
nmcli [ OPTIONS ] OBJECT { COMMAND | help }
device – show and manage network interfaces 显示和管理网络接口
nmcli device help nmcli设备帮助
connection – start, stop, and manage network connections连接–启动.停止和管理网络连接
nmcli connection help nmcli连接帮助
[root@localhost ~]# nmcli device show GENERAL.DEVICE: virbr0 GENERAL.TYPE: bridge GENERAL.HWADDR: 52:54:00:0E:BF:A5 GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: virbr0 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 IP4.ADDRESS[1]: 192.168.122.1/24 IP4.GATEWAY: IP6.GATEWAY:
修改IP 地址等属性:
#nmcli connection modify IFACE [+|-]setting.property value
setting.property:
ipv4.addresses ipv4.gateway
ipv4.dns1 ipv4.method manual | auto
[root@localhost network-scripts]# nmcli connection modify wired\ connection\ 1 connection.id con-eth1 将wired_connection_1改名为eth1
修改配置文件执行生效:systemctl restart network
nmcli con reload
nmcli命令生效:nmcli con down eth0 ;nmcli con up eth0
相关工具
网络接口配置tui工具:nmtui
主机名称配置工具:hostnamectl
status
set-hostname
使用nmcli 配置主机名
CentOS6 之前主机名配置文件:/etc/sysconfig/network
CentOS7 主机名 配置文件:/etc/hostname ,默认没有这个文件,通过DNS反向解析获取主机名,主机名默认为:localhost.localdomain
显示主机名信息
hostname
hostnamectl status
创建并修改文件并生效
hostnamectl set-hostname desktopX.example.com
删除文件,恢复 主机名localhost.localdomain
使用nmcli 配置网络
NeworkManager 是管理和监控网络设置的守护进程
设备即网络接口,连接是对网络接口的配置。一个网络接口可有多个连接配置,但同时只有一个连接配置生效
显示所有包括不活动连接
nmcli con show
[root@localhost ~]# nmcli con show (查看有多少套配置) NAME UUID TYPE DEVICE ens33 ad4e4a2e-c6fe-4661-94d3-c02901f2e328 802-3-ethernet ens33 virbr0 ad2192b6-9a6d-4624-b273-12389b246f54 bridge virbr0 [root@localhost ~]# nmcli connection show ens33 connection.id: ens33 connection.uuid: ad4e4a2e-c6fe-4661-94d3-c02901f2e328 connection.stable-id: -- connection.interface-name: ens33 connection.type: 802-3-ethernet connection.autoconnect: yes connection.autoconnect-priority: 0 connection.timestamp: 1494160439
显示所有活动连接
nmcli con show –active
[root@localhost network-scripts]# nmcli con show --active NAME UUID TYPE DEVICE ens33 ad4e4a2e-c6fe-4661-94d3-c02901f2e328 802-3-ethernet ens33 virbr0 ad2192b6-9a6d-4624-b273-12389b246f54 bridge virbr0
显示网络连接配置
nmcli con show “System eth0”
显示设备状态
nmcli dev status
[root@localhost ~]# nmcli dev status DEVICE TYPE STATE CONNECTION virbr0 bridge connected virbr0 ens33 ethernet connected ens33 lo loopback unmanaged -- 不可用 virbr0-nic tun unmanaged --
显示网络接口属性
nmcli dev show eno16777736
创建新连接default ,IP 自动通过dhcp 获取
nmcli con add con-name default type Ethernet ifname eth0
删除连接
nmcli con del default
创建新连接static,指定静态IP,不自动连接
nmcti con add con-name static ifname eth0 autoconnect no type Ethernet ip4 172.25.X.10/24 gw4 172.25.X.254
启用static 连接配置
nmcli con up static
启用default 连接配置
nmcli con up default
查看帮助
nmcli con add help
修改连接设置
nmcli con mod“static” connection.autoconnect no
nmcli con mod “static” ipv4.dns 172.25.X.254
nmcli con mod “static” +ipv4.dns 8.8.8.8
nmcli con mod “static” -ipv4.dns 8.8.8.8
nmcli con mod “static” ipv4.addresses “172.25.X.10/24 172.25.X.254”
nmcli con mod “static” +ipv4.addresses 10.10.10.10/16
DNS 设置,存放在/etc/resolv.conf 文件中
PEERDNS=no 表示当IP通过dhcp 自动获取时,dns仍是手动设置,不自动获取。等价于下面命令:
nmcli con mod “system eth0” ipv4.ignore-auto-dns yes
网络配置文件
设备配置被保存在文本文件中
/etc/sysconfig/network-scripts/ifcfg-<name>
帮助文档列出完整选项列表:/usr/share/doc/initcripts-*/sysconfig.txt
动态配置 |
静态配置 |
DEVICE=eth X HWADDR=0:02:8A:A6:30:45 BOOTPROTO=dhcp ONBOOT=yes Type=Ethernet |
DEVICE=eth X HWADDR=0:02:8A:A6:30:45 IPADDR=192.168.0.123 NETMASK=255.255.255.0 GATEWAY=192.168.0.254 ONBOOT=yes Type=Ethernet |
修改连接配置后 ,需要重新加载配置
nmcli con reload
nmcli con down “system eth0”可被自动激活
nmcli con up “system eth0”
nmcli dev dis eth0 禁用网卡,访止被自动激活
图形工具
nm-connection-editor
字符工具
nmtui
nmtui-connect
nmtui-edit
nmtui-hostname
nmcli 实现bonding
添加bonding 接口:
nmcli con add type bond con-name mybond0 ifname
mybond0 mode active-backup
添加从属接口:
nmcli con add type bond-slave ifname ens7 master mybond0
nmcli con add type bond-slave ifname ens3 master mybond0
注:如无为从属接口提供连接名,则该名称是接口名称加类型构成
要启动绑定,则必须首先启动从属接口:
nmcli con up bond-slave-ens7
nmcli con up bond-slave-ens3
现在可使用以下方法启动绑定:
nmcli con up mybond0
网络组Network Teaming
网络组:是将多个网卡聚合在一起方法,从而实现冗错和提高吞吐量
网络组不同于旧版中bonding 技术,提供更好的性能和扩展性
网络组由内核驱动和teamd 守护进程实现.
多种方式runner
broadcast
roundrobin
activebackup
loadbalance
lacp (implements the 802.3ad Link Aggregation Control Protocol)
网络组
启动网络组接口不会自动启动网络组中的port 接口
启动网络组接口中的port 接口总会自动启动网络组接口
禁用网络组接口会自动禁用网络组中的port 接口
没有port 接口的网络组接口可以启动静态IP 连接
启用DHCP 连接时,没有port 接口的网络组会等待port接口的加入
创建网络组接口
nmcli con add type team con-name CNAME ifname INAME [config JSON ]
CNAME 连接名 , INAME 接口名
JSON 指定runner 方式
格式:‘{“runner”: {“name”: ” METHOD “}}’
METHOD 可以是broadcast, roundrobin,activebackup, loadbalance, lacp
创建port 接口
nmcli con add type team-slave con-name CNAME ifname INAME master TEAM
CNAME 连接名
INAME 网络接口名
TEAM 网络组接口名
连接名若不指定,默认为team-slave- IFACE
nmcli dev dis INAME
nmcli con up CNAME
INAME 设备名 CNAME 网络组接口名或port
网络组示例
nmcli con add type team con-name team0 ifname team0 config ‘{“runner”: {“name”: “loadbalance”}}’
nmcli con mod team0 ipv4.addresses192.168.1.100/24
nmcli con mod team0 ipv4.method manual
nmcli con add con-name team0-eth1 type team-slave ifname eth1 master team0
nmcli con add con-name team0-eth2 type team-slave ifname eth2 master team0
nmcli con up team0
nmcli con up team0-eth1
nmcli con up team0-eth2
teamdctl team0 state; # nmcli dev dis eth1
实验: 创建网络组
ip link
nmcli con add type team con-name team0 ifname team0 config ‘{“runner”: {“name”: “activebackup”}}’
nmcli con mod team0 ipv4.addresses’192.168.0.100/24′
nmcli con mod team0 ipv4.method manual
nmcli con add con-name team0-port1 type team-slave ifname eth1 master team0
nmcli con add con-name team0-port2 type team-slave ifname eth2 master team0
teamdctl team0 state
ping -I team0 192.168.0.254
nmcli dev dis eno1
teamdctl team0 state
nmcli con up team0-port1
nmcli dev dis eno2
teamdctl team0 state
nmcli con up team0-port2
teamdctl team0 state
管理网络组配置文件
/etc/sysconfig/network-scripts/ifcfg-team0
DEVICE=team0
DEVICETYPE=Team
TEAM_CONFIG=”{\”runner\”: {\”name\”: \”broadcast\”}}”
BOOTPROTO=none
IPADDR0=172.25.5.100
PREFIX0=24
NAME=team0
ONBOOT=yes
/etc/sysconfig/network-scripts/ifcfg-team0-eth1
DEVICE=eth1
DEVICETYPE=TeamPort
TEAM_MASTER=team0
NAME=team0-eth1
ONBOOT=yes
删除网络组
nmcli connection down team0
teamdctl team0 state
nmcli connection show
nmcli connectioni delete team0-eth0
nmcli connectioni delete team0-eth1
nmcli connection show
测试网络工具
在命令行下测试网络的连通性
显示主机名
hostname
测试网络连通性
ping
mtr
[root@localhost ~]# ping 172.16.253.193 PING 172.16.253.193 (172.16.253.193) 56(84) bytes of data. 64 bytes from 172.16.253.193: icmp_seq=1 ttl=64 time=0.097 ms 64 bytes from 172.16.253.193: icmp_seq=2 ttl=64 time=0.051 ms 64 bytes from 172.16.253.193: icmp_seq=3 ttl=64 time=0.053 ms 64 bytes from 172.16.253.193: icmp_seq=4 ttl=64 time=0.052 ms
显示正确的路由表
ip route
确定名称服务器使用:
nslookup
host
dig
跟踪路由
traceroute
tracepath
网络客户端工具
ftp ,lftp
子命令:get、mget、ls、help
lftp [-p port] [-u user[,password]] SERVER
lftpget URL
wget [option]… [URL]…
-q:静默模式
-c:断点续传
-O:保存位置
–limit-rates=:指定传输速率
links URL
–dump
–source
原创文章,作者:Linux.rookie,如若转载,请注明出处:http://www.178linux.com/74962