1.查看及测试网络
(1)查看网络配置
- ifconfig命令,查看网络接口地址,直接使用此命令不带任何参数时,将会显示当前主机已启用的网络接口信息
1
2
3
4
5
6
7
8[root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.200 netmask 255.255.255.0 broadcast 192.168.100.255 …… lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 …… //centos7 中,ens33 默认是第一块网卡,lo 是loopback的缩写,并不代表实际的网络接口,通常仅用于对本机的网络测试
ifconfig -a 查看所有网络接口信息,包括启用的和未启用的
- 查看指定的网络接口信息,在ifconfig命令后接网络接口名
1
2
3
4
5
6
7
8
9[root@localhost ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.200 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fe80::20c:29ff:fecd:d157 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:cd:d1:57 txqueuelen 1000 (Ethernet) RX packets 2312 bytes 434316 (424.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1050 bytes 97364 (95.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-
-
- ether:表示网络接口的MAC地址
- inet:表示网络接口的IP地址
- broadcast:表示网络接口所在网络的广播地址
- netmask:表示网络接口的子网掩码
- TX:网络接口发送的数据包个数、流量等
- RX:网络接口接收的数据包个数、流量等
-
(2)route命令查看路由表:
1
2
3
4
5
6
7
8
9
10
11
12
13[root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 0 0 0 ens33 link-local 0.0.0.0 255.255.0.0 U 1002 0 0 ens33 172.16.0.0 192.168.100.1 255.255.0.0 UG 0 0 0 ens33 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.100.2 0.0.0.0 UG 0 0 0 ens33 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ens33
//route -n,如上对比,添加了 -n选项后,将路由记录中的地址显示为数字形式
(3)netstat命令查看网络连接情况:
netstat [选项]
常用选项:
- -a :显示当前主机中所有活动的网络连接信息(包括监听、非监听状态的服务端口)
- -n :以数字形式显示相关的主机地址、端口等信息
- -r :显示路由表信息
- -p :显示与网络连接相关联的进程号、进程名称信息(需要root权限)
- -t :查看TCP协议相关信息
- -u :查看UDP协议相关的信息
- -l :显示处于Listening(监听)状态的网络连接及端口信息
1
2
3
4
5
6
7
8
9[root@localhost ~]# netstat -antp Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 826/rpcbind tcp6 0 0 :::80 :::* LISTEN 1246/httpd …… 也可以结合管道命令,过滤出特定的记录 [root@localhost ~]# netstat -antp | grep ":80" //查看是否有监听TCP80端口
(4)ping命令测试网络连通性:
- ping 目标主机IP地址或域名
1
2
3
4
5
6
7
8
9[root@localhost ~]# ping 192.168.100.200 PING 192.168.100.200 (192.168.100.200) 56(84) bytes of data. 64 bytes from 192.168.100.200: icmp_seq=1 ttl=64 time=0.171 ms [root@localhost ~]# ping www.baidu.com PING www.baidu.com (14.215.177.38) 56(84) bytes of data. 64 bytes from www.baidu.com (14.215.177.38): icmp_seq=1 ttl=128 time=40.2 ms //Ctrl + C 快捷键终止测试
(5)使用traceroute命令跟踪路由路径:
测试从当前主机到目标主机之间经过了哪些网络节点,显示各节点状态
- traceroute 目标主机IP 或 目标域名
1
2
3
4
5[root@localhost Packages]# traceroute www.biadu.com traceroute to www.biadu.com (47.254.33.193), 30 hops max, 60 byte packets 1 gateway (192.168.100.2) 0.177 ms 0.089 ms 0.092 ms 2 * * * ……
(6)使用nslookup命令测试域名解析:
- nslookup 域名
1
2
3
4
5
6
7
8
9
10
11
12[root@localhost ~]# nslookup www.baidu.com Server: 192.168.100.2 Address: 192.168.100.2#53 Non-authoritative answer: Name: www.baidu.com Address: 14.215.177.38 [root@localhost Packages]# nslookup http://www.baidu.com ;; connection timed out; no servers could be reached //报错:表示不能连接到指定DNS服务器,此报错可以查看是否是网卡设置有问题
2.设置网络地址参数
设置网络参数的方式
-
临时配置——使用命令调整调整网络参数
-
- 简单、快速,可直接修改运行中的网络参数
- 一般只是用于调试网络的过程中使用
- 系统重启后失效
-
-
固定配置——通过配置文件修改网络参数
-
- 修改各项网络参数的配置文件
- 适合对服务器设置固定参数时使用
- 需要重载网络服务或重启才会生效
-
(1)ifconfig命令设置网卡的地址和状态
- 修改IP和子网掩码
ifconfig 网络接口名 IP地址 [netmask 子网掩码]
1[root@localhost Packages]# ifconfig ens33 192.168.100.201 netmask 255.255.255.0
或
ifconfig 网络接口名 IP地址 [/子网掩码长度]
1[root@localhost Packages]# ifconfig ens33 192.168.100.201/24
- 禁用或激活网卡:
ifconfig 网络接口 up //激活
1[root@localhost ~]# ifconfig ens33 down
ifconfig 网络接口 down //禁用
1
2[root@localhost ~]# ifconfig ens33 up
重启ens33网卡
1[root@localhost ~]# ifdown ens33;ifup ens33
- 设置虚拟网络接口:
对服务器网络进行调试过程中,有时需要在同一网卡上使用一个新的IP地址,相当于一块网卡配多个IP地址,不干扰原本正在使用的服务程序
ifconfig 接口名:序号 IP地址
1[root@localhost ~]# ifconfig ens33:0 192.168.100.202
(2)route命令设置路由记录
- 添加、删除静态路由记录:
route add -net 网段地址/子网掩码长度 gw 下一跳路由IP //添加静态路由
1[root@localhost ~]# route add -net 192.168.100.0/24 gw 192.168.6.254
route del -net 网段地址 //删除静态路由
1
2[root@localhost ~]# route del -net 192.168.100.0
route add default gw 下一跳路由IP //添加默认路由
1[root@localhost ~]# route add default gw 192.168.6.254
route del default gw IP地址 //删除默认路由
1[root@localhost ~]# route del default gw 192.168.4.1
添加路由表,指定通过那个网络接口,去往目的网段:
route add -net 网段IP/子网掩码长度 dev 网络接口名
1
2[root@localhost network-scripts]# route add -net 192.168.90.0 dev ens33 //C类地址可以省略 子网掩码和关键字 dev
(3)修改网络配置文件
- 网络接口配置文件:
在目录/etc/sysconfig/network-scripts/ 中,以ifcfg -XXX命名,如ifcfg-ens33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19[root@localhost network-scripts]# vim ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=yes BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no NAME=ens33 UUID=d090884a-5d96-47e3-9ea9-bf18f39483ba DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.200 NETMASK=255.255.255.0 GATEWAY=192.168.100.2 DNS1=192.168.100.2 //下半部分是设置静态IP地址内容
DEVICE:设置网络接口名称
ONBOOT:设置网络接口是否在Linux系统启动时激活
BOOTPROTO:设置网络接口的配置方式,为 static 时表示使用静态IP,为 dhcp 时表示通过DHCP方式动态获取IP
IPADDR:设置网络接口的IP
NETMASK:设置网络接口的子网掩码
GATEWAY:设置网络接口的默认网关
- 重启网络接口配置:
修改网络接口配置后,要使新的配置生效,可以重启network服务
1
2[root@localhost network-scripts]# systemctl restart network
只控制某个网络接口
1
2
3
4
5ifup ens33 //开启ens33网卡 ifdown ens33 //关闭ens33网卡 ifdown ens33;ifup ens33 //重启ens33网卡
(4)域名解析配置文件
/etc/resolv.conf 文件中记录了本机默认使用的DNS服务器的地址信息,对该文件所做的修改会立刻生效。Linux中最多可以指定3个不同的DNS服务器地址
1
2[root@localhost network-scripts]# vim /etc/resolv.conf nameserver 192.168.100.2 search localdomain //此行设置默认搜索域,当访问主机localhost时,相当于访问localhost.localdomain
(5)本地主机映射文件
/etc/hosts文件中记录着一份主机名与IP地址的映射关系表,一般用于保存经常需要访问的主机信息。当访问一个域名时,会先查看文件中是否有相应的记录,如果没有再去向DNS服务器查找
1
2
3
4
5[root@localhost network-scripts]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 14.215.177.38 www.baidu.com //添加的baidu的映射关系
添加常用的网站,可以减少DNS查询过程,从而更加节省时间,hosts文件只对当前主机生效
最后
以上就是跳跃巨人最近收集整理的关于Linux网络设置基础1.查看及测试网络2.设置网络地址参数的全部内容,更多相关Linux网络设置基础1内容请搜索靠谱客的其他文章。
发表评论 取消回复