查看自己的虚拟机IP信息,包括以下信息并将信息记录到一个文本文件

  1. IP地址及子网掩码、广播地址、MAC地址信息
  2. 主机名信息
  3. DNS服务器
# ifconfig ens33 > ttt 
# hostname >> ttt
# cat /etc/resolv.conf >> ttt

Linux常用网络配置(Ⅰ)_IPV6

临时修改自己的IP地址为192.168.154.200/24

# ifconfig ens33 192.168.154.200 netmask 255.255.255.0

Linux常用网络配置(Ⅰ)_IP_02

永久修改自己的主机名为test-server

# hostnamectl set-hostname test-server

添加202.106.0.20、8.8.8.8为自己的备份DNS服务器地址

[root@test-server ~]# vim /etc/resolv.conf
[root@test-server ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.115.2
nameserver 202.106.0.20
nameserver 8.8.8.8

通过修改网卡配置文件的方式,配置以下主机网络参数:

  • 配置ip地址为192.168.100.10/24
  • 网关地址为192.168.100.254
  • DNS服务器为202.106.0.20, 备份DNS为8.8.8.8
  • 开机启动网卡
[root@test-server ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
[root@test-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=2f28d90f-0572-49b1-84ce-c4aff6cb84d3
DEVICE=ens33
ONBOOT=yes
IPADDR0=192.168.100.10
PREFIX0=24
GATEWAY0=192.168.100.254
DNS1=202.106.0.20
DNS2=8.8.8.8

使用nmcli显示所有连接

[root@test-server ~]# nmcli connection show
NAME    UUID                                  TYPE      DEVICE 
ens33   2f28d90f-0572-49b1-84ce-c4aff6cb84d3  ethernet  ens33  
virbr0  607928a3-b670-407f-b4a5-e984e3d4320d  bridge    virbr0

使用nmcli显示其中一个活动链接的配置设

[root@test-server ~]# nmcli connection show ens33
connection.id:                          ens33
connection.uuid:                        2f28d90f-0572-49b1-84ce-c4aff6cb84d3
connection.stable-id:                   --
connection.type:                        802-3-ethernet
connection.interface-name:              ens33
connection.autoconnect:                 是
connection.autoconnect-priority:        0
........
.....

使用nmcli查看设备状态

# nmcli device status 
DEVICE      TYPE      STATE   CONNECTION 
ens33       ethernet  已连接  ens33      
virbr0      bridge    已连接  virbr0     
lo          loopback  未托管  --         
virbr0-nic  tun       未托管  --

使用nmcli删除现有所有链接

# nmcli connection delete ens33 virbr0 
成功删除连接 "ens33" (2f28d90f-0572-49b1-84ce-c4aff6cb84d3)。
成功删除连接 "virbr0" (607928a3-b670-407f-b4a5-e984e3d4320d)。

使用nmcli创建一个新连接。该链接使用DHCP方式自动获取ip 地址,网关和DNS.

# nmcli connection add con-name yu type ethernet ifname ens33 
连接 "ens33" (96629896-a1b7-4767-a41b-f9255fd86d11) 已成功添加。

使用nmcli查看新连接获得ip 地址,网关和DNS。

# nmcli connection show ens33

Linux常用网络配置(Ⅰ)_IPV6_03

使用nmcli将刚才的新连接改为手动设置,并将刚才通过自动获取获得的ip地址,网关和DNS改为手工配置。

# nmcli connection modify yu ipv4.method manual ipv4.addresses 192.168.115.128/24 ipv4.gateway 192.168.115.2 ipv4.dns 192.168.115.2

确认配置,使用新配置的地址测试网络是否通信。

[root@test-server ~]# ping www.sina.com
PING spool.grid.sinaedge.com (111.62.129.51) 56(84) bytes of data.
64 bytes from 111.62.129.51 (111.62.129.51): icmp_seq=1 ttl=128 time=8.67 ms
64 bytes from 111.62.129.51 (111.62.129.51): icmp_seq=2 ttl=128 time=8.10 ms
64 bytes from 111.62.129.51 (111.62.129.51): icmp_seq=3 ttl=128 time=11.7 ms
64 bytes from 111.62.129.51 (111.62.129.51): icmp_seq=4 ttl=128 time=8.24 ms
^C
--- spool.grid.sinaedge.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 8.106/9.184/11.706/1.474 ms

使用ping命令测试自己的Linux主机是否可以上网(可以选择www.baidu.com),连续ping100个包,查看其网络延迟大小

[root@test-server ~]# ping www.baidu.com -c 100
PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data.
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=128 time=16.9 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=128 time=19.3 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=3 ttl=128 time=18.9 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=4 ttl=128 time=17.1 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=5 ttl=128 time=17.2 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=6 ttl=128 time=16.7 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=7 ttl=128 time=17.5 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=8 ttl=128 time=19.0 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=9 ttl=128 time=21.6 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=10 ttl=128 time=17.1 ms
.............

使用traceroute命令测试到达www.baidu.com和www.google.com主机路径上的所有路由器地址

[root@test-server ~]# traceroute www.baidu.com
traceroute to www.baidu.com (39.156.66.14), 30 hops max, 60 byte packets
 1  gateway (192.168.115.2)  0.132 ms  0.133 ms  0.085 ms
 2  * * *
 3  * * *
.....
[root@test-server ~]# traceroute www.google.com
traceroute to www.google.com (31.13.70.20), 30 hops max, 60 byte packets
 1  gateway (192.168.115.2)  0.182 ms  0.096 ms  0.078 ms
 2  * * *
 3  * * *
.....

使用域名解析命令将自己经常访问的网站的IP地址解析出来,并记录到本地hosts文件,以便加快访问速度

[root@test-server ~]# host www.sina.com > hosts
[root@test-server ~]# host www.baidu.com >> hosts
[root@test-server ~]# cat hosts 
www.sina.com is an alias for us.sina.com.cn.
us.sina.com.cn is an alias for spool.grid.sinaedge.com.
spool.grid.sinaedge.com has address 111.62.129.51
spool.grid.sinaedge.com has IPv6 address 2409:8c04:1101:9::7:81
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 39.156.66.14
www.a.shifen.com has address 39.156.66.18

查看自己的主机网关的MAC地址,并静态绑定,以便防局域网ARP攻击

[root@test-server ~]# arp -a
? (192.168.115.1) at 00:50:56:c0:00:08 [ether] on ens33
gateway (192.168.115.2) at 00:50:56:e3:12:92 [ether] on ens33
[root@test-server ~]# arp -s 192.168.115.2 00:50:56:e3:12:92