DEV Community

AXUM中文博客
AXUM中文博客

Posted on

使用自签证书在局域网部署 HTTPS 并让浏览器识别为『安全』

自签证书

我们将使用 openssl 来自签证书,请确保你已安装了该工具。

自签IP证书

openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout <私钥路径>.key -out ./<证书路径>.crt -subj '/CN=<组织名称>' -addext 'subjectAltName=IP:<局域网IP地址>'
Enter fullscreen mode Exit fullscreen mode
  • <私钥路径>:要保存私钥的路径
  • <证书路径>:要保存证书的路径
  • <组织名称>:签发证书的组织的名称
  • <局域网IP地址>:本机IP地址

举例:

openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout ./oneplus.key -out ./oneplus.crt -subj '/CN=ONEPLUS' -addext 'subjectAltName=IP:192.168.1.13'
Enter fullscreen mode Exit fullscreen mode

自签“域名”证书

和 IP 证书类似,自签“域名”证书的唯一不同的是 subjectAltName 要修改为 DNS:<域名>

openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout <私钥路径>.key -out <证书路径>.crt -subj '/CN=<组织名称>' -addext 'subjectAltName=DNS:<域名>'
Enter fullscreen mode Exit fullscreen mode
  • <域名>:绑定到该局域网的域名。通常,我们可以在客户机上修改 hosts 文件进行绑定。

举例:

openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout ./oneplus.key -out ./oneplus.crt -subj '/CN=ONEPLUS' -addext 'subjectAltName=DNS:oneplus.axum.rs'
Enter fullscreen mode Exit fullscreen mode

修改 hosts

  • Linux/MacOS:/etc/hosts
  • Windows:C:\Windows\System32\drivers\etc

在文件末尾加上:

<IP地址> <域名>
Enter fullscreen mode Exit fullscreen mode

举例:

192.168.1.13 oneplus.axum.rs
Enter fullscreen mode Exit fullscreen mode

Windows 安装根证书

将上一步骤生成的 .crt 文件通过 SFTP 等软件,复制到 Windows 中。然后双击该文件,点击安装,然后选择受信任的根证书颁发机构即可

termux 中 proot-distro debian 的 root 用户的家目录位于: /data/data/com.termux/files/usr/var/lib/proot-distro/installed-rootfs/debian/root

Code Server 设置

修改 Code Server 配置文件:

// ...
cert: <证书路径>.crt
cert-key: <私钥路径>.key
Enter fullscreen mode Exit fullscreen mode

举例:

// ...
cert: /root/.config/code-server/oneplus.crt
cert-key: /root/.config/code-server/oneplus.key
Enter fullscreen mode Exit fullscreen mode

重启 Code Server。

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay