uplaod youdaonotes
This commit is contained in:
494
YueQian/MarkdownNotes/CQ2605/01Linux基础/05_网络配置.md
Normal file
494
YueQian/MarkdownNotes/CQ2605/01Linux基础/05_网络配置.md
Normal file
@@ -0,0 +1,494 @@
|
||||
# **1. 虚拟机VMware的网络配置**
|
||||
|
||||
虚拟机的网络适配器的模式有两种:
|
||||
|
||||
- 桥接模式:使得虚拟机跟物理机一样,各自拥有独立的IP地址
|
||||
|
||||
- NAT模式:使得虚拟机直接共享物理机的IP,自己本身没有独立的IP地址
|
||||
|
||||

|
||||
|
||||
虚拟机VMware的网卡配置
|
||||
|
||||
以上两种模式都可以联网,区别就是虚拟机要不要一个独立的IP。对于桥接模式需要注意,必须要仔细选择桥接的物理网卡,否则会出现无法联网的情形。
|
||||
|
||||

|
||||
|
||||
虚拟网卡的桥接配置
|
||||
|
||||
## **1.1 桥接模式(推荐使用)**
|
||||
|
||||
所谓桥接,是指虚拟网卡通过物理机的某一个网络接口,直接连接到路由器去获取
|
||||
|
||||
例如:
|
||||
|
||||
- 通过桥接到 WiFi 来联网。
|
||||
|
||||
- 通过桥接到 以太网 来连接开发板
|
||||
|
||||
如下图所示,在
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
选择具体所需的桥接网卡
|
||||
|
||||

|
||||
|
||||
**重置**
|
||||
|
||||
有时,如果网络参数经过多次配置导致无法再正确联网,可以通过点击下图中下方图表
|
||||
|
||||

|
||||
|
||||
## **1.2 NAT模式**
|
||||
|
||||
如果虚拟机不需要单独的IP地址,那么可以通过
|
||||
|
||||

|
||||
|
||||
选择具体所需的桥接网卡
|
||||
|
||||
此时的虚拟网卡
|
||||
|
||||
## **1.3 仅主机模式**
|
||||
|
||||
有时限于外部网络的局限性,在Ubuntu无需联外网而需与宿主机Windows通信时,将 VMware 虚拟网卡配置为
|
||||
|
||||

|
||||
|
||||
仅主机模式
|
||||
|
||||
在此模式中,虚拟网卡
|
||||
|
||||

|
||||
|
||||
VMnet1虚拟路由
|
||||
|
||||
从上图可以看到,
|
||||
|
||||

|
||||
|
||||
VMnet1虚拟路由
|
||||
|
||||
有了各自的
|
||||
|
||||
[](http://vm.yueqian.com.cn:8886/previewCourses.html?courseId=1331899420259463169&courseName=Linux%E5%9F%BA%E7%A1%80#0)
|
||||
|
||||
# **2. Ubuntu网络配置**
|
||||
|
||||
## 终端命令配置
|
||||
|
||||
给虚拟机 VMware 选择了某一种联网模式后,一般还需要给客户机(即Ubuntu主机)配置好如下网络参数才能正常联网:
|
||||
|
||||
- 网卡名称
|
||||
|
||||
> eth0
|
||||
> ens33
|
||||
> ens32
|
||||
|
||||
|
||||
- IP地址
|
||||
|
||||
> 在当前路由下所获得的主机IP(IPv4 IPv6)//192.168.2.x
|
||||
|
||||
|
||||
- 子网掩码
|
||||
|
||||
> A类IP:255.0.0.0
|
||||
> B类IP:255.255.0.0
|
||||
> C类IP:255.255.255.0 //现使用
|
||||
|
||||
|
||||
- 网关地址
|
||||
|
||||
> 当前路由的地址:192.168.2.1
|
||||
|
||||
|
||||
- DNS(域名解析服务)
|
||||
|
||||
[国内/国外公共DNS服务器地址(Ipv4/Ipv6)大全 (config.net.cn)](https://config.net.cn/tools/Dns.html)
|
||||
|
||||
## **2.1 经典Linux网络配置**
|
||||
|
||||
**Ubuntu24.04配置流程详见本教程第4节**
|
||||
|
||||
在
|
||||
|
||||
1. 设置固定IP:
|
||||
|
||||
```bash
|
||||
gec@ubuntu:~$ cat /etc/network/interfaces
|
||||
# interfaces(5) file used by ifup(8) and ifdown(8)
|
||||
auto lo
|
||||
iface lo inet loopback
|
||||
|
||||
auto ens33
|
||||
iface ens33 inet static
|
||||
address 169.254.54.200 # IP地址,根据具体的网络环境来写
|
||||
netmask 255.255.0.0 # 子网掩码
|
||||
gateway 169.254.54.1 # 网关地址
|
||||
gec@ubuntu:~$
|
||||
gec@ubuntu:~$
|
||||
```
|
||||
|
||||
1. 设置动态IP(自动获取IP)
|
||||
|
||||
```bash
|
||||
gec@ubuntu:~$ cat /etc/network/interfaces
|
||||
# interfaces(5) file used by ifup(8) and ifdown(8)
|
||||
auto lo
|
||||
iface lo inet loopback
|
||||
|
||||
auto ens33
|
||||
iface ens33 inet dhcp
|
||||
gec@ubuntu:~$
|
||||
gec@ubuntu:~$
|
||||
```
|
||||
|
||||
**注意1**
|
||||
|
||||
一般而言,只有在虚拟机设定为桥接模式下,才需要配置Ubuntu的网络参数信息,如果虚拟机设定的是NAT模式,那么一般直接重启网络服务,让其自动获取网络参数即可。
|
||||
|
||||
**注意2**
|
||||
|
||||
以上示例以虚拟网卡
|
||||
|
||||
```bash
|
||||
安装ifconfig
|
||||
sudo apt install net-tools
|
||||
```
|
||||
|
||||

|
||||
|
||||
ifconfig命令查看当前活跃的网卡名称
|
||||
|
||||
**重启网络服务**
|
||||
|
||||
```
|
||||
# Ubuntu-14.04以前的版本:
|
||||
gec@ubuntu:~$ sudo /etc/init.d/networking force-reload
|
||||
gec@ubuntu:~$ sudo /etc/init.d/networking restart
|
||||
|
||||
# Ubuntu-14.04以后的版本:
|
||||
gec@ubuntu:~$ sudo service networking force-reload
|
||||
gec@ubuntu:~$ sudo service networking restart
|
||||
```
|
||||
|
||||
**注意**
|
||||
|
||||
在更新的Ubuntu-17.10以后的版本中,网络配置用的是 netplan 命令,而不是上述命令。
|
||||
|
||||
**测试网络**
|
||||
|
||||
有两个命令可以测试网络是否连通:
|
||||
|
||||
一、执行
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ ping www.qq.com
|
||||
```
|
||||
|
||||
ping外网测试网络是否通畅
|
||||
|
||||

|
||||
|
||||
二、执行
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ host www.qq.com
|
||||
www.qq.com is an alias for public-v6.sparta.mig.tencent-cloud.net.
|
||||
public-v6.sparta.mig.tencent-cloud.net has address 14.18.175.154
|
||||
public-v6.sparta.mig.tencent-cloud.net has address 113.96.232.215
|
||||
public-v6.sparta.mig.tencent-cloud.net has IPv6 address 240e:ff:f101:10::15f
|
||||
gec@ubuntu:~$
|
||||
|
||||
```
|
||||
|
||||
- 注意:
|
||||
|
||||
- host成功返回域名的IP地址,代表当前网络是通的。
|
||||
|
||||
- host成功返回域名的IP地址,代表当前系统的DNS解析是正常的。
|
||||
|
||||
**DNS配置**
|
||||
|
||||
DNS(Domain Name Server),即域名服务器,访问网址时必须通过 DNS 解析为对应的IP地址,才能正常访问网站。在以上测试命令中,如果ping成功,但host不成功,代表当前电脑的DNS配置有问题。
|
||||
|
||||
**配置 **
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ sudo cat /etc/resolv.conf
|
||||
# This file is managed by man:systemd-resolved(8). Do not edit.
|
||||
#
|
||||
# This is a dynamic resolv.conf file for connecting local clients to the
|
||||
# internal DNS stub resolver of systemd-resolved. This file lists all
|
||||
# configured search domains.
|
||||
#
|
||||
# Run "resolvectl status" to see details about the uplink DNS servers
|
||||
# currently in use.
|
||||
#
|
||||
# Third party programs must not access this file directly, but only through the
|
||||
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
|
||||
# replace this symlink by a static file or a different symlink.
|
||||
#
|
||||
# See man:systemd-resolved.service(8) for details about the supported modes of
|
||||
# operation for /etc/resolv.conf.
|
||||
|
||||
nameserver x.x.x.x
|
||||
options edns0 trust-ad
|
||||
gec@ubuntu:~$
|
||||
```
|
||||
|
||||
具体域名服务器的IP地址
|
||||
|
||||

|
||||
|
||||
宿主机windows的DNS配置
|
||||
|
||||
**重启 **
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ sudo service systemd-resolved restart
|
||||
```
|
||||
|
||||
**查看、启停网络接口**
|
||||
|
||||
使用命令
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ ifconfig
|
||||
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
|
||||
inet 192.168.1.103 netmask 255.255.255.0 broadcast 192.168.1.255
|
||||
inet6 fe80::20c:29ff:fe80:949c prefixlen 64 scopeid 0x20<link>
|
||||
ether 00:0c:29:80:94:9c txqueuelen 1000 (Ethernet)
|
||||
RX packets 2020 bytes 266623 (266.6 KB)
|
||||
RX errors 0 dropped 0 overruns 0 frame 0
|
||||
TX packets 8299 bytes 548748 (548.7 KB)
|
||||
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 1000 (Local Loopback)
|
||||
RX packets 37191 bytes 2722682 (2.7 MB)
|
||||
RX errors 0 dropped 0 overruns 0 frame 0
|
||||
TX packets 37191 bytes 2722682 (2.7 MB)
|
||||
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||
```
|
||||
|
||||
**注意**
|
||||
|
||||
- lo是Linux系统的本地回环设备,此设备需如上所示已启动方可连网。
|
||||
|
||||
- 若某些网卡由于某些原因未启动,则可使用如下命令来解决:
|
||||
|
||||
- 使用 ifconfig -a 来查看所有的网络接口
|
||||
|
||||
- 使用 ifconfig 网卡名称 up 来启动指定的网卡。
|
||||
|
||||
**启停指定的网络接口(网卡)**
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ sudo ifconfig ens33 up ==> 启用ens33
|
||||
gec@ubuntu:~$ sudo ifconfig ens33 down ==> 停用ens33
|
||||
```
|
||||
|
||||
在某些系统中,以上命令也可以用如下命令替代:
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ sudo ifup ens33 ==> 启用ens33
|
||||
gec@ubuntu:~$ sudo ifdown ens33 ==> 停用ens33
|
||||
```
|
||||
|
||||
**临时指定的网口IP地址**
|
||||
|
||||
与编辑
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ sudo ifconfig ens33 192.168.1.103
|
||||
```
|
||||
|
||||
**指定默认网关**
|
||||
|
||||
在某些情况下,Ubuntu无法正确找到网关,从而导致无法连网,可以手动配置网关地址:
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ sudo route add default gw 192.168.1.1
|
||||
```
|
||||
|
||||
网关地址根据各个局域网的配置不同而有所改变,不能照抄上述地址,网关地址一般而言就是当前局域网的路由器的地址,可以从宿主机windows的网络配置化中查看:
|
||||
|
||||

|
||||
|
||||
[](http://vm.yueqian.com.cn:8886/previewCourses.html?courseId=1331899420259463169&courseName=Linux%E5%9F%BA%E7%A1%80#0)宿主机windows的DNS配置[](http://vm.yueqian.com.cn:8886/previewCourses.html?courseId=1331899420259463169&courseName=Linux%E5%9F%BA%E7%A1%80#0)
|
||||
|
||||
## **2.2 Netplan网络配置**
|
||||
|
||||
在17.10之前的老版本Ubuntu中,配置网络可能有多种手段,可以直接使用systemd,或者配置
|
||||
|
||||
通过统一对
|
||||
|
||||
**自动获取IP**
|
||||
|
||||
以下的配置信息展现了如何使用
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ cat /etc/netplan/01-network-manager-all.yaml
|
||||
# Let NetworkManager manage all devices on this system
|
||||
network:
|
||||
version: 2
|
||||
renderer: NetworkManager
|
||||
ethernets:
|
||||
ens33:
|
||||
dhcp4: yes
|
||||
dhcp6: yes
|
||||
gec@ubuntu:~$
|
||||
gec@ubuntu:~$
|
||||
```
|
||||
|
||||
说明:
|
||||
|
||||
- renderer:指定yaml文件针对配置的网络工具
|
||||
|
||||
- ethernets:配置以太网
|
||||
|
||||
- ens33:以太网口名称
|
||||
|
||||
- dhcp4:自动获取IPv4的网络地址
|
||||
|
||||
- dhcp6:自动获取IPv6的网络地址
|
||||
|
||||
注意:
|
||||
|
||||
- 冒号后面必须有空格
|
||||
|
||||
- 缩进的空格数必须一致
|
||||
|
||||
**设定静态IP**
|
||||
|
||||
```
|
||||
gec@ubuntu:~$ cat /etc/netplan/01-network-manager-all.yaml
|
||||
# Let NetworkManager manage all devices on this system
|
||||
network:
|
||||
version: 2
|
||||
renderer: NetworkManager
|
||||
ethernets:
|
||||
ens33:
|
||||
dhcp4: no
|
||||
addresses: [192.168.100.100/24]
|
||||
gateway4: 192.168.100.1
|
||||
nameservers:
|
||||
addresses: [202.96.134.133, 8.8.8.8]
|
||||
gec@ubuntu:~$
|
||||
gec@ubuntu:~$
|
||||
```
|
||||
|
||||
说明:
|
||||
|
||||
- [192.168.100.100/24] 中的24代表设定的IP地址的前24比特位为子网掩码,也就是255.255.255.0
|
||||
|
||||
- nameservers 代表 DNS 服务器
|
||||
|
||||
**使能netplan**
|
||||
|
||||
配置完
|
||||
|
||||
```
|
||||
gec@ubuntu:$ sudo netplan apply
|
||||
```
|
||||
|
||||
# 3.ssh网络协议使用
|
||||
|
||||
SSH(Secure Shell,安全外壳协议)是一种用于加密远程登录和网络服务的网络协议,主要作用是在不安全的网络环境中,为计算机之间的通信提供安全的加密通道,避免数据传输被窃听或篡改。
|
||||
|
||||
## 在Ubuntu安装SSH服务
|
||||
|
||||
1. 远程登录Ubuntu系统终端
|
||||
|
||||
ssh协议进行登录:
|
||||
|
||||
(1) 配置Ubuntu支持ssh服务
|
||||
|
||||
① 检查系统是否启动ssh服务:ps -e | grep ssh
|
||||
|
||||

|
||||
|
||||
② 若系统没有启动SSH服务则需要安装ssh服务
|
||||
|
||||
sudo apt install ssh -y
|
||||
|
||||
(2) 查看自己Ubuntu的IP
|
||||
|
||||

|
||||
|
||||
(3) 打开一个终端
|
||||
|
||||
使用vscode中终端的ssh指令来登录Ubuntu
|
||||
|
||||
ssh [user@ip](http://user@ip)
|
||||
|
||||
user :用户名
|
||||
|
||||
ip:ubuntu的IP
|
||||
|
||||
输入指令:ssh 自己的用户名称@自己Ubuntu系统的IP地址
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
1. 文件传输
|
||||
|
||||
下载文件:
|
||||
|
||||
scp <用户名>@<ssh服务器地址>:<文件> <本地文件路径>
|
||||
|
||||
```shell
|
||||
scp root@192.168.13.233:text /opt #从远程主机192.168.13.233的root用户下载text文件到本地的/opt目录中
|
||||
```
|
||||
|
||||
文件夹:
|
||||
|
||||
scp -r <用户名>@<ssh服务器地址>:<文件> <本地文件路径>
|
||||
|
||||
```shell
|
||||
scp -r root@192.168.13.233:text /opt #从远程主机192.168.13.233的root用户下载text文件夹到本地的/opt目录中
|
||||
```
|
||||
|
||||
上传:
|
||||
|
||||
scp <本地文件名> <用户名>@<ssh服务器地址>:<上传保存路径>
|
||||
|
||||
```shell
|
||||
scp text root@192.168.13.233:/opt #将本地的text文件上传到远程主机192.168.13.233的root用户的/opt目录中
|
||||
```
|
||||
|
||||
例子:
|
||||
|
||||
scp ts root@192.168.13.233:/ //将本地文件ts传输到目标(192.168.13.233)的/ 目录下
|
||||
|
||||
# 4.图形化界面IP地址修改(推荐)
|
||||
|
||||
主机WindowsIP固定
|
||||
|
||||
打开控制面板-->网络和Internet-->网络共享中心
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
**注意:离开教室后使用有线网络是要将IPv4设置为自动获取方可上网**
|
||||
|
||||
UbuntuIP地址固定。虚拟机的网络设置要修改为桥接
|
||||
|
||||
设置-->网络-->设置-->IPv4-->手动
|
||||
|
||||

|
||||
|
||||
DNS推荐使用:192.168.2.1
|
||||
|
||||

|
||||
|
||||
重启网络链接不上外网则将DNS改为192.168.2.1
|
||||
Reference in New Issue
Block a user