10.11 Linux网络相关

时间:2022-04-27
本文章向大家介绍10.11 Linux网络相关,主要内容包括linux网络相关、ifconfig命令 参数 -a、ifup/ifdown命,启动/关闭网卡、增加虚拟的网卡,然后给虚拟网卡设定IP、mii-tool命令查看网卡是否连接网线、hostnamectl命令更改主机名、/etc/hosts文件、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

linux网络相关

  • ifconfig查看网卡ip(yum install net-tools)
  • ifup ens33/ifdown ens33
  • 设定虚拟网卡ens33:1
  • mii-tool ens33 查看网卡是否连接
  • ethtool ens33 也可以查看网卡是否连接
  • 更改主机名 hostnamectl set-hostname aminglinux
  • DNS配置文件/etc/resolv.conf
  • /etc/hosts文件

ifconfig命令

  • 在centos7中,只能只用默认的 ip addr命令查看IP
  • ifconfig命令,查看网卡IP
    • 在centos6中是默认就存在的
    • 在centos7中则需要安装——>yum install net-tools
[root@localhost ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.130  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::20c:29ff:feff:458f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ff:45:8f  txqueuelen 1000  (Ethernet)
        RX packets 3131  bytes 295354 (288.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 345  bytes 37930 (37.0 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 0  (Local Loopback)
        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 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ff:45:8f brd ff:ff:ff:ff:ff:ff
    inet 192.168.202.130/24 brd 192.168.202.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feff:458f/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost ~]# 
  • ifconfig命令和ip addr命令,相互比较
    • 两者显示的效果是相同的
      • ifconfig命令,显示的更加 清爽
      • ip add命令,看起来比较乱

ifconfig命令 参数 -a

  • ifconfig -a 表示当你的网卡dang掉的时候(没有ip的时候,就不会显示网卡),但在加上-a参数后,就可查看到

ifup/ifdown命,启动/关闭网卡

  • ifup eno16777736 启动网卡
  • ifdown eno16777736 关闭网卡
  • 若是在终端设置网卡,则需要两个命令同时进行(否则,在终端关闭网卡后,就需要在本机上重启网卡了)
    • ifdown eno16777736 && ifup eno16777736
[root@localhost ~]# ifdown eno16777736 && ifup eno16777736  //关闭网卡后并重新启动网卡
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@localhost ~]# 

增加虚拟的网卡,然后给虚拟网卡设定IP

  • 需求
    • 增加虚拟的网卡,然后给虚拟网卡设定IP
  • 做法:
  1. 首先到网卡配置文件里拷贝下
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-eno16777736  ifdown-isdn      ifdown-tunnel  ifup-isdn    ifup-Team
ifcfg-lo           ifdown-post      ifup           ifup-plip    ifup-TeamPort
ifdown             ifdown-ppp       ifup-aliases   ifup-plusb   ifup-tunnel
ifdown-bnep        ifdown-routes    ifup-bnep      ifup-post    ifup-wireless
ifdown-eth         ifdown-sit       ifup-eth       ifup-ppp     init.ipv6-global
ifdown-ippp        ifdown-Team      ifup-ippp      ifup-routes  network-functions
ifdown-ipv6        ifdown-TeamPort  ifup-ipv6      ifup-sit     network-functions-ipv6
[root@localhost network-scripts]# cp ifcfg-eno16777736 ifcfg-eno16777736:0    //增加反斜杠就为了脱义冒号
  1. 然后进入到编辑文件
[root@localhost network-scripts]# vi !$
vi ifcfg-eno16777736:0

更改其中的网卡名称和IP、DNS1和网关GATEWAY都可删除(可删除)
将NAME=eno16777736名称更改为NAME=eno16777736:0——>配置文件中的 冒号 ,就不需要脱义了
添加DEVICE=eno16777736:0——>这一步若不添加,我的虚拟网卡实现不了
将IP地址IPADDR=192.168.202.130更改为192.168.202.150(这里面的ip可随意更改)
DNS1和网关GATEWAY都可删除(可删除)——>因为已经有了DNS和网关GATEWAY了

然后退出保存
  1. 这时关闭,重启网卡,然后在查看,就会看到虚拟网卡的存在
[root@localhost network-scripts]# ifdown eno16777736 && ifup eno16777736
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/Act
[root@localhost network-scripts]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.130  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::20c:29ff:feff:458f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ff:45:8f  txqueuelen 1000  (Ethernet)
        RX packets 4180  bytes 393171 (383.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 982  bytes 138704 (135.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.150  netmask 255.255.255.0  broadcast 192.168.202.255
        ether 00:0c:29:ff:45:8f  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 0  (Local Loopback)
        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 network-scripts]# 
  1. 这时在自己的window物理机上去ping 192.168.202.150,会发现可以连接虚拟网卡的IP
  • 在lvs和keepalived的时候,会使用到虚拟网卡

mii-tool命令查看网卡是否连接网线

  • 一台服务器,连接了网线,现在我要知道这个网卡有没有插着网线,我自己本身不确定
    • 在机房中,可以查看网卡灯是否亮着
    • 但在系统中,可以使用mii-tool eno16777736 命令查看是否是link ok
  • 格式:mii-tool 网卡名称 查看网卡是否连接网线
[root@localhost ~]# mii-tool eno16777736
eno16777736: negotiated 1000baseT-FD flow-control, link ok
[root@localhost ~]# 
  • 这里若显示为 link ok 或者 no link ——>另外会有些机器不支持,提示not support
    • 这时候就可以使用另外一个命令查看ethtool eno16777736
      • 查看最底部 Link detected 是否为 yes

hostnamectl命令更改主机名

  • hostnamectl命令,更改主机名
    • centos6中,不支持该命令
[root@localhost ~]# hostnamectl set-hostname hanfeng-001
[root@localhost ~]# hostname    //在当前终端下,使用hostname命令,查看主机名
hanfeng-001
[root@localhost ~]# 

但会发主机名并没有立即生效,需要退出重新登录终端,或者进入一个子shell(输入一个bash即可)
[root@localhost ~]# bash
[root@hanfeng-001 ~]#         //就会看到主机名变更了

主机名配置文件存放位置

  • 主机名的配置文件存放位置 /etc/hostname
[root@hanfeng-001 ~]# cat /etc/hostname
hanfeng-001
[root@hanfeng-001 ~]# 

DNS配置文件存放位置

  • DNS配置文件存放位置 /etc/resolv.conf
[root@hanfeng-001 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29
[root@hanfeng-001 ~]# 
  • 谷歌的DNS为8.8.8.8
  • 我们在更改DNS的时候,只需要去更改网卡配置文件即可/etc/sysconfig/network-scripts/ifcfg-eno16777736
    • 当然也可以临时的编辑 /etc/resolv.conf 这个文件。但是更改/etc/resolv.conf 文件之后 ,在我们重启网卡后,它依旧会被网卡里面的配置文件 DNS 所覆盖

/etc/hosts文件

  • /etc/hosts这个文件是linux和window中都有的一个文件
  • 格式:左边是IP ,右边是域名
    • 支持一个IP,配多个域名(用 格 分开即可)
    • 若是一个域名,有多个IP,则会 以后面的生效——>若是前面有,后面也有,则以后面的为主
[root@hanfeng-001 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[[root@hanfeng-001 ~]# ping www.qq123.com
PING www.qq123.com (202.91.250.93) 56(84) bytes of data.
64 bytes from 202.91.250.93: icmp_seq=1 ttl=128 time=11.6 ms
64 bytes from 202.91.250.93: icmp_seq=2 ttl=128 time=11.3 ms
64 bytes from 202.91.250.93: icmp_seq=3 ttl=128 time=11.7 ms
64 bytes from 202.91.250.93: icmp_seq=4 ttl=128 time=11.8 ms
^C
--- www.qq123.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 11.363/11.652/11.811/0.172 ms
[root@hanfeng-001 ~]# 
  • 其中的202.91.250.93是一个公网IP,我想在我本机不需要访问到这个IP
    • 编辑/etc/hosts文件
[root@hanfeng-001 ~]# vim /etc/hosts

在/etc/hosts文件添加    192.168.202.150 www.qq123.com

[root@hanfeng-001 ~]# ping www.qq123.com
PING www.qq123.com (192.168.202.150) 56(84) bytes of data.
64 bytes from www.qq123.com (192.168.202.150): icmp_seq=1 ttl=64 time=0.100 ms
64 bytes from www.qq123.com (192.168.202.150): icmp_seq=2 ttl=64 time=0.045 ms
64 bytes from www.qq123.com (192.168.202.150): icmp_seq=3 ttl=64 time=0.042 ms
^C
--- www.qq123.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.042/0.062/0.100/0.027 ms
[root@hanfeng-001 ~]# 
  • 这里会发现直接解析到192.168.202.150这个IP这个IP上——>只在本机上生效
    • 在window上是无效的