alpinelinux的网络配置
在 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