Ubuntu22.04虛擬機配置及使用代理工具

特別注意:本教程基於VMware虛擬機,安裝Ubuntu22.04。其他類型虛擬機及Linux其他版本配置相似但有所不同。

1. 虛擬機配置

1.1 打開虛擬機設置。

1.2 選擇硬件選項卡-網絡適配器,在網絡連接處選擇“NAT模式”。

2. 主機設置

2.1 確認IP地址

開啓虛擬機系統後,查看主機分配的虛擬機IP地址。此時可以看到虛擬機被分配了兩個網絡,其中VMnet1爲1.2中網絡連接選擇“僅主機模式”時使用的網絡,VMnet8爲網絡連接選擇“NAT模式”時使用的網絡。

由於我們選擇的是“NAT模式”,因此需要記下VMnet8中IPv4地址,即192.168.81.1,子網掩碼爲255.255.255.0。.

2.2 代理設置

代理工具內查看監聽端口號,一般爲10809或7890,其他工具請自行查找。同時須開啓代理工具內“允許來自局域網的連接”。

3. Ubuntu虛擬機設置

3.1 配置網絡代理

打開Ubuntu設置,選擇網絡-網絡代理,點擊右側按鈕進入設置界面。

選擇“手動”,填寫下方相關代理IP和監聽端口,IP地址爲2.1中VMnet8下IPv4地址,端口爲代理工具使用端口。

 

3.2 測試網絡代理

重啓虛擬機網絡後,開啓主機代理,打開虛擬機終端運行:

curl cip.cc

 如代理成功,會顯示代理方IP地址及相關數據,如未成功則顯示本地所在網絡信息。

3.2 關閉網絡代理

 如無需代理時,可選擇將虛擬機網絡代理設置爲“關閉”,或在主機內斷開代理工具代理即可。

4. 代理開關快捷方式

4.1 編寫.sh文件

新建.sh文件。

開啓代理版

 
  1. #!/bin/bash
  2.  
  3. #設置代理爲手動
  4. gsettings set org.gnome.system.proxy mode 'manual'
  5.  
  6. #終端反饋
  7. echo "已開啓"
  8. echo 按任意鍵繼續
  9.  
  10. #確保反饋後未按鍵時不會退出終端
  11. read -n 1
 

關閉代理版

 
  1. #!/bin/bash
  2.  
  3. #設置代理爲無
  4. gsettings set org.gnome.system.proxy mode 'none'
  5.  
  6. #終端反饋
  7. echo "已關閉"
  8. echo 按任意鍵繼續
  9.  
  10. #確保反饋後未按鍵時不會退出終端
  11. read -n 1
 

4.2 編寫.desktop文件

新建.desktop文件,輸入以下內容,並移動至桌面(注意文件不可刪除.desktop後綴,否則系統會默認爲文本文件,下方內容中Name字段會修正圖標顯示文字)。

開啓代理版

 
  1. [Desktop Entry]
  2.  
  3. #快捷方式顯示名稱
  4. Name = ON
  5.  
  6. #待執行.sh文件路徑
  7. Exec = /home/fujd/Desktop/ProxyControl/proxyManual.sh
  8.  
  9. #待執行.sh文件所在目錄
  10. Path = /home/fujd/Desktop/ProxyControl
  11.  
  12. #快捷方式顯示圖標路徑
  13. Icon = /home/fujd/Desktop/ProxyControl/ON.png
  14.  
  15. #是否顯示終端,爲確保.sh文件的echo能夠被看到,此處要選擇true
  16. Terminal = true
  17.  
  18. #快捷方式類型
  19. Type = Application
 

關閉代理版

 
  1. [Desktop Entry]
  2.  
  3. #快捷方式顯示名稱
  4. Name = OFF
  5.  
  6. #待執行.sh文件路徑
  7. Exec = /home/fujd/Desktop/ProxyControl/proxyDisabled.sh
  8.  
  9. #待執行.sh文件所在目錄
  10. Path = /home/fujd/Desktop/ProxyControl
  11.  
  12. #快捷方式顯示圖標路徑
  13. Icon = /home/fujd/Desktop/ProxyControl/OFF.png
  14.  
  15. #是否顯示終端,爲確保.sh文件的echo能夠被看到,此處要選擇true
  16. Terminal = true
  17.  
  18. #快捷方式類型
  19. Type = Application
 

4.3 實現快捷方式功能

現階段此.desktop文件不可用,需要右鍵點擊文件,選擇“允許啓動”,此時圖標轉換,可以雙擊運行,實現代理開啓或關閉。

5. One More Thing...

