在 Alpine Linux 中配置網絡,您可以根據以下步驟進行:
配置本機 hostname:
本機hostname保存在/etc/hostname
文件中。
echo 'alpine-web' > /etc/hostname
hostname -F /etc/hostname # 立即生效
運行結果:
localhost:~# echo "alpine-test" > /etc/hostname
localhost:~# hostname -F /etc/hostname
alpine-test:~#
配置 hosts:
修改 /etc/hosts
文件,如下面是將harbor的域名添加映射:
192.168.10.69 herbor.three-corner.xyz
配置 Loopback:
在 /etc/network/interfaces
文件中,默認有以下的內容,如果沒有的話,請自行添加:
auto lo
iface lo inet loopback
配置 Ethernet:
以 eth0
為例:在/etc/network/interfaces
文件中添加:
auto eth0
IPv4 動態DHCP獲取地址配置:
iface eth0 inet dhcp
IPv4 靜態地址配置:
iface eth0 inet static
address 192.168.1.150
netmask 255.255.255.0
gateway 192.168.1.1
IPv6的配置同理,將inet
改為inet6
,地址也是由IPv4的地址改為IPv6的地址:
iface eth0 inet6 dhcp
# 或者
iface eth0 inet static
address fe80::20c:29ff:fe69:3471/64
gateway 2409:8a56:4e2f:50f0:4a81:d4ff:fea2:5c06/64
配置 DNS:
建議在配置了靜態地址之后,再配置一下DNS。修改 /etc/resolv.conf
文件:
nameserver 114.114.114.114
nameserver 114.114.115.115
臨時配置 IP 和網關:
ifconfig eth0 192.168.1.150 netmask 255.255.255.0 up
route add default gw 192.168.1.1
重啟網絡服務:
rc-service networking restart
# 或者
/etc/init.d/networking restart
檢查網絡連接:
ping命令默認沒有指定次數是不斷的ping下去,下面的例子是ping百度8次就退出。
alpine-test:~# ping -c 8 baidu.com
PING baidu.com (110.242.68.66): 56 data bytes
64 bytes from 110.242.68.66: seq=0 ttl=49 time=64.199 ms
64 bytes from 110.242.68.66: seq=1 ttl=49 time=64.059 ms
64 bytes from 110.242.68.66: seq=2 ttl=49 time=64.386 ms
64 bytes from 110.242.68.66: seq=3 ttl=49 time=63.930 ms
64 bytes from 110.242.68.66: seq=4 ttl=49 time=63.982 ms
64 bytes from 110.242.68.66: seq=5 ttl=49 time=64.181 ms
64 bytes from 110.242.68.66: seq=6 ttl=49 time=64.064 ms
64 bytes from 110.242.68.66: seq=7 ttl=49 time=63.915 ms--- baidu.com ping statistics ---
8 packets transmitted, 8 packets received, 0% packet loss
round-trip min/avg/max = 63.915/64.089/64.386 ms
安裝和配置無線網絡:
安裝wpa_supplicant
:
apk add wpa_supplicant
配置無線網絡:
wpa_passphrase 'YourSSID' 'YourPassword' > /etc/wpa_supplicant/wpa_supplicant.conf
啟動無線網絡:
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf