3_获取网络接口相关的信息

Getting information about the Network interfaces

  1. igconfig命令,是net-tools软件包的一部分。

  2. ip address|addr|a show

    如果只想显示ipv4或者ipv6地址,可用:

    ip -4 address

    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
    valid_lft forever preferred_lft forever
    2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
    valid_lft 77264sec preferred_lft 77264sec

    lo表示loopback interface,enp0s3表示以太网接口,p0表示port number,s3表示slot number。

  3. DNS表示域名解析服务器。

    in bash:

    route -n #获取路由、网关 和 ip route show 命令相同

    ...

    查看DNS服务可用:

    systemd-resolve --status

    ...

  4. 使用 ifconfigip配置网络,注意该配置不是持久的,当系统重启之后会更新。

    in bash:

    ifconfig enp0s3 down #关闭enp0s3网络接口
    ifconfig enp0s3 up #打开网络接口

    or

    ip link set enp0s3 down
    ip link set enp0s3 up
    ip link show dev enp0s3 #查看某个网络接口的状态

    使用 ifconfig配置ip address

    ifconfig enp0s3 192.168.0.111/24 up

    使用 ip配置ip address

    ip address del 192.168.0.111/24 dev ens0p3 #先删除原来的ip地址
    ip address add 192.168.0.222/24 dev ens0p3 #再添加新的ip地址

    使用 route配置网关

    route del default gw 192.168.0.1 #先删除默认网关
    route add default gw 192.168.0.2 #再添加新的网关

    使用 ip配置网关

    ip route del default
    ip route add default via 192.168.0.1

    使用 ifconfig配置MAC地址

    ifconfig enp0s3 down
    ifconfig enp0s3 hw ether 08:00:27:51:05:09
    ifconfig enp0s3 up

    使用 ip配置MAC地址

    ip link set dev enp0s3 address 08:00:27:51:05:09
  5. 静态配置网络信息,编辑Netplan Yaml file。

    in bash:

    ls /etc/netplan/

    01-netconfig.yaml

    cat /etc/netplan/01-netconfig.yaml

    network:

     version: 2

     renderer: networkd

     ethernets:

      enp0s3:

       dhcp4: true

       addresses:

         - 192.168.0.20/24

       gateway4: "192.168.0.1"

       nameservers:

        addresses:

         - "8.8.8.8"

         - "8.8.4.4"

    network:是关键词。

    该关键词后跟着三个属性:

    version:是版本。

    renderer:是渲染器,表示配置网络的装置,这里设置为netwokd网络守护进程。

    ethernets:是以太网接口,后面可以定义多个以太网接口配置策略。

    enps0p3:后面的是该端口的配置策略。

    使该配置生效,可用:

    netplan apply

    yaml file use two spaces for indentation,if you are not consistant,you will get a syntax error.

  6. ping是检测网络和排错的常用命令。ping通过将 ICMP Echo Request数据报发送到指定IP并等待回复。

    一个ip地址可以设置多个域名。

    in bash:

    ping -c 4 ubuntu.com #-c表示发送的数据包的数量

    PING baidu.com (110.242.68.66) 56(84) bytes of data.
    64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=48 time=28.3 ms
    64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=48 time=28.5 ms
    64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=3 ttl=48 time=28.4 ms
    64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=4 ttl=48 time=28.6 ms
    --- baidu.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 28.262/28.434/28.574/0.112 ms

    icmp_seq是收到的数据报的编号,可以用它来检测路由错误的问题。

    ttl是the Time to live Value in the received packets,通常是源和目标之间的跃点数或路由器数。

    time又叫RTT,是数据报的往返时间。

    通常情况下,ping发送数据报的时间间隔为1秒,可以用 -i参数修改。

    ping -i 0.4 -c 4 baidu.com

    只有super user可以设置间隔小于零点的时间,不允许非特权用户如此快速的发送数据报。

    使用TTL参数 -t,可以查看相应的路由器。

    ping -t 1 -c 3 -n baidu.com #-t 1 查看第一个路由器,-n 不允许反向DNS

    PING baidu.com (110.242.68.66) 56(84) bytes of data.
    From _gateway (10.13.118.1) icmp_seq=1 Time to live exceeded
    From _gateway (10.13.118.1) icmp_seq=2 Time to live exceeded
    From _gateway (10.13.118.1) icmp_seq=3 Time to live exceeded
    --- baidu.com ping statistics ---
    3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2004ms

    如果有任何网络连接问题,可以遵循以下步骤来排错:

    首先,ping默认网关:

    route -n

    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface
    0.0.0.0 10.13.118.1 0.0.0.0 UG 100 0 0 enp0s3
    10.13.118.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3
    169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 enp0s3

    ping 10.13.118.1

    如果你ping不通,可能是你的局域网出了问题,或者是身份验证未通过。

    如果可行,ping一个互联网的公共地址,比如 8.8.8.8。

    如果ping不通,可能是你的路由器未正确配置,无法连接至互联网。

    如果可行,接着 ping一个域名,比如 baidu.com

    如果ping不通,可能是你的DNS服务未配置正确。

posted @   lyyyuanfang  阅读(27)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示