Java教程

veth技术

本文主要是介绍veth技术,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

veth peer技术

文章目录

  • veth peer技术
  • 前言
    • 创建一个命名空间
    • 创建veth接口
    • 将其中一个接口放入命名空间
    • 配置命名空间中的接口地址
    • 测试连接

前言

veth peer 技术简而言之就是创建两个接口,将这两个接口放在两个原本不能互通的地方启用后,就可以互通了。

创建一个命名空间

使用ip netns命令 --建议可以查看帮助文档了解此命令

ip netns add zhang
[root@hoyeong ~]# ip netns add zhang
[root@hoyeong ~]# ip netns list
zhang

##进阶理解
[root@hoyeong ~]# cd /var/run/netns/
[root@hoyeong netns]# ls
zhang
#命名空间的文件路径在/var/run/netns/
#也可以通过创建文件夹和做软链接的方式创建命令空间-要复杂一点

创建veth接口

使用ip link命令 --可以查看帮助文档学习此命令。

ip link add veth0 type veth peer name veth1
Garbage instead of arguments "peer ...". Try "ip link help".
[root@hoyeong ~]# ip link add veth0 type veth peer name veth1
[root@hoyeong ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:df:3a:a1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.130/24 brd 192.168.200.255 scope global noprefixroute dynamic ens33
       valid_lft 1560sec preferred_lft 1560sec
    inet6 fe80::fe8f:a78a:691f:6e62/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:df:3a:ab brd ff:ff:ff:ff:ff:ff
4: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether de:54:d8:db:23:3e brd ff:ff:ff:ff:ff:ff
5: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 9e:0e:06:a0:ab:57 brd ff:ff:ff:ff:ff:ff

将其中一个接口放入命名空间

1、添加veth

ip link set veth1 netns zhang
    link/ether 9e:0e:06:a0:ab:57 brd ff:ff:ff:ff:ff:ff
[root@hoyeong ~]# ip link set veth1 netns zhang
[root@hoyeong ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:df:3a:a1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.130/24 brd 192.168.200.255 scope global noprefixroute dynamic ens33
       valid_lft 1531sec preferred_lft 1531sec
    inet6 fe80::fe8f:a78a:691f:6e62/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:df:3a:ab brd ff:ff:ff:ff:ff:ff
5: veth0@if4: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 9e:0e:06:a0:ab:57 brd ff:ff:ff:ff:ff:ff link-netnsid 0

2、查看zhang命令空间内部的网络信息

ip netns exec zhang ip link list
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
4: veth1@if5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether de:54:d8:db:23:3e brd ff:ff:ff:ff:ff:ff link-netnsid 0
[root@hoyeong ~]# ip link

配置命名空间中的接口地址

使用ifconfig命令,需要下载网络工具–注意yum的配置

yum install -y net-tools
[root@hoyeong ~]# yum install -y net-tools
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.ustc.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.neusoft.edu.cn
base                                                                                                                                                                                                                | 3.6 kB  00:00:00     
extras                                                                                                                                                                                                              | 2.9 kB  00:00:00     
updates                                                                                                                                                                                                             | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                                                                                                                                                       | 153 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                                                                                                                                                   | 242 kB  00:00:00     
(3/4): base/7/x86_64/primary_db                                                                                                                                                                                     | 6.1 MB  00:00:01     
(4/4): updates/7/x86_64/primary_db                                                                                                                                                                                  | 8.8 MB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================================================================================================================
 Package                                                Arch                                                Version                                                                Repository                                         Size
===========================================================================================================================================================================================================================================
Installing:
 net-tools                                              x86_64                                              2.0-0.25.20131004git.el7                                               base                                              306 k

Transaction Summary
===========================================================================================================================================================================================================================================
Install  1 Package

Total download size: 306 k
Installed size: 917 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.25.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for net-tools-2.0-0.25.20131004git.el7.x86_64.rpm is not installed
net-tools-2.0-0.25.20131004git.el7.x86_64.rpm                                                                                                                                                                       | 306 kB  00:00:00     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-7.1908.0.el7.centos.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                                                                                                               1/1 
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                                                                                                               1/1 

Installed:
  net-tools.x86_64 0:2.0-0.25.20131004git.el7                                                                                                                                                                                              

Complete!

配置地址-注意要使用同一个网段

ip netns exec zhang ifconfig veth1 192.168.200.2/24 up 
[root@hoyeong ~]# ip netns exec zhang ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
4: veth1@if5: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
    link/ether de:54:d8:db:23:3e brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.1.0.2/24 brd 10.1.0.255 scope global veth1
       valid_lft forever preferred_lft forever

测试连接

ping 192.168.200.2
[root@hoyeong ~]# ping 192.168.200.2
PING 192.168.200.2 (192.168.200.2) 56(84) bytes of data.
64 bytes from 192.168.200.2: icmp_seq=1 ttl=128 time=0.217 ms
64 bytes from 192.168.200.2: icmp_seq=2 ttl=128 time=0.108 ms
64 bytes from 192.168.200.2: icmp_seq=3 ttl=128 time=0.326 ms
64 bytes from 192.168.200.2: icmp_seq=4 ttl=128 time=0.216 ms
^C
--- 192.168.200.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.108/0.216/0.326/0.079 ms
这篇关于veth技术的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!