使用 cloudflared 和 systemd 在 Linux 上设置 DNS over HTTPS (DoH)

Linux 上如何使用 systemd 设置 DNS over HTTPS (DoH) ?通过利用 systemd,我们可以确保在启动时自动启动提供 DoH 服务的 cloudflared

第一步:安装 cloudflared

  1. 首先,下载 cloudflared
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64

Copy

  1. 使二进制文件可执行并移至合适的目录:
chmod +x cloudflared-linux-amd64
sudo mv cloudflared-linux-amd64 /usr/local/bin/cloudflared

Copy

第二步:配置 cloudflared

  1. 创建 cloudflared 的配置目录:
sudo mkdir /etc/cloudflared

Copy

  1. 创建配置文件:
sudo nano /etc/cloudflared/config.yml

Copy

  1. 填入以下内容:
proxy-dns: true
proxy-dns-upstream:
  - https://1.1.1.1/dns-query
  - https://1.0.0.1/dns-query

Copy

保存并退出。

第三步:将 cloudflared 设置为 systemd 服务

  1. 创建 systemd 服务文件:
sudo nano /etc/systemd/system/cloudflared.service

Copy

  1. 填入以下内容:
[Unit]
Description=cloudflared DNS over HTTPS 代理
After=syslog.target network-online.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/cloudflared proxy-dns
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target

Copy

保存并退出。

  1. 启用并开始服务:
sudo systemctl enable cloudflared
sudo systemctl start cloudflared

Copy

第四步:更新系统 DNS 设置

  1. 编辑 resolv.conf
sudo nano /etc/resolv.conf

Copy

  1. 修改或添加:
nameserver 127.0.0.1

Copy

这将使用 cloudflared 代理作为系统的 DNS 服务器。

第五步:验证 DoH 配置

检查 DNS 请求是否通过 HTTPS:

dig @127.0.0.1 example.com

Copy

注意:使用 NetworkManager 或其他网络工具的系统可能会覆盖 /etc/resolv.conf。确保配置这些工具以防止此类更改,或使 resolv.conf 不可变。

我们希望此指南帮助您在 Linux 上使用 cloudflared 和 systemd 设置了 DNS over HTTPS。如果遇到任何问题,请查阅相关文档或寻求技术支持。

备份现有apt源配置

mv /etc/apt/sources.list /etc/apt/sources.list.old

Debian 11

全部复制后,直接粘贴并按下回车执行

官方源

cat > /etc/apt/sources.list << EOF
deb http://deb.debian.org/debian/ bullseye main contrib non-free
deb-src http://deb.debian.org/debian/ bullseye main contrib non-free
deb http://deb.debian.org/debian/ bullseye-updates main contrib non-free
deb-src http://deb.debian.org/debian/ bullseye-updates main contrib non-free
deb http://deb.debian.org/debian/ bullseye-backports main contrib non-free
deb-src http://deb.debian.org/debian/ bullseye-backports main contrib non-free
deb http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
deb-src http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
EOF

或者去掉源码包,提高apt update的速度

cat << EOF > /etc/apt/sources.list
deb http://deb.debian.org/debian/ bullseye main contrib non-free
deb http://deb.debian.org/debian/ bullseye-updates main contrib non-free
deb http://deb.debian.org/debian/ bullseye-backports main contrib non-free
deb http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
EOF

阿里云镜像站

cat << EOF > /etc/apt/sources.list
deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb http://mirrors.aliyun.com/debian-security/ bullseye-security main
deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main
deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
EOF

腾讯云镜像站

cat << EOF > /etc/apt/sources.list
deb http://mirrors.tencent.com/debian/ bullseye main non-free contrib
deb-src http://mirrors.tencent.com/debian/ bullseye main non-free contrib
deb http://mirrors.tencent.com/debian-security/ bullseye-security main
deb-src http://mirrors.tencent.com/debian-security/ bullseye-security main
deb https://mirrors.tencent.com/debian/ bullseye-updates main non-free contrib
deb-src http://mirrors.tencent.com/debian/ bullseye-updates main non-free contrib
deb http://mirrors.tencent.com/debian/ bullseye-backports main non-free contrib
deb-src http://mirrors.tencent.com/debian/ bullseye-backports main non-free contrib
EOF

网易镜像站

cat << EOF > /etc/apt/sources.list
deb http://mirrors.163.com/debian/ bullseye main non-free contrib
deb-src http://mirrors.163.com/debian/ bullseye main non-free contrib
deb http://mirrors.163.com/debian-security/ bullseye-security main
deb-src http://mirrors.163.com/debian-security/ bullseye-security main
deb http://mirrors.163.com/debian/ bullseye-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ bullseye-updates main non-free contrib
deb http://mirrors.163.com/debian/ bullseye-backports main non-free contrib
deb-src http://mirrors.163.com/debian/ bullseye-backports main non-free contrib
EOF

清华大学镜像站

cat << EOF > /etc/apt/sources.list
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
EOF

Debian 12

全部复制后,直接粘贴并按下回车执行

官方源

cat > /etc/apt/sources.list << EOF
deb http://deb.debian.org/debian/ bookworm main contrib non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm main contrib non-free-firmware
deb http://deb.debian.org/debian/ bookworm-updates main contrib non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm-updates main contrib non-free-firmware
deb http://deb.debian.org/debian/ bookworm-backports main contrib non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm-backports main contrib non-free-firmware
deb http://deb.debian.org/debian-security/ bookworm-security main contrib non-free-firmware
deb-src http://deb.debian.org/debian-security/ bookworm-security main contrib non-free-firmware
EOF

或者去掉源码包,提高apt update的速度

cat << EOF > /etc/apt/sources.list
deb http://deb.debian.org/debian/ bookworm main contrib non-free-firmware
deb http://deb.debian.org/debian/ bookworm-updates main contrib non-free-firmware
deb http://deb.debian.org/debian/ bookworm-backports main contrib non-free-firmware
deb http://deb.debian.org/debian-security/ bookworm-security main contrib non-free-firmware
EOF

阿里云镜像站

cat << EOF > /etc/apt/sources.list
deb http://mirrors.aliyun.com/debian/ bookworm main non-free-firmware contrib
deb-src http://mirrors.aliyun.com/debian/ bookworm main non-free-firmware contrib
deb http://mirrors.aliyun.com/debian-security/ bookworm-security main
deb-src http://mirrors.aliyun.com/debian-security/ bookworm-security main
deb http://mirrors.aliyun.com/debian/ bookworm-updates main non-free-firmware contrib
deb-src http://mirrors.aliyun.com/debian/ bookworm-updates main non-free-firmware contrib
deb http://mirrors.aliyun.com/debian/ bookworm-backports main non-free-firmware contrib
deb-src http://mirrors.aliyun.com/debian/ bookworm-backports main non-free-firmware contrib
EOF

腾讯云镜像站

cat << EOF > /etc/apt/sources.list
deb http://mirrors.tencent.com/debian/ bookworm main non-free-firmware contrib
deb-src http://mirrors.tencent.com/debian/ bookworm main non-free-firmware contrib
deb http://mirrors.tencent.com/debian-security/ bookworm-security main
deb-src http://mirrors.tencent.com/debian-security/ bookworm-security main
deb https://mirrors.tencent.com/debian/ bookworm-updates main non-free-firmware contrib
deb-src http://mirrors.tencent.com/debian/ bookworm-updates main non-free-firmware contrib
deb http://mirrors.tencent.com/debian/ bookworm-backports main non-free-firmware contrib
deb-src http://mirrors.tencent.com/debian/ bookworm-backports main non-free-firmware contrib
EOF

网易镜像站

cat << EOF > /etc/apt/sources.list
deb http://mirrors.163.com/debian/ bookworm main non-free-firmware contrib
deb-src http://mirrors.163.com/debian/ bookworm main non-free-firmware contrib
deb http://mirrors.163.com/debian-security/ bookworm-security main
deb-src http://mirrors.163.com/debian-security/ bookworm-security main
deb http://mirrors.163.com/debian/ bookworm-updates main non-free-firmware contrib
deb-src http://mirrors.163.com/debian/ bookworm-updates main non-free-firmware contrib
deb http://mirrors.163.com/debian/ bookworm-backports main non-free-firmware contrib
deb-src http://mirrors.163.com/debian/ bookworm-backports main non-free-firmware contrib
EOF

清华大学镜像站

cat << EOF > /etc/apt/sources.list
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free-firmware
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free-firmware
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free-firmware
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free-firmware
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free-firmware
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free-firmware
deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free-firmware
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free-firmware
EOF

错误命令行:

root@ubuntu:/etc/apt# apt install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
vim : Depends: vim-common (= 2:7.4.052-1ubuntu3.1) but 2:8.0.1453-1ubuntu1 is to be installed
E: Unable to correct problems, you have held broken packages.

原因
已有安装过vim,但没有安装成功

解决方案
先执行 sudo apt-get remove vim-common 卸载vim-common
再进行安装vim,执 sudo apt-get install vim

今天,Linus Torvalds 宣布了 Linux 6.5 内核系列的最终发布,这是一个重大更新,引入了多项新功能,更新和新的驱动程序以获得更好的硬件支持,以及其他更改。

经过七个RC版本的测试,Linux内核6.5现已发布,新功能包括在ALSA中支持MIDI 2.0,为RISC-V架构提供ACPI支持,为UML(User-Mode Linux)提供Landlock支持,更好地支持AMD“Zen”系统,以及对ARMv8.8 memcpy/memset指令的用户空间支持,改进 Btrfs 性能等。

Linux内核6.5现在可以从Linus Torvalds’ Git treekernel.org网站下载。但这是源代码,需要手动编译。如果这不符合你的口味,你将不得不等待Linux 6.5出现在你的GNU/Linux发行版的稳定软件存储库中以进行更新。

Ubuntu是站长个人常用的系统镜像,当然有些在运维的时候还是需要用到一些命令操作的。这里整理常用的20个Ubuntu常用命令。

1、ls:列出当前目录下的文件和文件夹。

示例:ls

2、cd:切换到指定目录。

示例:cd /home/user/Documents

3、pwd:显示当前工作目录的路径。

示例:pwd

4、mkdir:创建一个新的目录。

示例:mkdir new_directory

5、rm:删除文件或目录。

示例:rm file.txt 或 rm -r directory

6、cp:复制文件或目录。

示例:cp file.txt new_file.txt 或 cp -r directory new_directory

7、mv:移动文件或目录,可以同时用于重命名文件或目录。

示例:mv file.txt new_location/file.txt 或 mv file.txt new_name.txt

8、cat:显示文件内容。

示例:cat file.txt

9、grep:在文件中搜索指定的字符串。

示例:grep “search_string” file.txt

10、chmod:修改文件或目录的权限。

示例:chmod 755 file.txt

11、chown:修改文件或目录的所有者。

示例:chown user:group file.txt

12、chgrp:修改文件或目录的所属组。

示例:chgrp group file.txt

13、apt-get:用于安装、更新和卸载软件包。

示例:sudo apt-get install package_name

14、dpkg:用于管理Debian软件包。

示例:sudo dpkg -i package.deb

15、wget:从网络上下载文件。

示例:wget http://example.com/file.txt

16、tar:打包和解压缩文件。

示例:tar -cvf archive.tar file1 file2 或 tar -xvf archive.tar

17、ssh:通过SSH协议远程登录到另一台计算机。

示例:ssh username@remote_host

18、top:显示系统中运行的进程和资源占用情况。

示例:top

19、ifconfig:显示和配置网络接口信息。

示例:ifconfig

20、ping:测试与另一台计算机之间的连通性。

示例:ping 192.168.0.1

OpenSSL 是一个用于安全通信的开放源代码项目,可以用来检查证书。下面是使用 OpenSSL 检查证书的步骤

1.下载并安装 OpenSSL

在你的计算机上下载并安装 OpenSSL 工具。可从官网https://www.openssl.org/source/下载。下载后按照官网提供的安装方法进行安装。

2.导入证书

将证书导入 OpenSSL,可以通过以下命令将证书导入 OpenSSL 中:

openssl x509 -in cert_file.pem -text

复制

上述命令指出打印 cert_file.pem 证书文件的详细信息,其中包括证书持有人、颁发机构、有效期等。

3.验证证书

可以使用 OpenSSL 验证证书的有效性,可以使用以下命令:

openssl verify cert_file.pem

如果证书是有效的,命令将返回以下信息:

cert_file.pem: OK

如果证书无效,命令将返回以下信息:

cert_file.pem: C = US, O = Example Company, CN = example.com
error 18 at 0 depth lookup:self signed certificate
error cert_file.pem:self signed certificate

4.检查证书链

可以检查整个证书链的有效性,可以使用以下命令:

openssl verify -CAfile ca_file.pem cert_file.pem

上述命令用于检查 cert_file.pem 证书文件是否在 ca_file.pem 证书链中,同时也可以检查证书链的有效性。如果证书链有效,则命令将返回以下信息:

cert_file.pem: OK

如果证书链无效,则命令将返回以下信息:

cert_file.pem: C=US,O=Example Company,CN=example.com
error 20 at 0 depth lookup:unable to get local issuer certificate
error cert_file.pem: verification failed

复制

上述异常错误信息也可能有多种,可以根据具体的错误类型进行诊断。

5.其他操作

OpenSSL 还支持其他证书查询与操作,如通过 OCSP 验证证书状态等。

综上所述,OpenSSL 提供了丰富的功能来检查证书,可以通过简单的命令行操作来实现。同时,需要注意正确安装 OpenSSL 后才能进行上述的检查操作。

