1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > linux网络设置与基础服务命令(ifconfig hostname route netstat ss ping traceroute nslookup route)

linux网络设置与基础服务命令(ifconfig hostname route netstat ss ping traceroute nslookup route)

时间:2020-01-06 19:47:10

相关推荐

linux网络设置与基础服务命令(ifconfig hostname route netstat ss ping traceroute nslookup route)

文章目录

linux网络设置与基础服务前言查看网络配置使用ifconfig命令查看网络接口地址查看指定网络接口信息使用 hostname命令查看当前主机名称使用route命令查看路由表条目使用netstat命令查看网络连接情况测试网络连接使用ping命令测试网络连通性使用traceroute命令跟踪数据包的路由途径使用nslookup命令测试DNS域名解析域名解析 nslookup使用ifconfig命令修改网卡地址,状态使用route命令设置路由记录修改网络配置文件域名解析配置文件本地主机映射文件

linux网络设置与基础服务

前言

查看及测试网络是管理linux网络服务的第一步

查看网络配置

使用ifconfig命令查看网络接口地址

[root@localhost ~]# yum install net-tools 安装ifconfig命令[root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 第一块以太网名称inet 192.168.136.206 netmask 255.255.255.0 broadcast 192.168.136.255inet6 fe80::7b77:7163:2178:608c prefixlen 64 scopeid 0x20<link>ether 00:0c:29:81:bd:e1 txqueuelen 1000 (Ethernet)RX packets 2128 bytes 180947 (176.7 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 1756 bytes 167874 (163.9 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1000 (Local Loopback)RX packets 72 bytes 6272 (6.1 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 72 bytes 6272 (6.1 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0inet 192.168.136.206 Ip地址netmask 255.255.255.0 子网掩码broadcast 192.168.136.255 广播地址ether 00:0c:29:81:bd:e1 mac地址

IP地址默认为 “127.0.0.1”,回环地址通常仅用于对本机的网络测试’

[root@localhost ~]# ifconfig -a 查看所有的网络接口ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.136.206 netmask 255.255.255.0 broadcast 192.168.136.255inet6 fe80::7b77:7163:2178:608c prefixlen 64 scopeid 0x20<link>ether 00:0c:29:81:bd:e1 txqueuelen 1000 (Ethernet)RX packets 2156 bytes 183247 (178.9 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 1781 bytes 171244 (167.2 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1000 (Local Loopback)RX packets 72 bytes 6272 (6.1 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 72 bytes 6272 (6.1 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

查看指定网络接口信息

[root@localhost ~]# ifconfig ens33ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.136.206 netmask 255.255.255.0 broadcast 192.168.136.255inet6 fe80::7b77:7163:2178:608c prefixlen 64 scopeid 0x20<link>ether 00:0c:29:81:bd:e1 txqueuelen 1000 (Ethernet)RX packets 2220 bytes 187995 (183.5 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 1824 bytes 175952 (171.8 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0inet 192.168.136.206 Ip地址netmask 255.255.255.0 子网掩码broadcast 192.168.136.255广播地址inet6 fe80::7b77:7163:2178:608c mac地址

使用 hostname命令查看当前主机名称

[root@localhost ~]# hostname192.168.136.206

永久修改当前主机名

[root@localhost ~]# hostnamectl set-hostname zhang[root@localhost ~]# hostnamezhang

使用route命令查看路由表条目

Kernel IP routing tableDestinationGateway Genmask Flags Metric Ref Use Ifacedefault 192.168.136.1 0.0.0.0 UG 100 0 0 ens33192.168.136.0 0.0.0.0 255.255.255.0 U100 0 0 ens33192.168.136.0 0.0.0.0 255.255.255.0 U100 0 0 ens33

Destination列对应目标网段的地址’

‘Gateway列对应下一跳路由器地址’

‘Genmask列对应子网掩码’

‘Iface列对应发送数据的网络接口’

‘目标网段为default时,表示此行时默认网关记录’

使用netstat命令查看网络连接情况

常用选项-a:显示当前主机中所有活动的网络连接信息(包括监听,非监听状态的服务端口)-n:以数字的形式显示相关的主机地址-p:显示与网络连接相关联的进程号,进程名称信息('该选项需要root权限')-t:查看TCP协议相关信息-u:显示UDP协议相关的信息-r:显示路由信息-l:显示处于监听状态

通常使用“-ntap”组合选项,以数字形式显示当前系统中所有的TCP连接信息,同时显示对应的进程信息。

[root@localhost ~]# netstat -ntapActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 00 0.0.0.0:22 0.0.0.0:*LISTEN9243/sshd tcp 00 127.0.0.1:25 0.0.0.0:*LISTEN9519/master tcp 00 192.168.136.190:22192.168.136.2:62516ESTABLISHED 10622/sshd: root@pt tcp 00 192.168.136.190:22192.168.136.2:49360ESTABLISHED 10962/sshd: root@pt tcp6 00 :::22 :::*LISTEN9243/sshd tcp6 00 ::1:25 :::*LISTEN9519/master

结合grep命令和管道符号可以过滤22端口

[root@localhost ~]# netstat -ntap | grep 22tcp 00 0.0.0.0:22 0.0.0.0:*LISTEN9243/sshd tcp 00 192.168.136.190:22192.168.136.2:62516ESTABLISHED 10622/sshd: root@pt tcp 036 192.168.136.190:22192.168.136.2:49360ESTABLISHED 10962/sshd: root@pt tcp6 00 :::22 :::*LISTEN9243/sshd

ss 获取socket统计信息

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

ss [选项]

1

常用选项:

-t:TCP协议​ -u:UDP协议​ -n:显示端口号​ -l:监听状态​ -p:显示PID进程号​ -a:所有信息​ -r:显示名称,默认不填写该选项

【注意】:ss 与 netstat区别是什么?

netstat命令使用与连接数不超过1万的场合

ss命令适用于高并发连接的场合

测试网络连接

使用ping命令测试网络连通性

[root@localhost ~]# ping 192.168.136.1PING 192.168.136.1 (192.168.136.1) 56(84) bytes of data.64 bytes from 192.168.136.1: icmp_seq=1 ttl=128 time=0.298 ms64 bytes from 192.168.136.1: icmp_seq=2 ttl=128 time=0.321 ms64 bytes from 192.168.136.1: icmp_seq=3 ttl=128 time=0.349 ms64 bytes from 192.168.136.1: icmp_seq=4 ttl=128 time=0.252 ms64 bytes from 192.168.136.1: icmp_seq=5 ttl=128 time=0.770 ms

使用traceroute命令跟踪数据包的路由途径

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

对于无法响应的节点,连接状态将显示为*

traceroute命令比ping命令更准确的定位网络连接的故障点(中断点),执行速度也因此比ping命令慢

使用nslookup命令测试DNS域名解析

测试DNS域名解析,将域名解析为IP地址

[root@localhost ~]# nslookup Server:192.168.136.1Address:192.168.136.1#53以下为DNS解析的反馈结果Non-authoritative answer:canonical name = www..Name:www.Address: 112.80.248.75Name:www.Address: 112.80.248.76

域名解析 nslookup

nslookup命令(nameserver)

测试DNS域名解析

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

示例:

[root@localhost ~]# nslookup Server:8.8.8.8Address:8.8.8.8#53Non-authoritative answer:canonical name = www..www.canonical name = .Name:Address: 104.193.88.123 'DNS服务器地址'Name:Address: 104.193.88.77 '解析的IP地址'

使用ifconfig命令修改网卡地址,状态

ifconfig命令很强大不仅可以查看网卡配置,修改网卡IP地址,子网掩码,绑定网卡接口,激活或停止网络接口

