理论篇·Linux网络设置「ifconfig--hostname--route--netstat--ss--ping--traceroute--nslookup」

理论篇·Linux网络设置

Linux—ifconfig命令—查看网络接口信息

查看所有活动的网络接口信息

​ 执行ifconfig命令

查看指定网络接口信息

​    ifconfig 网络接口

​    [root@localhost ~]# ifconfig ens33   #查看ens33的网络接口信息
​    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
​        inet 20.0.0.10  netmask 255.255.255.0  broadcast 20.0.0.255
​        inet6 fe80::a785:826f:72c8:c9c7  prefixlen 64  scopeid 0x20<link>
​        ether 00:0c:29:7d:ce:f5  txqueuelen 1000  (Ethernet)
​        RX packets 90  bytes 10847 (10.5 KiB)
​        RX errors 0  dropped 0  overruns 0  frame 0
​        TX packets 88  bytes 11993 (11.7 KiB)
​        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Linux—hostname命令—查看主机名字

hostname命令

​ 查看或设置当前主机名

​    hostname [主机名]

​    [root@localhost ~]# hostname  ===>临时修改主机名
​    [root@localhost ~]# hostnamectl set-hostname test01===>永久修改主机名

Linux—route命令—查看路由表条目

route命令

​ 查看或设置主机中路由表信息

​    route [-n] 

​    -n  以数字形式表示

​    [root@localhost ~]# route -n
​    Kernel IP routing table
​    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
​    0.0.0.0         20.0.0.2        0.0.0.0         UG    100    0        0 ens33
​    20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33

Linux—netstat命令—查看网络连接情况

netstat命令

​ 查看系统的网络连接状态、路由表、接口统计等信息

​    netstat [选项]

​    常用选项
​    -a:查看所有的信息.
​    -n:以数字形式进行显示
​    -p:显示进程号PID
​    -t:查看tcp协议连接
​    -u:查看udp协议连接
​    -r:查看route路由表

Linux—ss命令—获取socket统计信息

ss命令===>默认查看已连接的信息

​ 查看系统的网络连接情况,获取socket统计信息

​    ss [选项]

​    常用选项
​    -t:查看tcp协议===>tcp
​    -u:查看udp协议===>udp
​    -n:取消了服务名称改换了端口号
​    -l:监听状态的信息===>listen
​    -p:查看进程号PID
​    -a:查看所有
​    -r:显示服务名称===>默认不敲就是r

Linux—ping命令—测试网络连接

ping命令

​ 测试网络连通性

​    ping [选项] 目标主机

​    示例
​    [root@localhost ~]# ping 103.235.46.39
​    PING 103.235.46.39 (103.235.46.39) 56(84) bytes of data.
​    64 bytes from 103.235.46.39: icmp_seq=1 ttl=128 time=316 ms
​    64 bytes from 103.235.46.39: icmp_seq=2 ttl=128 time=317 ms
​    64 bytes from 103.235.46.39: icmp_seq=3 ttl=128 time=300 ms
​    --- 103.235.46.39 ping statistics ---
​    3 packets transmitted, 3 received, 0% packet loss, time 2002ms
​    rtt min/avg/max/mdev = 300.216/311.260/317.404/7.839 ms

Linux—traceroute命令—跟踪数据包

traceroute命令

​ 测试从当前主机到目的主机之间经过的网络节点

​    traceroute 目标主机地址

​    示例
​    [root@localhost ~]# traceroute 103.235.46.39
​    traceroute to 103.235.46.39 (103.235.46.39), 30 hops max, 60 byte packets
​     1  gateway (20.0.0.2)  0.113 ms  0.056 ms  0.088 ms
​     2  * * *

Linux—nslookup命令—域名解析

nslookup命令

​ 测试DNS域名解析

​    nslookup 目标主机地址 [DNS服务器地址]

​    示例
​    [root@localhost ~]# nslookup www.baidu.com
​    Server:         8.8.8.8
​    Address:        8.8.8.8#53

​    Non-authoritative answer:
​    www.baidu.com   canonical name = www.a.shifen.com.
​    www.a.shifen.com        canonical name = www.wshifen.com.
​    Name:   www.wshifen.com
​    Address: 104.193.88.77
​    Name:   www.wshifen.com
​    Address: 104.193.88.123

设置网络参数的方式

临时配置------使用命令调整网络参数

​ 简单、快速、可直接修改运行中的网络参数
​ 一般只适合在调试网络的过程中使用
​ 系统重启以后,所做的修改将会失效

扫描二维码关注公众号,回复: 11549427 查看本文章

固定设置------通过配置文件修改网络参数

​ 修改各项网络参数的配置文件
​ 适合对服务其设置固定参数时使用
​ 需要重载网络服务或重启以后才会生效

Linux—ifconfig命令—设置网络接口参数

设置网络接口的IP地址、子网掩码

​    ifconfig 网络接口 ip地址 [netmask 子网掩码]
​    ifconfig 网络接口 ip地址 [/子网掩码长度]

禁用或者重新激活网卡

​    ifconfig 网络接口 up
​    ifconfig 网络接口 down

设置虚拟网络接口

​    ifconfig 网络接口:序号 IP地址    

Linux—route命令—设置路由记录

添加到指定网段的路由记录

​    route add -net 网段地址/24 gw IP地址

删除到指定网段的路由记录

​    route del -net 网段地址/24

向路由表中添加默认网关记录

​    route add default gw IP地址

删除路由表中默认的网关记录

​    route del default gw IP地址

网络接口配置文件

​    /etc/sysconfig/network-scripts/目录下

启用、禁用网络接口配置

重启network网络服务

​    [root@localhost ~]# systemctl restart network

禁用、启用网络接口

​    [root@localhost ~]# ifdown ens33   #禁用网络接口

​    [root@localhost ~]# ifup ens33   #启用网络接口

域名解析配置文件

/etc/resolv.conf文件===>可以直接填写网关

​ 保存本机需要使用的DNS服务器的IP地址

​    [root@localhost ~]# vi /etc/resolv.conf

​    Generated by NetworkManager

​    nameserver 8.8.8.8

本地主机映射文件

/etc/hosts文件===>必须要对应填写很多IP不建议使用

​ 保存主机名与IP地址的映射记录

​    [root@localhost ~]# cat /etc/hosts

​    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.
​    119.75.218.70 [www.baidu.com](http://www.baidu.com)

hosts文件和DNS服务器的比较

​ 默默情况下,系统首先从hosts文件查找解析记录
​ hosts文件只对当前的主机有效
​ hosts文件可减少DNS查询过程,从而加快访问速度

​    [root@localhost ~]# cat /etc/hosts

​    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.
​    119.75.218.70 [www.baidu.com](http://www.baidu.com)

猜你喜欢

转载自blog.csdn.net/weixin_47153668/article/details/107091226