错误信息 “/lib64/libm.so.6: version `GLIBC_2.27′ not found” 表示程序运行需要 GLIBC 2.27 版本的 libc 库,而系统中安装的 libc 库的版本较低,因此无法找到所需的库。

要解决这个问题,一种可行的方法是更新 libc 库的版本。可以按照以下步骤进行操作:

  1. 检查系统中 libc 库的版本。可以使用以下命令查看:
ldd --version

该命令会返回系统中的 libc 库版本以及其他相关信息。

  1. 下载 GLIBC 2.27 版本的源码包。可以从 GLIBC 的官方网站下载(http://ftp.gnu.org/gnu/glibc/glibc-2.27.tar.gz)。
  2. 解压源码包并进入源码目录。
  3. 编译并安装 GLIBC 2.27。可以使用以下命令:
./configure --prefix=/usr/local/glibc-2.27
make
sudo make install

请注意,安装的目录可以根据实际情况进行修改。

  1. 将新的 libc 库路径添加到 LD_LIBRARY_PATH 环境变量中。可以使用以下命令:
export LD_LIBRARY_PATH=/usr/local/glibc-2.27/lib:$LD_LIBRARY_PATH
  1. 重新运行程序,查看是否仍然出现错误。

请注意,更新系统的 libc 库版本可能会影响到其他程序的运行,因此在执行此操作之前,请仔细考虑潜在的影响,并在操作之前备份重要数据。

在大部分 Linux 系统中,cat 命令是通过 coreutils 包默认安装的。但是如果您在 Alpine Linux 中找不到 cat 命令,您可以按照以下步骤安装 Coreutils 包:

1. 使用 apk 命令更新索引:

apk update   

2. 安装 Coreutils 包:

apk add coreutils   

安装完成后,您就可以使用 cat 命令了。

注意:在 Alpine Linux 中,cat 命令位于 coreutils 包中,该包提供了其他常见的 Linux 命令,如 chgrp、chmod、chown、cp、date、dd、df等等,因此安装 coreutils 包是非常重要的。

在 Alpine Linux 中,可以使用多种命令来检查 CPU 利用率。以下是其中一些命令:

1. top 命令

   top 命令是一种交互式的进程监视器,它可以实时显示系统中正在运行的进程、CPU 利用率、内存使用情况等信息。启动 top 命令后,按下大写字母 P 键可以按 CPU 占用率排序,在第三行中可以看到 CPU 利用率的统计信息。

top -n 1

2. mpstat 命令

   mpstat 命令用于监视多处理器系统中每个处理器的使用情况。以下命令将显示每个 CPU 的利用率百分比。

mpstat -P ALL 1

   该命令使用 -P 参数指定要监视的处理器,并指定每秒钟进行一次采样。

3. vmstat 命令

   vmstat 命令用于监视系统的虚拟内存、进程、系统CPU、硬盘和CPU的使用情况,其中包括 CPU 利用率。以下命令将显示 CPU 利用率的详细统计信息。

vmstat 1

   该命令使用 1 秒的采样间隔,并在第 4 列和第 13 列中显示 CPU 利用率的详细统计信息。

4. sar 命令

   sar 命令用于收集系统性能信息,包括 CPU 利用率、内存使用情况、磁盘和网络 I/O 等。以下命令将从当前时间开始每秒钟记录 CPU 利用率信息。

sar -u 1   

   该命令使用 -u 参数指定要监视的 CPU 利用率,并指定每秒钟进行一次采样。

以上是一些常见的获取 CPU 利用率的方法和命令,您可以根据需要进行选择和调整。同时,请注意在实际应用中综合使用多种命令,以充分了解系统的整体性能和各项指标。

“nslookup: command not found” 是由于您的系统找不到 nslookup 命令引起的。nslookup 是一个常用的 DNS 查询工具,通常在 Linux 和 Unix 系统上使用。

要解决这个问题,您需要检查您的系统是否安装了 nslookup 工具。您可以尝试通过以下命令检查:

which nslookup

如果输出为空或返回 “command not found”,则说明您的系统未安装 nslookup 工具。

对于基于 Debian 或 Ubuntu 的系统,您可以通过以下命令安装 nslookup 工具:

sudo apt-get update
sudo apt-get install dnsutils

对于基于 Red Hat 或 CentOS 的系统,您可以通过以下命令安装 nslookup 工具:

sudo yum update
sudo yum install bind-utils

如果您使用的是其他 Linux 或 Unix 发行版,请参考其文档或使用适合您的包管理器来安装 nslookup 工具。

如果命令仍然无法找到 nslookup,您可以尝试直接使用 IP 地址查询 DNS 记录,例如:

ping -c1 example.com

这将向 example.com 发送一个 ICMP 回显请求并显示其 IP 地址。

希望以上信息可以帮助您解决问题。