若采用 mini 版 CentOS 7 安装的系统,默认是没有 ifconfig 命令的,需要先通过 yum 方式安装 net-tools 软件包,才有 ifconfig 命令
在不带任何选项和参数执行 ifconfig 命令时, 将显示当前主机中已启用(活动)的网络接口信息
##直接执行 ifconfig 命令后可以看到 ens33、lo 这两个网络接口的信息,具体操作如下
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.4.11 netmask 255.255.255.0 broadcast 192.168.4.255
…… //省略部分内容
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
…… //省略部分内容
##ens33 对应为第一块物理网卡
##lo 对应为虚拟的回环接口
##执行“ifconfig ens33”命令后可以只查看网卡 ens33 的配置信息
[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.4.11 netmask 255.255.255.0 broadcast 192.168.4.255 inet6 fe80::9106:d38b:670d:1a89 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:3a:81:cc txqueuelen 1000 (Ethernet)
RX packets 4625 bytes 428860 (418.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2302 bytes 344920 (336.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# hostname
localhost.localdomain
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 100 0 0 ens33
192.168.4.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
##执行“route -n”命令后, 输出信息中的“gateway”地址将显示为“192.168.4.1”,默认网关记录中的“default”也将显示为 “0.0.0.0”
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.4.1 0.0.0.0 UG 100 0 0 ens33
192.168.4.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
选项 | 解释 |
---|---|
-a | 显示主机中所有活动的网络连接信息(包括监听、非监听状态的服务端口) |
-n | 以数字的形式显示相关的主机地址、端口等信息 |
-r | 显示路由表信息 |
-l | 显示处于监听(Listening)状态的网络连接及端口信息 |
-t | 查看 TCP(Transmission Control Protocol,传输控制协议)相关的信息 |
-u | 显示 UDP(User Datagram Protocol,用户数据报协议)协议相关的信息 |
-p | 显示与网络连接相关联的进程号、进程名称信息(该选项需要 root 权限) |
##执行以下操作可以查看本机中是否有监听“TCP 80”端口(即标准 Web 服务)的服务程序,输出信息中包括 PID 号和进程名称
[root@localhost ~]# netstat -anpt | grep ":80"
tcp60 0 :::80 :::* LISTEN 20563/httpd
选项 | 解释 |
---|---|
-h | –help 通过该选项获取更多的使用帮助 |
-V | –version 显示软件的版本号 |
-t | –tcp 显示 TCP 协议的 sockets |
-u | –udp 显示 UDP 协议的 sockets |
-n | –numeric 不解析服务的名称,如 “22” 端口不会显示成 “ssh” |
-l | –listening 只显示处于监听状态的端口 |
-p | –processes 显示监听端口的进程 |
-a | –all 对 TCP 协议来说,既包含监听的端口,也包含建立的连接 |
-r | –resolve 把 IP 解释为域名,把端口号解释为协议名称 |
##执行以下操作可以显示TCP 协议下已经建立的连接,“Local Address:Port” 表示本地监听的 IP 和端口,“Peer Address:Port”表示远端连接的 IP 和端口
[root@localhost ~]# ss -t state established
Recv-Q Send-Q Local Address:Port Peer Address:Port 0 96 192.168.8.10:ssh 192.168.8.1:55213
命令基本格式
ping [选项] 目标主机
[root@localhost ~]# ping 192.168.100.3
PING 192.168.100.3 (192.168.100.3) 56(84) bytes of data.
64 bytes from 192.168.100.3: icmp_seq=1 ttl=128 time=0.524 ms
64 bytes from 192.168.100.3: icmp_seq=2 ttl=128 time=0.244 ms
64 bytes from 192.168.100.3: icmp_seq=3 ttl=128 time=0.435 ms
64 bytes from 192.168.100.3: icmp_seq=4 ttl=128 time=0.400 ms
64 bytes from 192.168.100.3: icmp_seq=5 ttl=128 time=0.497 ms
^C
--- 192.168.100.3 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4001ms
rtt min/avg/max/mdev = 0.244/0.420/0.524/0.098 ms
'与windows操作系统不同,Linux操作系统中使用ping命令不会自动终止'
'按Ctrl C 组合键终止ping测试'
'反馈目标主机不可达:可能目标地址不存在或主机已经关闭'
'反馈 网络不可达:表示没有可用的路由记录,如网关,无法到达目标主机所在网络'
'反馈 请求超时:表示数据到达对方,但回不来'
traceroute 目标主机地址
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#int f0/1
R1(config-if)#ip add 192.168.20.1 255.255.255.0 '配置接口IP地址'
R1(config-if)#no shut
R1(config-if)#int f0/0
R1(config-if)#ip add 192.168.10.1 255.255.255.0 '配置接口IP地址'
R1(config-if)#no shut
R1(config-if)#do sh ip int b '查看ip地址是否配置成功'
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 192.168.10.1 YES manual up up
FastEthernet0/1 192.168.20.1 YES manual up up
R1(config-if)#ip route 0.0.0.0 0.0.0.0 192.168.20.2 '添加默认路由'
R2#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R2(config)#int f0/0
R2(config-if)#ip add 192.168.20.2 255.255.255.0 '配置接口IP地址'
R2(config-if)#no sh
R2(config-if)#int f0/1
R2(config-if)#ip add 192.168.30.1 255.255.255.0 '配置接口IP地址'
R2(config-if)#no sh
R2(config-if)#do sh ip int b '查看ip地址是否配置成功'
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 192.168.20.2 YES manual up up
FastEthernet0/1 192.168.30.1 YES manual up up
R1(config-if)#ip route 0.0.0.0 0.0.0.0 192.168.20.1 '添加默认路由'
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
'进入后输入和修改以下内容'
将BOOTPROTO=dhcp 修改为 BOOTPROTO=static
并在末尾添加IP地址信息
IPADDR=192.168.10.10
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
:wq保存退出
[root@localhost ~]# service network restart '重启网卡服务'
Restarting network (via systemctl): [ 确定 ]
[root@localhost ~]# ifconfig '查看网址是否配置成功'
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::39ab:1caa:1fd3:5019 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:78:56:a8 txqueuelen 1000 (Ethernet)
...省略以下内容
C:\Users\wangermazi>ipconfig '查看是否修改成功'
Windows IP 配置
以太网适配器 Ethernet0:
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : fe80::c068:9f0e:2246:18ad%5
IPv4 地址 . . . . . . . . . . . . : 192.168.30.30
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . : 192.168.30.1
以太网适配器 蓝牙网络连接:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
隧道适配器 isatap.{
8D07E2F8-BFAB-41DB-9394-6A4E7B6B0E86}:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
[root@localhost ~]# ping 192.168.30.30
PING 192.168.30.30 (192.168.30.30) 56(84) bytes of data.
64 bytes from 192.168.30.30: icmp_seq=2 ttl=126 time=49.0 ms
64 bytes from 192.168.30.30: icmp_seq=3 ttl=126 time=44.9 ms
64 bytes from 192.168.30.30: icmp_seq=4 ttl=126 time=52.0 ms
64 bytes from 192.168.30.30: icmp_seq=5 ttl=126 time=42.3 ms
^C
--- 192.168.30.30 ping statistics ---
6 packets transmitted, 4 received, 33% packet loss, time 5002ms
rtt min/avg/max/mdev = 42.306/47.080/52.026/3.730 ms
[root@localhost ~]# traceroute 192.168.30.30 '追踪数据包'
traceroute to 192.168.30.30 (192.168.30.30), 30 hops max, 60 byte packets
1 gateway (192.168.10.1) 4.002 ms 14.763 ms 24.923 ms
2 192.168.20.2 (192.168.20.2) 35.693 ms 46.129 ms 57.016 ms
3 192.168.30.30 (192.168.30.30) 78.146 ms
* *
'追踪结果显示,数据包先到达192.168.10.1网关,之后到达192.168.20.2,'
'最终到达192.168.30.30'
命令基本格式
nslookup 目标主机地址 [DNS服务器地址]
centos 7显示结果
[root@localhost ~]# nslookup www.baidu.com
Server: 192.168.197.2
Address: 192.168.197.2#53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 180.101.49.11
Name: www.a.shifen.com
Address: 180.101.49.12
Windows 显示
C:\Users\wangermazi>nslookup www.baidu.com
服务器: ns.gwbnnj.net.cn
Address: 211.162.31.80
非权威应答:
名称: www.a.shifen.com
Addresses: 182.61.200.7
182.61.200.6
Aliases: www.baidu.com
ifconfig 接口名 IP地址 [netmask 子网掩码]
ifconfig 网络接口 IP地址[/掩码长度]
例如:
[root@localhost ~]# ifconfig ens33 10.10.10.10/24
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.10.10.10 netmask 255.255.255.0 broadcast 10.10.10.255
..省略内容
或者
[root@localhost ~]# ifconfig ens33 12.12.12.12 netmask 255.255.255.0
ifconfig 网络接口 up
ifconfig 网络接口 down
例如
[root@localhost ~]# ifconfig ens33 down '临时禁用ens33网卡'
[root@localhost ~]# ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 596 bytes 51824 (50.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 596 bytes 51824 (50.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:ac:c5:4f txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# ifconfig ens33 up '重新激活ens33网卡'
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::39ab:1caa:1fd3:5019 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:78:56:a8 txqueuelen 1000 (Ethernet)
RX packets 559 bytes 69546 (67.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 811 bytes 143394 (140.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 668 bytes 58080 (56.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 668 bytes 58080 (56.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:ac:c5:4f txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
设置虚拟网络接口的命令格式
ifconfig 接口名:序号 IP地址
[root@localhost ~]# ifconfig ens33:1 11.11.11.11
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::39ab:1caa:1fd3:5019 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:78:56:a8 txqueuelen 1000 (Ethernet)
RX packets 675 bytes 85246 (83.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 827 bytes 145682 (142.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 '添加成功'
inet 11.11.11.11 netmask 255.0.0.0 broadcast 11.255.255.255
ether 00:0c:29:78:56:a8 txqueuelen 1000 (Ethernet)
route add -net 网段地址 gw IP地址
例如
[root@localhost ~]# route '原本的路由条目'
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
11.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 ens33
192.168.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@localhost ~]# route add -net 192.168.30.0/24 gw 192.168.10.10 '添加静态路由,本机访问另一个网段192.168.30.0/24的数据都发给192.168.10.10'
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
11.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 ens33
192.168.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.30.0 localhost.local 255.255.255.0 UG 0 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
route del -net 网段地址
例如
[root@localhost ~]# route del -net 192.168.30.0/24 '删除静态路由'
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
11.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 ens33
192.168.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
route add default gw IP地址
[root@localhost ~]# route add default gw 192.168.10.30 '添加到192.168.10.30的默认网关记录'
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.10.30 255.255.255.255 UGH 0 0 0 ens33
11.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 ens33
192.168.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
route del default gw IP地址
[root@localhost ~]# route del default gw 192.168.10.30 '删除到192.168.10.30的默认网关记录'
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
11.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 ens33
192.168.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@localhost ~]# ls /etc/sysconfig/network-scripts/ifcfg-*
/etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-lo
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
..省略部分内容
BOOTPROTO="dhcp" '设置网络接口的配置方式,值为 static 表示使用静态指定的IP地址,值为 dhcp 表示 通过dhcp的方式动态获取地址'
DEVICE="ens33" '设置网络接口的名称'
ONBOOT="yes" '设置网络接口是否在Linux系统启动时激活'
IPADDR="192.168.158.165" '设置网络接口的IP地址'
NETMASK="255.255.255.0" '设置网络接口的子网掩码'
GATEWAY="192.168.158.1" '设置网络接口的默认网关地址'
[root@localhost ~]# systemctl restart network
或者
[root@localhost ~]# service network restart
[root@localhost ~]# vim /etc/resolv.conf
search localdomain '默认的搜索域(域名后缀),即,当访问主机localhost 时,相当于访问 localhost.localdomain'
nameserver 100.100.1.30
nameserver 100.100.100.30
[root@localhost ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
6.6.6.6 www.baidu.com '这是新添加的,当访问网站www.baidu.com时,就会直接向IP地址211.168.31.80发送web请求,省略了向DNS服务器解析IP地址的过程'