• AlmaLinux 9  安装配置WireGuard

    前言:相较于当下IPSec,OpenVPN等VPN技术,WireGuard被誉为下一代VPN技术,而且Linux的管理者Linus Torvalds已经将WireGuard合并至内核5.6中。所以所有使用新版本内核的服务器理论上都不需要安装WireGuard。参考的最佳实践链接 https://orcacore.com/install-configure-wireguard-almalinux-9/

    安装部分:您必须在服务器上启用“wireguard”内核模块。为此,您可以使用以下命令:

    sudo modprobe wireguard
    然后,使用以下命令验证您的模块是否已启用:
    lsmod | grep wireguard
    如果 WireGuard 模块已启用,您应该得到以下输出:

    Output
    wireguard 94208 0
    libblake2s 16384 1 wireguard
    ip6_udp_tunnel 16384 1 wireguard
    udp_tunnel 24576 1 wireguard
    curve25519_x86_64 36864 1 wireguard
    libcurve25519_generic 49152 2 curve25519_x86_64,wireguard

    现在您需要永久加载wireguard 模块。为此,请运行以下命令:
    sudo echo wireguard > /etc/modules-load.d/wireguard.conf
    该命令将在AlmaLinux 9上的系统启动时永久加载wireguard内核模块。
    最后,使用以下命令安装“wireguard-tools”软件包:
    sudo dnf install wireguard-tools -y #该软件包用于管理 Wireguard 服务器。

    配置部分1: 首先需要生成私钥与公钥对用于通信验证

    wg genkey | sudo tee /etc/wireguard/private.key #文件将会存在 /etc/wireguard/private.key
    sudo chmod 0400 /etc/wireguard/private.key #给文件定义权限
    sudo cat /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.pub #通过私钥生成公钥
    如果没记住可以查询公钥私钥内容
    cat /etc/wireguard/private.key
    cat /etc/wireguard/public.pub

    如果需要在服务器上生成其它服务器或客户端的公私钥。
    mkdir -p /etc/wireguard/clients
    wg genkey | sudo tee /etc/wireguard/clients/client01.key
    cat /etc/wireguard/clients/client01.key | wg pubkey | tee /etc/wireguard/clients/client01.pub

    配置部分2:

    服务器端配置(服务器作为VPN的HUB节点需要有真实固定IP并且开放指定的UDP端口,端口号自行指定)如果只在WireGuard虚拟网络中通信。不需要路由到下面的其它子网则不需要 在配置文件中定义 PostUp = iptables -A FORWARD xxxxx 和 PostDown = iptables -D FORWARD xxxxxxx

    sudo vi /etc/wireguard/wg0.conf

    [Interface]
    Address = 10.100.200.1/24 # 此服务器在WireGuard网络中的IP地址和所属子网
    SaveConfig = false # 可以是true或者false(true的话会在此文件的peer配置里自动写入客户端公网ip)
    ListenPort = 12347 #服务器端监听的UDP端口
    PrivateKey = CLInDXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXPncWw= #服务器私钥
    PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

    [Peer]
    PublicKey = T90oXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXTQLbzc= #客户端1的公钥
    AllowedIPs = 10.100.200.11/32 #容许客户端连接的地址

    [Peer]
    PublicKey = T90o2XXXXXXXXXXXXXXXXXXXXXXXXXXXXaeTQLbzc= #客户端2的公钥
    AllowedIPs = 10.100.200.12/32 #容许客户端连接的地址

    配置部分3:

    客户端配置(客户端可以在多层NAT之后,如果客户端配置里没有指定UDP端口,则客户端会使用随机UDP端口与定义的服务器UDP端口通信)

    客户端如果是WireGuard客户端软件的话,在软件中创建空隧道就会自动创建密钥对(公钥和私钥)
    [Interface]
    Address = 10.100.200.2/24 (可以是/24也可以是/32不影响)
    PrivateKey = 8ExJ6XvuXXXXXXXXXXXXXXXXXXXEDZyou7X2U= #客户端主机的私钥(不是服务器的私钥)

    [Peer]
    Endpoint = 1.2.3.4:12347 # 目标服务器的公网地址及端口
    PublicKey = T90o2KXXXXXXXXXXXXXXXXXXXXXXXXXXe9aeTQLbzc= #注意:这里填服务器主机的公钥
    AllowedIPs = 0.0.0.0/1, 128.0.0.0/1 #客户端对外访问的路由地址范围,0.0.0.0/0 备选(/32的话会不通)
    PersistentKeepalive = 25

    启动与管理部分:

    sudo wg-quick up /etc/wireguard/wg0.conf
    sudo wg-quick down /etc/wireguard/wg0.conf
    systemctl enable wg-quick@wg0
    systemctl status wg-quick@wg0
    systemctl disable wg-quick@wg0

  • 在使用 Nginx 进行 WebSocket 负载均衡时的问题

    WebSocket 协议需要特定的 Nginx 配置来正确处理连接。确保你的 Nginx 配置文件中包含以下内容:

    Nginx 配置问题

    upstream websocket {
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
    }
    
    server {
        listen 80;
    
        location /ws/ {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }

    }

    关键配置解释-WebSocket 协议升级

    WebSocket 协议通过 HTTP 协议进行初始握手,然后升级到 WebSocket 协议。确保 Nginx 正确处理了 UpgradeConnection 头。

    proxy_http_version 1.1; 是WebSocket连接必须的
    proxy_set_header Upgrade $http_upgrade; 之后这两行是将 头协议从HTTP协议级成WebSocket协议
    proxy_set_header Connection $connection_upgrade;

    超时设置

    WebSocket 连接通常是长连接,确保 Nginx 的超时设置足够长。

    proxy_connect_timeout 7d;
    proxy_send_timeout 7d;
    proxy_read_timeout 7d;

  • Linux中申请Let’s Encrypt通配符证书

    1. 使用的工具,使用certbot申请并使用acme-dns-client作为manual-auth-hook执行更新证书的验证工作。
    • 安装certbot:在不同的Linux发行版中可以使用apt install certbot, yum install epel-release 之后再yum install certbot以及 dnf install certbot
    • 安装certbot之后会有以下提示,自动证书更新服务没有启动,但是新版本的话当使用certbot申请证书后,certbot-renew.timer服务会自动开启

    ###Certbot auto renewal timer is not started by default.

    ###Run ‘systemctl start certbot-renew.timer’ to enable automatic renewals.

    systemctl status certbot-renew.timer

    • 使用acme-dns-client注册需要申请证书的域名,每一个执行一次并按提示修改DNS记录,因为CAA记录验证属于有风险操作所以 {必选参数 –dangerous},例子如下

    acme-dns-client register -d cn-it.org -s https://auth.acme-dns.io –dangerous

    acme-dns-client register -d 51azure.com -s https://auth.acme-dns.io –dangerous

    acme-dns-client register -d yuushatech.com -s https://auth.acme-dns.io –dangerous

    acme-dns-client register -d maoutech.com -s https://auth.acme-dns.io –dangerous

    acme-dns-client register -d 5i818.com -s https://auth.acme-dns.io –dangerous

    acme-dns-client register -d 51aws.com -s https://auth.acme-dns.io –dangerous

    每一次命令输入完都会有一个通过DNS验证域名所有权的过程,需要添加CNAME记录,如下

    _acme-challenge.cn-it.org.     IN      CNAME   6185f842-efb2-4c65-9813-a0c410ea36ac.auth.acme-dns.io

    • 在验证完CNAME记录之后会提示验证CAA记录,需要在DNS中添加CAA记录

    caa         @            letsencrypt.org 0 issue  (单域名证书验证记录)

    caa         @            letsencrypt.org 0 issuewild (通配符证书验证记录)

    阿里云DNS注意事项,DNS记录值中的 “letsencrypt.org” 一定要有””

    • 验证CNAME的过程中可能会有警告信息,说明这个服务器以前做过相同操作留有旧的账户信息。

    查看 /etc/letsencrypt/accounts/  目录下可能有两个账号,删掉老的。

    Do you wish to set up a CAA record with accounturi now? [y/N]: y

    [i] Found a total of 2 ACME account(s) on this system:

     [Certbot] URI: https://acme-staging-v02.api.letsencrypt.org/acme/acct/177419874

    • 在所有域名验证完成后,申请证书。Challenges模式是 dns,auth-hook 是acme-dns-client。由于新的证书安全性需要,通配符证书必须还要配合根域域名证书才能信任,所以要同时申请。

    certbot certonly –manual –preferred-challenges dns \

    –manual-auth-hook ‘acme-dns-client’ -d “*.cn-it.org” -d cn-it.org -d “*.51azure.com” -d 51azure.com -d “*.yuushatech.com” -d yuushatech.com -d “*.maoutech.com” -d maoutech.com -d “*.5i818.com” -d 5i818.com -d “*.51aws.com” -d 51aws.com

    • 如果之前的CNAME记录及CAA记录都完好可查的情况下,证书应该可以顺利签署。

    使用命令 certbot renew –dry-run 可以测试证书是否可以正常renew,使用命令 systemctl status certbot-renew.timer 可以查看自动renew服务的状态

    1. 关于配置文件及自动运行脚本

    配置文件位置 /etc/letsencrypt/renewal/cn-it.org.conf

    自动运行脚本 /etc/letsencrypt/renewal-hooks/post/pkcs12convert.sh

    脚本内容如下,每次自动更新证书后会自动运行renewal-hooks/post 中的脚本

    #!/bin/sh

    openssl pkcs12 -export -out /etc/letsencrypt/live/cn-it.org/cn-it.org.pfx -inkey /etc/letsencrypt/live/cn-it.org/privkey.pem -in /etc/letsencrypt/live/cn-it.org/cert.pem -certfile /etc/letsencrypt/live/cn-it.org/chain.pem -passout pass:XXXxxxXXX

    echo “pkcs#12 generated!”

    systemctl restart nginx

    echo “Nginx restarted”

  • 转:BBR for CentOS 7/debian8 整合分享

    Centos7转自源网站
    http://www.hostloc.com/thread-342505-1-1.html
    Debian8转自源网站
    http://www.awkxy.com/archives/721
     


    Centos7
    先把/etc/sysctl.conf 文件中 关于 net.ipv4.tcp_congestion_control的配置注释掉。(Azure的CentOS 7本来就没有)
    wget http://mirrors.kernel.org/debian/pool/main/l/linux/linux-image-4.13.0-1-amd64_4.13.4-1_amd64.deb
    ar x linux-image-4.13.0-1-amd64_4.13.4-1_amd64.deb
    tar -Jxf data.tar.xz
    install -m644 boot/vmlinuz-4.13.0-1-amd64 /boot/vmlinuz-4.13.0-1-amd64
    cp -Rav lib/modules/4.13.0-1-amd64 /lib/modules
    depmod -a 4.13.0-1-amd64
    dracut -f -v –hostonly -k ‘/lib/modules/4.13.0-1-amd64’ /boot/initramfs-4.13.0-1-amd64.img 4.13.0-1-amd64
    grub2-mkconfig -o /boot/grub2/grub.cfg
    #开启bbr
    echo “net.core.default_qdisc=fq” >> /etc/sysctl.conf
    echo “net.ipv4.tcp_congestion_control=bbr” >> /etc/sysctl.conf
    #调整内核启动顺序
    grub2-set-default “CentOS Linux (4.13.0-1-amd64) 7 (Core)”
    grub2-editenv list
    grub2-mkconfig -o /boot/grub2/grub.cfg
    然后reboot


    ubuntu/debian系统
    下载新内核:
    wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.11.2/linux-image-4.11.2-041102-generic_4.11.2-041102.201705201036_amd64.deb
    安装内核:
    dpkg -i linux-image-4.11.[Tab补全]
    删除其他内核:
    dpkg -l|grep linux-image
    apt-get remove linux-image-4.9.0-040900rc8-generic #删4.11.0以外的旧内核
    apt-get remove linux-image-4.11.0-trunk-amd64 #删4.11.0以外的旧内核
    更新 grub 系统引导文件并重启(Azure虚机执行update-grub报错时看下面)
    update-grub
    reboot
    开启BBR
    echo “net.core.default_qdisc=fq” >> /etc/sysctl.conf
    echo “net.ipv4.tcp_congestion_control=bbr” >> /etc/sysctl.conf
    sysctl -p
    sysctl net.ipv4.tcp_available_congestion_control
    查看下是否有BBR:lsmod | grep bbr


    Azure的Debian8 没有dracut
    apt-get install dracut
    Azure的Debian8 grub安装不全
    sudo apt-get update; sudo apt-get install –reinstall grub
    mkdir /boot/grub
    Linux小技巧
    1,查看发行版
    lsb_release -a
    2,查看内核
    cat /proc/version
    uname -a
    uname -r
    3,查看系统位数
    file /bin/ls
    4,验证BBR
    sysctl net.ipv4.tcp_available_congestion_control
    sysctl net.ipv4.tcp_congestion_control
     

  • 转:Custom linux boot up screen

    Displaying an image during boot instead of the default command line scrolling text

    This is based on the guide here.
    This solution works but there are a few seconds of text shown before the boot image appears.

    Install fbi
    
    sudo apt-get install fbi
    
    Copy the splashscreen image to be used

    Copy your custom splash image into: /etc/ and name it “splash.png”.
    Presumably the resolution to use is 1920x1080px.
    Create A Script

    
    sudo nano
    

    Paste the following into the text editor:

    
    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides:          asplashscreen
    # Required-Start:
    # Required-Stop:
    # Should-Start:
    # Default-Start:     S
    # Default-Stop:
    # Short-Description: Show custom splashscreen
    # Description:       Show custom splashscreen
    ### END INIT INFO
    do_start () {
        /usr/bin/fbi -T 1 -noverbose -a /etc/splash.png
        exit 0
    }
    case "$1" in
      start|"")
        do_start
        ;;
      restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
      stop)
        # No-op
        ;;
      status)
        exit 0
        ;;
      *)
        echo "Usage: asplashscreen [start|stop]" >&2
        exit 3
        ;;
    esac
    :
    

    IMPORTANT – If copying and pasting via SSH check it has pasted in correctly (pasting via FiseSSH for us caused the # lines and the esac line to be altered and need modifying back to be correct)
    Exit and save the file as: /etc/init.d/asplashscreen
    (using a name starting with ‘a’ will ensure it runs first)
    Finally make the script executable and install it for init mode:

    
    sudo chmod a+x /etc/init.d/asplashscreen
    sudo insserv /etc/init.d/asplashscreen
    

    Thats it:

    
    sudo reboot
    

     

    Getting Out Of Black Screen

    If you get a black screen at the end of booting (if you’ve not setup auto running the GUI etc) use CTRL + ALT + F2 to get the command prompt