Linux网络服务-----测试网络命令与DHCP服务

目录

 

一.ifconfig---查看网络接口信息

二.永久修改主机名称

三.route-----查看路由表条目

四.netstat-----查看网络连接的情况

五.traceroute-----跟踪数据包

六.nslookup-----域名解析

七.ifconfig-----设置网络接口参数

八.route-----设置路由记录

九.DHCP服务


一.ifconfig---查看网络接口信息

  • 直接输入ifconfig命令,查看所有活动网络接口的信息
  • ifconfig 网卡,查看指定网络接口信息
  • 实例如下
[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.133  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::fe27:a784:dd8d:8487  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:de:a0:aa  txqueuelen 1000  (Ethernet)
        RX packets 95615  bytes 135044639 (128.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 37149  bytes 2870389 (2.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# 

二.永久修改主机名称

  • 使用hostnamectl命令
  • 实例如下
[root@fpx ~]# hostnamectl status      //查看系统基本信息
   Static hostname: fpx
   Pretty hostname: FPX
         Icon name: computer-vm
           Chassis: vm
        Machine ID: dec81954d1114e09bb2764d831144880
           Boot ID: 48d4c49ff069497e849570f929534d8e
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-693.el7.x86_64
      Architecture: x86-64
[root@fpx ~]# hostnamectl set-hostname centos7   //永久修改主机名
[root@fpx ~]# su           //刷新
[root@centos7 ~]# 

  • 修改配置文件(/etc/hostname)
  • 实例如下
[root@centos7 ~]# cat /etc/hostname
centos7

[root@centos7 ~]# vim /etc/hostname
localhost
~                                                                   
~   
[root@centos7 ~]

修改之后需要重启,主机名才能修改

  • 使用nmtui终端工具,打开图形界面修改主机名
  • 实例如下
[root@localhost ~]# nmtui

三.route-----查看路由表条目

  • 直接执行route命令可以查看当前主机中的路由表信息,如下
[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.43.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 ~]#
 
destination表示目标网段的地址,gateway对应下一跳路由器的地址,default表示此行是默认网关记录
  • route -n,将路有记录中的地址显示为数字形式,如下
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.43.2    0.0.0.0         UG    100    0        0 ens33
192.168.43.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 ~]# 

四.netstat-----查看网络连接的情况

  • netstat命令,查看系统的网络连接状态、路由表、接口统计信息
  • -a:显示当前主机中所有活动的网络连接状态、路由表、接口统计信息
  • -n:以数字形式显示相关的主机地址、端口等信息
  • -p:显示与网络连接相关联的进程号、进程名称信息(选择root权限)
  • -t:查看TCP协议相关的信息
  • -u:查看UDP协议相关的信息
  • 查看tcp先关联的端口,进程,实例如下:
[root@localhost ~]# netstat -natp
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      1/systemd           
tcp        0      0 192.168.100.99:80       0.0.0.0:*               LISTEN      43500/httpd         
tcp        0      0 192.168.100.99:53       0.0.0.0:*               LISTEN      42348/named         
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      42348/named         
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1346/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1051/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1052/cupsd          
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      42348/named         
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1229/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 ::1:53                  :::*                    LISTEN      42348/named         
tcp6       0      0 :::22                   :::*                    LISTEN      1051/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1052/cupsd          
tcp6       0      0 ::1:953                 :::*                    LISTEN      42348/named         
tcp6       0      0 ::1:25                  :::*                    LISTEN      1229/master         
[root@localhost ~]# 
  • 查看udp相关联的端口,进程,实例如下:
[root@localhost ~]# netstat -naup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
udp        0      0 192.168.122.1:53        0.0.0.0:*                           42348/named         
udp        0      0 192.168.100.99:53       0.0.0.0:*                           42348/named         
udp        0      0 127.0.0.1:53            0.0.0.0:*                           42348/named         
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1346/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1346/dnsmasq        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           41944/dhclient      
udp        0      0 0.0.0.0:39500           0.0.0.0:*                           618/avahi-daemon: r 
udp        0      0 192.168.100.99:22145    198.41.0.4:53           ESTABLISHED 42348/named         
udp        0      0 192.168.100.99:23187    198.41.0.4:53           ESTABLISHED 42348/named         
udp        0      0 192.168.100.99:62666    198.41.0.4:53           ESTABLISHED 42348/named         
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           618/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           597/chronyd         
udp6       0      0 ::1:53                  :::*                                42348/named         
udp6       0      0 ::1:323                 :::*                                597/chronyd         
[root@localhost ~]# 

五.traceroute-----跟踪数据包

  • traceroute命令,测试从当前主机到目的主机之间经过的网络结点
  • 使用格式:traceroute + 目标IP地址,实例如下:
[root@host2 ~]# traceroute 192.168.100.99
traceroute to 192.168.100.99 (192.168.100.99), 30 hops max, 60 byte packets
 1  gateway (192.168.20.1)  8.917 ms  19.319 ms  96.157 ms
 2  192.168.100.99 (192.168.100.99)  194.701 ms  203.973 ms  214.894 ms
[root@host2 ~]# 

六.nslookup-----域名解析

  • nslookup命令,测试DNS域名解析
  • 使用格式:nslookup + 目标主机的域名,实例如下:
[root@localhost ~]# nslookup www.taobao.com
Server:		192.168.43.2
Address:	192.168.43.2#53

Non-authoritative answer:
www.taobao.com	canonical name = www.taobao.com.danuoyi.tbcache.com.
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 111.3.79.234
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 111.3.79.235
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 211.138.124.237
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 2409:8c28:2808:5:3::3fa
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 2409:8c28:2808:5:3::3f9
Name:	www.taobao.com.danuoyi.tbcache.com
Address: 2409:8c28:801:3:3::3f9

[root@localhost ~]# 

七.ifconfig-----设置网络接口参数

  • 设置网卡的IP地址和子网掩码: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
        inet6 fe80::94fa:2735:a1c:2b37  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:92:43:7a  txqueuelen 1000  (Ethernet)
        RX packets 268  bytes 22312 (21.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 146  bytes 16065 (15.6 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 65  bytes 5617 (5.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 65  bytes 5617 (5.4 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:6c:2b:02  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 ~]# 
  • 禁用、重新激活网卡,示例如下:
[root@localhost ~]# ifconfig ens33 down
[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 65  bytes 5617 (5.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 65  bytes 5617 (5.4 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:6c:2b:02  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
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.136  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::94fa:2735:a1c:2b37  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:92:43:7a  txqueuelen 1000  (Ethernet)
        RX packets 1190  bytes 83666 (81.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 297  bytes 30962 (30.2 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 69  bytes 5965 (5.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 69  bytes 5965 (5.8 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:6c:2b:02  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 ~]# 

注:ifdown ens33 禁用网卡;ifup ens33 启用网卡

  • 设置虚拟网络接口
  • 使用格式:ifconfig    网卡名称:序号  IP地址,实例如下:
[root@localhost ~]# ifconfig ens33:1 10.10.10.10
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.136  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::94fa:2735:a1c:2b37  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:92:43:7a  txqueuelen 1000  (Ethernet)
        RX packets 1242  bytes 86848 (84.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 315  bytes 33297 (32.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.10.10  netmask 255.0.0.0  broadcast 10.255.255.255
        ether 00:0c:29:92:43:7a  txqueuelen 1000  (Ethernet)

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 69  bytes 5965 (5.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 69  bytes 5965 (5.8 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:6c:2b:02  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 ~]# 

八.route-----设置路由记录

  • 添加到指定网段的路由记录:route add -net 网段地址 gw IP地址,实例如下:
[root@localhost ~]# route add -net 192.168.3.0/24 gw 10.10.10.10
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.43.2    0.0.0.0         UG    100    0        0 ens33
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 ens33
192.168.3.0     10.10.10.10     255.255.255.0   UG    0      0        0 ens33
192.168.43.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 -net 网段地址,实例如下:
[root@localhost ~]# route del -net 192.168.3.0/24
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.43.2    0.0.0.0         UG    100    0        0 ens33
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 ens33
192.168.43.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 del default gw IP地址
  • 向路由表中添加默认网关记录:route add default gw IP地址
  • gw后面跟下一跳地址

注:/etc/resolv.conf  域名解析配置文件;/etc/hosts 本地主机映射文件(可以修改域名映射)

九.DHCP服务

  • DHCP(动态主机配置协议)是用来自动分发IP地址的协议,DHCP避免了IP地址冲突,提高了IP地址的利用率
  • DHCP的租约过程:1,客户机在网络发出一个discover广播搜索服务器;2,服务器收到广播之后,向客户端响应了一个offer广播;3,客户端收到offer之后,向服务器发出request广播请求;4,服务器向客户端发送ACK确认广播
  • 租约一般是8天,但是到第四天时就必须开始续约
  • 更新租约时,客户机直接向提供租约的服务期发送DHCP request包,要求更新现有的地址租约
  • dhcp软件的主要文件,实例如下:
[root@localhost ~]# rpm -qc dhcp
/etc/dhcp/dhcpd.conf
/etc/dhcp/dhcpd6.conf
/etc/openldap/schema/dhcp.schema
/etc/sysconfig/dhcpd
/var/lib/dhcpd/dhcpd.leases
/var/lib/dhcpd/dhcpd6.leases
[root@localhost ~]# 

  • 主配置文件:/etc/dhcp/dhcpd.conf;主配置文件的内容构成,如下:
subnet 192.168.100.0 netmask 255.255.255.0 {     //局部配置参数,网段声明
        range 192.168.100.10 192.168.100.100;    //IP地址分发范围
        option routers 192.168.100.1;            //网关地址
        option domain-name-servers 192.168.100.99;   //DNS服务IP地址
        default-lease-time 600;          //默认租约期限
        max-lease-time 7200;        //最大租约期限
        host dns {             //host主机声明,作用单个主机,指定IP地址
        hardware ethernet 00:0c:29:0b:69:b8; //该指定IP主机的MAC地址
        fixed-address 192.168.100.99;//指定的IP地址
    }
}
  • 启动的DHCP服务的详细步骤
1.安装DHCP的相关安装包
yum install dhcp* -y
2.给服务器配置静态的IP地址,添加IP地址
vim /etc/sysconfig/network-scripts/ifcfg-ens33
3.复制dhcp主配置文件的模板
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
4.编辑主配置文件
vim /etc/dhcp/dhcpd.conf
参数如下:
subnet 192.168.10.0 255.255.255.0 {
range 192.168.10.100 192.168.10.200;
option routers 192.168.10.1;
option domian-name-servers 192.168.100.99;
default-lease-time 7200
max-lease-time 620
host ns {
      hardware ethernet MAC地址;
      fixed-address 192.168,100.88;
 }
}

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值