2019年12月31日星期二

Ubuntu18.04 及更高版本Ubuntu 配置Ip地址、网关、DNS

一、查看各项信息,IP、网关、DNS

1. 查看IP:

ifconfig

2. 查看网关

netstat -rnroute -n

3. 查看DNS

cat /etc/resolv.conf

3. 查看DNS解析 ,nslookup  [host]

示例:
nslookup www.baidu.com

结果如下:
root@localhost:~# nslookup www.baidu.com
Server:         192.168.0.1
Address:        192.168.0.1#53

二、配置IP、网关、DNS

Ubuntu 20.04

首先修改 /etc/systemd/resolved.conf 文件,在其中添加dns信息,例如:
DNS=8.8.8.8 114.114.114.114

然后退出保存。
然后以root身份在ubuntu终端中依次执行如下命令:
重启systemd-resolved服务
systemctl restart systemd-resolved

设置开机启动
systemctl enable systemd-resolved
 
重命名原有配置文件
mv /etc/resolv.conf  /etc/resolv.conf.bak

创建配置文件的软连接
ln -s /run/systemd/resolve/resolv.conf /etc/

再查看/etc/resolv.conf文件就可以看到新的dns信息已经写入其中了。
cat /etc/resolv.conf

Ubuntu18.04

1. 使用ifupdown配置网络

1)如果要使用之前的方式配置网络,需要重新安装ifupdown:

sudo apt install ifupdown

2)修改配置文件/etc/network/interfaces:

配置文件修改如下:

iface ens160 inet static
address 210.72.92.25
gateway 210.72.92.254
netmask 255.255.255.0
dns-nameservers 8.8.8.8

3)重启网络服务使配置生效

sudo services network restrart

4)修改DNS

打开/etc/resolv.conf
sudo vim /etc/resolv.conf

重启服务生效:
sudo /etc/init.d/resolvconf restart #使DNS生效

2. 使用netplan配置网络(推荐)

1) 配置IP及网关

Ubuntu 18.04使用netplan配置网络,其配置文件是yaml格式的。安装好Ubuntu 18.04之后,在/etc/netplan/目录下默认的配置文件名是50-cloud-init.yaml,我们通过VIM修改它:
sudo vim /etc/netplan/50-cloud-init.yaml
配置文件修改如下:
参考配置:配置eth0为外网网卡,eth1为内网网卡
# /etc/netplan/50-cloud-init.yaml

network:
renderer: networkd
ethernets:
eth0:
addresses: [192.168.100.211/23, 'fe80:0:0:0:0:0:c0a8:64d3']
gateway4: 192.168.100.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
search: []
optional: true
eth1:
dhcp4: false
addresses: [192.168.100.10/24]
routes:
- to: 0.0.0.0/0
via: 192.168.100.1
metric: 50
optional: true
eno4:
dhcp4: true
optional: true
version: 2

典型配置2,双网卡自动获取IP
# /etc/netplan/50-cloud-init.yaml

network:
renderer: networkd
ethernets:
eth1:
dhcp4: true
dhcp6: true
optional: true
eth0:
dhcp4: true
dhcp6: true
optional: true
version: 2


这里把DNS和ipv4地址配置在一个文件里了,不用再修改/etc/resolv.conf 文件。
重启网络服务使配置生效
sudo netplan apply

查看网络信息
ip addr list

推荐 :

  • Debian/Ubuntu 系统初始化设置Debian/Ubuntu  系统初始化设置快速初始化基本示例:复制粘贴回车执行:1-替换镜像源: sed -i "s@http://ports.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list2-复制以下连续执… 继续阅读
  • Ubuntu升级或安装Nginx最新稳定版(官方源安装)卸载旧版本:#卸载软件sudo apt purge nginx   #自动清理一些程序   sudo apt autoremove nginx       #删除不需要的配置文件sudo apt autocle… 继续阅读
  • 使用DDNS-GO配置DDNSddns-go自动获得你的公网 IPv4 或 IPv6 地址,并解析到对应的域名服务。特性支持Mac、Windows、Linux系统,支持ARM、x86架构支持的域名服务商 阿里云 腾讯云 Dnspod Cloudflare 华为云 Cal… 继续阅读
  • systemd 服务方式运行 mihomo-linux-arm64 实例为用户帐户运行 mihomo-linux-arm64 系统实例重新加载 systemd 模块systemctl daemon-reload 启动 clash 服务systemctl start clash.service设置开机自启在开机时启用一个服务: sy… 继续阅读
  • 使用 Docker 部署 Memos 的综合指南在继续安装 Memos 之前,请确保满足以下先决条件:安装了 Docker 的服务器:Memos 被设计为使用 Docker 进行自托管。Docker 运行要使用 设置备忘录,请执行以下命令以启动备忘录:docker rundocker run -d \ --init \ … 继续阅读