通過以上設置,我們已經可以通過全局代理方式在虛擬機上訪問之前無法訪問的網站和應用服務了,但某種情況下終端可能會無法被代理,或只是終端需要代理,不想如此麻煩進行設置。如果只需要在終端上實現代理,可以使用“proxychains”工具,具體方法如下:

5.1 安裝proxychains

可以選擇apt直接安裝,兩種版本均可,但要注意,如果安裝proxychains4,後續所有配置都需要將proxychains替換爲proxychains4。

 
  1. #4以下版本
  2. apt-get install proxychains
  3.  
  4. #4及以上版本
  5. apt-get install proxychains4
 

或通過make編譯,編譯配置不在此介紹。

 
  1. git clone https://github.com/rofl0r/proxychains-ng
  2. cd proxychains-ng
  3. ./configure
  4. sudo make && make install
 

5.2 配置proxychains

proxychains 的配置文件位於 /etc/proxychains.conf ,打開後在末尾添加根據需要填寫使用的代理,前綴、IP地址和端口號與3.1內配置一致。不瞭解如何填寫可以看.conf文件內的示例。

5.3 使用proxychains

使用方法非常簡單,在需要執行的命令前輸入proxychains+空格即可,例如:

proxychains git clone https://github.com/xxxx/xxxx.git

但注意,使用ping命令時候添加proxychains無效,因爲 proxychains 只會代理 TCP 連接,而 ping 使用的是 ICMP。

在使用proxychains進行命令行代理時,要保持主機代理工具處於代理狀態,虛擬機無需設置或打開代理。

驗證是否成功的方法與3.2一致。

ubuntu apt install 工具添加網絡代理_apt install proxy-CSDN博客

  1. Create a new configuration file named proxy.conf.
    sudo touch /etc/apt/apt.conf.d/proxy.conf
  2. Open the proxy.conf file in a text editor.
    sudo vi /etc/apt/apt.conf.d/proxy.conf
  3. Just like in the first example, create a new file under the /etc/apt/apt.conf.d directory, and then add the following lines.

    Acquire {
      HTTP::proxy "http://127.0.0.1:8080";
      HTTPS::proxy "http://127.0.0.1:8080";
    }

ubuntu 22.04版本修改服務器名、ip,dns信息的操作方法_ubuntu 22.04 修改ip-CSDN博客

總結
1、ubuntu修改服務器名重啓後生效的方法是直接修改/etc/hostname文件

2、ubuntu 22.04操作系統配置ip和dns信息,一般只需要使用netplan命令行工具來配置就行,在/etc/netplan/在目錄下創建一個yaml文件就可以實現ip和dns的配置,當然如果/etc/netplan下有多個yaml文件,則所有/etc/netplan/*.yaml文件都將被netplan命令行使用,參見官方文檔https://ubuntu.com/server/docs/network-configuration和https://manpages.ubuntu.com/manpages/jammy/man5/netplan.5.html

3、個人不建議使用/etc/resolve.conf來配置ubuntu 22.04操作系統的dns信息,因爲太複雜了,這個文件是被systemd-resolved服務託管。ubuntu操作系統/etc/resolve.conf中默認使用的是 nameserver 127.0.0.53迴環地址,/etc/resolve.conf文中有這麼一句話This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8). Do not edit。說明這個文件是被systemd-resolved這個服務託管的。通過 netstat -tnpl| grep systemd-resolved 查看到這個服務是監聽在 53 號端口上。爲啥要用127.0.0.53作爲迴環地址而不是127.0.0.1,因爲127網段都是迴環地址(127.0.0.1 - 127.255.255.254),不過通常用大家只喜歡用127.0.0.1來表示而已,53表示dns端口
3.1、新安裝的ubuntu 22.04環境不做其他改動的情況下,/etc/resolv.conf是/run/systemd/resolve/stub-resolv.conf的軟鏈接,如果只是手工更改/etc/resolv.conf中的內容,重啓會後發現/etc/resolv.conf中的內容又恢復成原樣了,比如註釋/etc/resolv.conf文件中的nameserver 127.0.0.53這一行,重啓系統後被註釋掉的nameserver 127.0.0.53這一行又回來了,如果不想重啓後/etc/resolv.conf中的內容恢復成原樣,則執行systemctl stop systemd-resolved和systemctl disable systemd-resolved,或把/etc/resolv.conf對應的軟鏈接刪除,再手工建立一個/etc/resolv.conf
3.2、使用/etc/netplan/00-installer-config.yaml配置DNS 172.22.10.66並執行netplan apply使之生效,再修改/etc/systemd/resolved.conf內容裏的DNS爲172.22.136.2,然後執行systemctl restart systemd-resolved,發現/etc/resolv.conf中的內容沒變還是127.0.0.53,再執行resolvectl status可以看到/etc/systemd/resolved.conf中的DNS 172.22.136.2存在,/etc/netplan/00-installer-config.yaml中的DNS 172.22.10.66也在,就是沒有/etc/resolv.conf中的127.0.0.53。重啓操作系統後/etc/resolv.conf中的內容沒變還是127.0.0.53,重啓操作系統後執行resolvectl status還是隻有/etc/systemd/resolved.conf中的DNS 172.22.136.2和/etc/netplan/00-installer-config.yaml中的DNS 172.22.10.66,沒有/etc/resolv.conf中的127.0.0.53

4、ubuntu官方不建議使用/etc/resolve.conf來配置ubuntu 的dns信息,ubuntu對於/etc/resolv.conf的官方描述:If you require DNS for your temporary network configuration, you can add DNS server IP addresses in the file /etc/resolv.conf. In general, editing /etc/resolv.conf directly is not recommended, but this is a temporary and non-persistent configuration.

5、ubuntu 22.04查看dns信息的命令是resolvectl status,該命令可以看到全局dns信息和某個網卡的dns信息,全局DNS服務器信息一般來自配置文件/etc/systemd/resolved.conf,僅在/etc/resolv.conf不是一個指向/run/systemd/resolve/stub-resolv.conf, /usr/lib/systemd/resolv.conf, /run/systemd/resolve/resolv.conf 之一的軟連接的情況下,且/etc/systemd/resolved.conf中dns被註釋掉的情況下,全局DNS服務器纔會讀取自/etc/resolv.conf,所以這就是不建議大家使用/etc/resolve.conf來配置ubuntu 的dns信息的原因。

修改服務器名
修改服務器名爲FRSBachDEV3,重啓後也生效的方法
vi /etc/hostname
FRSBachDEV3
備註:如果/etc/hostname文件中的內容寫成hostname FRSBachDEV3,那麼重啓後服務器名就變成了hostnameFRSBachDEV3

修改IP
https://ubuntu.com/server/docs/network-configuration
https://manpages.ubuntu.com/manpages/jammy/man5/netplan.5.html
netplan是一個命令行工具,用於ubuntu上配置網絡,所有/etc/netplan/*.yaml文件都將被使用

root@FRSBachDEV3:~# cat /etc/netplan/00-installer-config.yaml
network:
  ethernets:
    ens160:
      dhcp4: true
  version: 2
root@FRSBachDEV3:~# vim /etc/netplan/00-installer-config.yaml
network:
  ethernets:
    ens160:
      dhcp4: false
      addresses: [172.22.136.147/22]
      routes:
        - to: default
          via: 172.22.136.1
      nameservers:
        search: [dai.netdai.com,netdai.com]
        addresses: [172.22.10.66,172.22.10.67]
  version: 2

備註:
dhcp4中的4表示ipv4
gateway4中的4表示ipv4,不過gateway4已經被廢棄,配置了gateway4再執行netplan apply的話會報錯** (generate:1426): WARNING **: 09:54:13.918: gateway4 has been deprecated, use default routes instead.See the ‘Default routes’ section of the documentation for more details.
routes項填寫網關地址
addresses項填寫ip地址
nameservers項填寫dns地址或搜索域,其中addresses子項是dns地址列表,search子項是搜索域名列表
version: 2這一項表示YAML版本是2,curtin,MaaS等目前使用的YAML的版本是1

root@FRSBachDEV3:~# netplan apply

root@FRSBachDEV3:~# cat /etc/resolv.conf
nameserver 127.0.0.53
options edns0 trust-ad
search dai.netdai.com netdai.com

root@FRSBachDEV3:~# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.22.136.147  netmask 255.255.252.0  broadcast 172.22.139.255
        inet6 fe80::250:56ff:fe94:522d  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:94:52:2d  txqueuelen 1000  (Ethernet)
        RX packets 45071  bytes 3394009 (3.3 MB)
        RX errors 0  dropped 40  overruns 0  frame 0
        TX packets 765  bytes 78732 (78.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 2331  bytes 168025 (168.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2331  bytes 168025 (168.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

查看網關

root@FRSBachDEV3:~# ip route | grep default
default via 172.22.136.1 dev ens160 proto static

root@FRSBachDEV3:~# nmcli dev show|grep GATEWAY
IP4.GATEWAY:                            172.22.136.1
IP6.GATEWAY:                            --
IP4.GATEWAY:                            --
IP6.GATEWAY:                            --

查看dns

root@FRSBachDEV3:~# resolvectl status
Global
       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub

Link 2 (ens160)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 172.22.10.66
       DNS Servers: 172.22.10.66 172.22.10.67
        DNS Domain: dai.netdai.com netdai.com

實驗:如何纔會使用到/etc/resolve.conf中的dns信息

/etc/systemd/resolved.conf中dns爲172.22.136.2
root@FRSBachDEV3:~# cat /etc/systemd/resolved.conf |grep DNS=
DNS=172.22.136.2

/etc/resolv.conf來自軟鏈接/run/systemd/resolve/stub-resolv.conf
root@FRSBachDEV3:~# ll /etc/resolv.conf
lrwxrwxrwx 1 root root 39 Aug  9  2022 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

root@FRSBachDEV3:~# cat /run/systemd/resolve/stub-resolv.conf
nameserver 127.0.0.53
options edns0 trust-ad
search dai.netdai.com netdai.com

/run/systemd/resolve/resolv.conf有來自/etc/systemd/resolved.conf中dns 172.22.136.2,也有來自/etc/netplan/00-installer-config.yaml中dns 172.22.10.66 172.22.10.67,就是沒有來自/etc/resolv.conf的127.0.0.53
root@FRSBachDEV3:~# cat /run/systemd/resolve/resolv.conf
nameserver 172.22.136.2
nameserver 172.22.10.66
nameserver 172.22.10.67
search dai.netdai.com netdai.com

/etc/systemd/resolved.conf中dns 172.22.136.2和/etc/netplan/00-installer-config.yaml中dns 172.22.10.66 172.22.10.67,就是沒有來自/etc/resolv.conf的127.0.0.53
root@FRSBachDEV3:~# resolvectl status
Global
         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  resolv.conf mode: stub
Current DNS Server: 172.22.136.2
       DNS Servers: 172.22.136.2

Link 2 (ens160)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 172.22.10.66
       DNS Servers: 172.22.10.66 172.22.10.67
        DNS Domain: dai.netdai.com netdai.com

刪除/etc/resolv.conf來自軟鏈接,並手工建立/etc/resolv.conf文件,其中dns爲172.22.136.3
root@FRSBachDEV3:~# ll /etc/resolv.conf
-rw-r--r-- 1 root root 946 Oct 12 10:49 /etc/resolv.conf

root@FRSBachDEV3:~# cat /etc/resolv.conf
nameserver 172.22.136.3
options edns0 trust-ad
search dai.netdai.com netdai.com

保留/etc/systemd/resolved.conf中dns 172.22.136.2的情況下,重啓systemd-resolved,發現還是用的/etc/systemd/resolved.conf中dns 172.22.136.2而沒有使用手工建立/etc/resolv.conf文件的dns 172.22.136.3
root@FRSBachDEV3:~# systemctl restart systemd-resolved
root@FRSBachDEV3:~# resolvectl status
Global
       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: foreign
     DNS Servers: 172.22.136.2

Link 2 (ens160)
Current Scopes: DNS
     Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
   DNS Servers: 172.22.10.66 172.22.10.67
    DNS Domain: dai.netdai.com netdai.com

註釋掉/etc/systemd/resolved.conf中dns再重啓systemd-resolved,發現這個時候才真正用上了手工建立/etc/resolv.conf文件的dns 172.22.136.3
root@FRSBachDEV3:~# vim /etc/systemd/resolved.conf
root@FRSBachDEV3:~# cat /etc/systemd/resolved.conf |grep DNS
#DNS

root@FRSBachDEV3:~# systemctl restart systemd-resolved
root@FRSBachDEV3:~# resolvectl status
Global
         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  resolv.conf mode: foreign
Current DNS Server: 172.22.136.3
       DNS Servers: 172.22.136.3
        DNS Domain: dai.netdai.com netdai.com

Link 2 (ens160)
Current Scopes: DNS
     Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
   DNS Servers: 172.22.10.66 172.22.10.67
    DNS Domain: dai.netdai.com netdai.com

root@FRSBachDEV3:~# cat /run/systemd/resolve/resolv.conf
nameserver 172.22.136.3
nameserver 172.22.10.66
nameserver 172.22.10.67
search dai.netdai.com netdai.com

Ubuntu18.04 使用netplan配置局域網DHCP主從機_netplan 配置dhcp-CSDN博客

安裝 sudo apt install isc-dhcp-server

修改/etc/dhcp/dhcpd.conf配置文件,設置本DHCP的配置

default-lease-time 180;
max-lease-time 600;
​
authoritative;
​
subnet 192.168.90.0 netmask 255.255.255.0 {
    range 192.168.90.150 192.168.90.200; #隨機分配IPv4地址池
    option routers 192.168.90.254;#網關地址(網關的IP地址就是具有路由功能的設備的IP地址,比如路由器或者啓用了路由協議的服務器)
    option domain-name-servers 192.168.90.1,192.168.90.2;#
    option domain-name "rsync DHCP network";
}

修改/etc/default/isc-dhcp-server,確定DHCP的監聽端口

INTERFACESv4="eno1"

更新配置 sudo systemctl restart isc-dhcp-server.service

修改/etc/netplan/01-network-manager-all.yaml,給有線網口eno1配置靜態IP地址

network:
  version: 2
  renderer: NetworkManager
  ethernets:
      eno1:
          addresses: [192.168.90.1/24]
          gateway4: 192.168.90.254
          dhcp4: no #意思是在識別eno1的IP地址時不需要使用DHCP動態分配,因此在從機端設爲yes/true
          nameservers:
              addresses:  [8.8.8.8]

保存配置 sudo netplan apply

啓動DHCP服務 sudo service isc-dhcp-server start (start/restart/stop)

  • 配置DHCP從機

修改etc/netplan/netplan.yaml

network:
  ethernets:
      eth0:
          dhcp4: yes
  version:2

使用ifconfig,發現已配置192.168.90.150,配置成功

  • 輔助命令

nmcli dev show 查看每個端口的網絡配置信息

nmcli connection show 查看每個端口的聯網情況

service isc-dhcp-server status 查看狀態

  • 參考文檔

《ubuntu server guide》

ubuntu配置IP地址,網關,DNS和路由_ubuntu ip route add_QJings的博客-CSDN博客

自建DHCP服務之isc-dhcp-server_王三三的博客-CSDN博客

A Step-by-Step Guide to Set up a DHCP Server on Ubuntu - LinuxForDevices

1. User and password — Firefly Wiki (t-firefly.com)

Ubuntu 22.04下配置DHCP_ubuntu22.04 dhcp 服務器配置-CSDN博客

3.登錄Ubuntu 22.04系統,運行apt命令更新包

sudo apt update
在這裏插入圖片描述

4.安裝DHCP

sudo apt install isc-dhcp-server -y
在這裏插入圖片描述

5.配置DHCP服務器,修改DHCP運行的網絡接口

a. sudo vi /etc/default/isc-dhcp-server
在這裏插入圖片描述

b. 修改DHCP網絡接口爲上文所提到的enp5s0(這邊根據自己的配置來)

在這裏插入圖片描述

6.修改DHCP配置文件

a. sudo vi /etc/dhcp/dhcpd.conf
在這裏插入圖片描述

b. 註釋掉域名服務器參數
#option domain-name “example.org”;
#option domain-name-servers ns1.example.org, ns2.example.org
在這裏插入圖片描述

c. 取消authoritative參數註釋
在這裏插入圖片描述

d. 添加DHCP服務器所使用的子網和IP地址範圍。同時指定enp5s0接口的ip地址爲路由器

subnet 192.168.100.0 netmask 255.255.255.0{
range 192.168.100.10 192.168.100.20;
option routers 192.168.100.8;
}
在這裏插入圖片描述

7.運行並啓動DHCP Server服務

a. sudo systemctl start isc-dhcp-server
b. sudo systemctl enable isc-dhcp-server
在這裏插入圖片描述

8.檢查DHCP Server服務狀態,無報錯即成功

sudo systemctl status isc-dhcp-server
在這裏插入圖片描述

9.將Win10客戶端通過網線連接服務器端(也就是我們配置好Ubuntu 22.04 DHCP的這臺),並設置自動獲取IP,可以看到客戶端自動獲取了IP地址

在這裏插入圖片描述
在這裏插入圖片描述

apt-get install 使用代理

一. 環境變量方法
設置環境變量,下面是臨時設置

export http_proxy=http://127.0.0.1:8000
sudo apt-get update

二.設置apt-get的配置
修改/etc/apt/apt.conf(或者/etc/envrionment),增加
Acquire::http::proxy "http://127.0.0.1:8000/";
Acquire::ftp::proxy "ftp://127.0.0.1:8000/";
Acquire::https::proxy "https://127.0.0.1:8000/";

三.在命令行臨時帶入
這是我最喜歡的方法,畢竟apt不是時時刻刻都用的
在命令行後面增加-o選項
sudo apt-get -o Acquire::http::proxy="http://127.0.0.1:8000/" update

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章