ifconfig网络接口名称 ip地址【netmake 子网掩码】ifconfig网络接口名称 ip地址【/子网掩码长度】[root@localhost ~]# ifconfig ens33 192.168.136.210/24[root@localhost ~]# ifconfig ens33 192.168.136.210 netmask 255.255.255.0

禁用或者重新激活网卡

ifconfig 网络接口 upifconfig 网络接口 down

为网卡绑定虚拟接口

在对服务器网络进行调试的过程中,有时候需要临时在同一个网卡上使用一个新的IP地址,但是又不能够覆盖掉原本的IP地址而导致服务程序不可用。

ifconfig 网络接口:序号 IP地址[root@localhost ~]# ifconfig ens33:1 140.140.140.140ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 140.140.140.140 netmask 255.255.0.0 broadcast 140.140.255.255ether 00:0c:29:0f:7d:45 txqueuelen 1000 (Ethernet)

使用route命令设置路由记录

使用route命令不仅可以用于查看路由表信息,还可以用来添加,删除静态的路由表条目

添加路由段

route add -net 网段地址 gw IP地址[root@192 ~]# route add -net 192.168.40.0/24 gw 192.168.136.10 '添加静态路由,本机访问另一个网段192.168.30.0/24的数据都发给192.168.136.10'[root@192 ~]# routeKernel IP routing tableDestinationGateway Genmask Flags Metric Ref Use Ifacedefault 192.168.136.30 0.0.0.0 UG 00 0 ens33default 192.168.136.1 0.0.0.0 UG 100 0 0 ens33140.140.0.00.0.0.0 255.255.0.0U00 0 ens33140.140.0.00.0.0.0 255.255.0.0U100 0 0 ens33192.168.40.0 192.168.136.10 255.255.255.0 UG 00 0 ens33192.168.136.0 0.0.0.0 255.255.255.0 U100 0 0 ens33

删除路由段

route del -net 网段地址[root@localhost ~]# route del -net 192.168.400.0/24'删除静态路由'[root@192 ~]# routeKernel IP routing tableDestinationGateway Genmask Flags Metric Ref Use Ifacedefault 192.168.136.1 0.0.0.0 UG 100 0 0 ens33140.140.0.00.0.0.0 255.255.0.0U00 0 ens33192.168.136.0 0.0.0.0 255.255.255.0 U100 0 0 ens33

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

route add default gw IP地址[root@localhost ~]# route add default gw 192.168.10.70'添加到192.168.10.0的默认网关记录'[root@192 ~]# route add default gw 192.168.136.30[root@192 ~]# routeKernel IP routing tableDestinationGateway Genmask Flags Metric Ref Use Ifacedefault 192.168.136.30 0.0.0.0 UG 00 0 ens33default 192.168.136.1 0.0.0.0 UG 100 0 0 ens33

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

route del default gw IP地址[root@localhost ~]# route del default gw 192.168.136.70'删除到192.168.10.30的默认网关记录'[root@192 ~]# routeKernel IP routing tableDestinationGateway Genmask Flags Metric Ref Use Ifacedefault 192.168.136.1 0.0.0.0 UG 100 0 0 ens33140.140.0.00.0.0.0 255.255.0.0U00 0 ens33140.140.0.00.0.0.0 255.255.0.0U100 0 0 ens33

修改网络配置文件

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

回环接口lo的配置文件是“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.136.20"设置网络接口的IP地址NETMASK="255.255.255.0"设置网络接口的子网掩码GATEWAY="192.168.158.1"设置网络接口的默认网关地址

域名解析配置文件

/etc/resolv.conf文件,保存本机需要使用的DNS服务器的IP地址

[root@192 ~]# vi /etc/resolv.conf# Generated by NetworkManagersearch localdomainnameserver 192.168.136.1

本地主机映射文件

/etc/hosts文件;保存主机名与IP地址的映射记录,一般保存经常需要访问的主机信息,访问先查映射在查DNS

[root@192 ~]# vi /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。