Avatar

Organizations

  • 脚本基于Centos 7

    1、查看系统内核版本

    $ uname -r
    3.10.0-514.26.2.el7.x86_64
    $ cat /etc/redhat-release 
    CentOS Linux release 7.6.1810 (Core)
    

    2、升级内核

    导入elrepo的key,然后安装elrepo的yum源

    $ rpm -import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
    $ rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
    

    查看可用的内核相关包

    $ yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
    

    主分支ml(mainline),稳定版(stable),长期维护版lt(longterm) 安装内核

    $ yum -y --enablerepo=elrepo-kernel install kernel-ml.x86_64 kernel-ml-devel.x86_64
    

    3、修改grub

    # 查看系统内核列表
    $ awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
    CentOS Linux (5.2.11-1.el7.elrepo.x86_64) 7 (Core) # 新内核(5.2.11)在0的位置上
    CentOS Linux (3.10.0-957.27.2.el7.x86_64) 7 (Core)
    CentOS Linux (3.10.0-514.26.2.el7.x86_64) 7 (Core)
    CentOS Linux (3.10.0-514.el7.x86_64) 7 (Core)
    CentOS Linux (0-rescue-963c2c41b08343f7b063dddac6b2e486) 7 (Core)
    
    $ vim /etc/default/grub
    #将 GRUB_DEFAULT=saved 改为 GRUB_DEFAULT=0
    
    # 重建内核配置
    $ grub2-mkconfig -o /boot/grub2/grub.cfg
    
    # or 使用第二种命令行方式,减少操作复杂度,减少出错机率
    
    # 查看系统内核列表
    $ cat /boot/grub2/grub.cfg | grep menuentry
    if [ x"${feature_menuentry_id}" = xy ]; then
      menuentry_id_option="--id"
      menuentry_id_option=""
    export menuentry_id_option
    menuentry 'CentOS Linux (6.2.2-1.el7.elrepo.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1160.81.1.el7.x86_64-advanced-fbc2582d-2e8a-4c41-8ba8-83656d8df89b' {
    menuentry 'CentOS Linux (3.10.0-1160.83.1.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1160.81.1.el7.x86_64-advanced-fbc2582d-2e8a-4c41-8ba8-83656d8df89b' {
    menuentry 'CentOS Linux (3.10.0-1160.81.1.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1160.81.1.el7.x86_64-advanced-fbc2582d-2e8a-4c41-8ba8-83656d8df89b' {
    menuentry 'CentOS Linux (3.10.0-1160.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1160.el7.x86_64-advanced-fbc2582d-2e8a-4c41-8ba8-83656d8df89b' {
    menuentry 'CentOS Linux (0-rescue-9745ea2ecc634c89aef55f4dc21ee8fc) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-9745ea2ecc634c89aef55f4dc21ee8fc-advanced-fbc2582d-2e8a-4c41-8ba8-83656d8df89b' {
    # 配置默认内核
    $ grub2-set-default "CentOS Linux (6.2.2-1.el7.elrepo.x86_64) 7 (Core)"
    # 验证修改结果
    $ grub2-editenv list
    saved_entry=CentOS Linux (6.2.2-1.el7.elrepo.x86_64) 7 (Core)
    

    4、重启系统

    $ reboot
    
    #查看内核版本
    $ uname -r
    5.2.11-1.el7.elrepo.x86_64
    
    kernel Linux Created Tue, 08 Feb 2022 10:17:48 +0800
  • 基于Centos 7

    注意:CentOS 7默认的防火墙不是iptables,而是firewalld

    停止firewalld服务

    # 停止firewalld服务
    systemctl stop firewalld
    # 禁用firewalld服务
    # systemctl mask firewalld
    # 删除firewalld
    yum erase firewalld
    

    安装 iptables

    # 先检查是否安装了iptables
    systemctl status iptables
    # 安装iptables
    yum install iptables iptables-services -y
    

    启动 iptables

    # 注册iptables服务,相当于以前的chkconfig iptables on
    systemctl enable iptables
    # 开启服务
    systemctl start iptables
    # 查看状态
    systemctl status iptables
    # 重启防火墙
    systemctl restart iptables
    # 保存规则
    service iptables save
    # 如果报“-bash: service: command not found”,则需要安装initscripts
    yum install initscripts -y
    

    关键规则

    注意添加规则的先后顺序

    iptables Firewall linux Created Tue, 08 Feb 2022 10:09:48 +0800
  • 1、准备环境

    建议使用纯linux系统进行编译,如 Ubuntu 20.04 LTS,这样环境比较简单,兼容问题也少。

    用WSL环境编译,可参考

    为提高编译的成功率,采用国内大神Lean的版本。

    官方版本因编译过程中,下载、兼容等等太多问题,建议不要使用。

    sudo apt-get update
    sudo apt-get -y install build-essential asciidoc binutils bzip2 gawk gettext git libncurses5-dev libz-dev patch python3 python2.7 unzip zlib1g-dev lib32gcc1 libc6-dev-i386 subversion flex uglifyjs git-core gcc-multilib p7zip p7zip-full msmtp libssl-dev texinfo libglib2.0-dev xmlto qemu-utils upx libelf-dev autoconf automake libtool autopoint device-tree-compiler g++-multilib antlr3 gperf wget curl swig rsync
    

    2、下载代码

    国内用户请准备好梯子

    git clone https://github.com/coolsnowwolf/lede
    cd lede
    ./scripts/feeds update -a
    ./scripts/feeds install -a
    

    3、配置组件

    make menuconfig
    

    Hyper-V平台配置,可参考

    make OpenWRT Created Tue, 08 Feb 2022 10:05:20 +0800
  • 1. 升级系统软件包

    $ apt-get update
    $ apt-get dist-upgrade
    

    2. 安装 ifupdown2

    使用 ifupdown2 网络管理软件包,还可以实时重新加载网络配置,而无需重新启动。

    $ apt install ifupdown2
    

    3. 配置物理网卡

    使用 PVE 的 WEB 管理界面,设置物理网卡的配置。

    注意:一定要分配一个静态的IP地址,否则无法正常登录 PVE

    节点 > 系统 > 网络:

    • address:192.168.10.66/24
    • gateway:192.168.10.1

    保存后应用配置

    4. 配置虚拟网卡

    4.1 修改虚拟网卡配置

    使用 PVE 提供管理界面进行配置。 删除 vmbr0 设备的所有配置内容。

    编辑 /etc/network/interfaces

    $ nano /etc/network/interfaces
    

    加入

    source /etc/network/interfaces.d/*
    
    # network interface settings; autogenerated
    # Please do NOT modify this file directly, unless you know what
    # you're doing.
    #
    # If you want to manage parts of the network configuration manually,
    # please utilize the 'source' or 'source-directory' directives to do
    # so.
    # PVE will preserve these directives, but will NOT read its network
    # configuration from sourced files, so do not attempt to move any of
    # the PVE managed interfaces into external files!
    
    source /etc/network/interfaces.d/*
    
    auto lo
    iface lo inet loopback
    
    auto enp1s0
    iface enp1s0 inet static
            address 192.168.10.66/24
            gateway 192.168.10.1
    
    iface vmbr0 inet manual
            bridge-ports none
            bridge-stp off
            brideg-fd
    

    4.2 新建虚拟网卡配置

    创建新的虚拟网卡配置

    NAT proxmoxve Created Mon, 07 Feb 2022 14:07:32 +0800
  • 本文记录OpenWrt设置透明代理的步骤及原理。

    1. 系统环境

    • 硬件环境:Windows 10 Hyper-V虚拟机,单核处理器,256M内存
    • 固件版本:OpenWrt 19.07.6 r11278-8055e38794 / LuCI openwrt-19.07 branch git-21.101.59933-c56d9f2
    • shadowsocks-libev:3.3.5
    • luci-app-shadowsocks:2.0.2
    • v2ray-plugin:4.37.3-20210413
    • ChinaDNS:1.3.3
    • luci-app-chinadns:1.6.2
    • https-dns-proxy:2021-01-17-5
    • luci-app-https-dns-proxy:git-21.062.76689-a607f9c-1
    • luci-i18n-https-dns-proxy-zh-cn:git-21.062.76689-a607f9c-1

    2. 运行流程

    透明代理的运行原理主要由DNS访问流程IP访问流程两部分组成组成

    2.1 DNS访问流程

    flowchart LR
        lh((localhost));
        dm(Dnsmasq);
        cd(ChinaDNS);
        hdp(https-dns-proxy);
        ss(Shadowsocks);
        fd(国外DNS);
        dd(国内DNS);
    
        lh -- 1 udp:53 --> dm;
        dm -- 2 udp:5555 --> cd;
        cd -- 3.1 udp:5353--> hdp;
        cd -- 3.2 udp --> dd;
        hdp -- 4 tcp --> ss;
        ss -- 5 tcp --> fd;
    
        fd -. 6 .-> ss;
        ss -. 7 .-> hdp;
        hdp -. 8.1 .-> cd;
        dd -. 8.2 .-> cd;
        cd -. 9 缓存 .-> dm;
        dm -. 10 IP地址 .-> lh;
    

    2.2 IP访问流程

    flowchart LR
        fs(国外服务器);
        ds(国内服务器);
        lh((localhost));
        route{chinadns_chnroute.txt};
        ss(Shadowsocks);
    
        lh -- 1 --> route;
        route -- 2.1 国内IP --> ds;
        route -- 2.2 国外IP --> ss;
        ss -- 3 加密访问 --> fs;
    
        fs -. 4 返回加密数据 .-> ss;
        ds -. 5.1 返回数据 .-> lh;
        ss -. 5.2 返回解密数据 .-> lh;
    

    3. 配置

    3.1 Shadowsocks-libev + v2ray-plugin

    • 服务器管理-编辑服务器-插件参数:一定要加上"loglevel=none",如果不加v2ray-plugin插件几分钟就会被系统杀死
    • 访问控制-被忽略IP列表:/etc/chinadns_chnroute.txt

    3.2 https-dns-proxy

    由于使用了v2ray-plugin 插件,导致Shadowsocks不再支持UDP包,所以使用TCP的方式查询DNS

    v2ray proxy ShadowSocks OpenWRT Created Mon, 07 Feb 2022 13:37:19 +0800
  • 1. 使用自定义域名访问WEB管理界面

    • 网络-DHCP/DNS-常规设置-DNS转发:/openwrt.lan/192.168.xxx.xxx
    • 网络-DHCP/DNS-常规设置-重绑定保护:不勾选
    • /etc/config/uhttpd-option redirect_https:‘1’ 不强制跳转https
    DNS OpenWRT Created Mon, 07 Feb 2022 13:17:39 +0800
  • 1. 安装软件包

    # opkg install kmod-nf-nathelper-extra
    # opkg install kmod-nf-ipvs-ftp
    

    2. 添加配置

    # nano /etc/sysctl.d/11-nf-conntrack.conf
    

    增加"net.netfilter.nf_conntrack_helper=1"

    在使用FTP的主动模式时,要注意关闭网关上的ALG功能(如Windows ALG服务),同时使用可能引起失败

    参考文档

    FTP ALG OpenWRT Created Mon, 07 Feb 2022 10:08:02 +0800
  • 1. 复制

    2. 导入

    2.1 使用 LOAD DATA

    3. 删除

    mysql Created Tue, 18 Jan 2022 14:50:28 +0800
  • urllib.parse.urlencode(query, doseq=False, safe=’’, encoding=None, errors=None, quote_via=quote_plus) urllib.parse.urlencode 将对象或两元素序列转换为百分比编码的ASCII文本字符串,字符串是由’&‘字符分隔的一系列 key=value 对,其中 key 和 value 都使用 quote_via 函数引用。

    GET 请求

    import urllib
    params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
    url = "http://www.musi-cal.com/cgi-bin/query?%s" % params
    with urllib.request.urlopen(url) as f:
        print(f.read().decode('utf-8'))
    

    POST 请求

    import urllib.request
    import urllib.parse
    data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
    data = data.encode('ascii')
    with urllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f:
        print(f.read().decode('utf-8'))
    

    如果结果字符串要用作具有 urlopen() 函数的POST操作的 data,则它应该被编码为字节,否则将导致 TypeError。

    默认情况下 urlencode 函数使用 quote_plus() 函数进行编码,也可以选用 quote() 函数进行编码,两者最大的不同在于对特定字符的处理。

    urllib.parse.quote_plus(string, safe=’’, encoding=None, errors=None) 默认情况下使用 quote_plus() 函数,它将空格被编码为 ‘+’ 字符,而“/”字符被编码为 %2F,它遵循GET请求(application/x-www-form-urlencoded)的标准。 urllib.parse.quote(string, safe=’/’, encoding=None, errors=None) 可以作为备用的函数是 quote(),它将空格编码为 %20,字母,数字和 ‘_.-‘字符不被编码,而“/”字符被默认为安全字符不被编码。

    编码时会对所有字符进行编码处理,这会导致一些情况下编码后的参数不能被正确的识别,所以在使用时可根据编码的需求,指定一个编码函数。

    urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}, quote_via=urllib.parse.quote)
    

    但即使指定函数,还会有一些特殊情况,需要保留一些特定的字符不被编码。 比如,我们想得到下面的参数

    constraints[colors][0]=blue&constraints[colors][1]=%E7%99%BD%20%E8%89%B2&constraints[colors][2]=red&order=newest
    

    但无论使用 quote_plus() 或 quote(),“[” 和 “]” 字符一定会被编码。

    url编码 Python Created Mon, 17 Jan 2022 15:24:58 +0800
  • 基于Centos 7

    查看设备信息

    # 简单接口状态
    $ nmcli dev status
    # 详细的接口信息
    $ nmcli dev show
    # 接口的详细信息
    $ nmcli dev show <interface-name>
    

    查看连接(connection)的信息

    # 简单信息
    $ nmcli conn show
    # 详细的连接信息
    $ nmcli conn show
    # 某个连接的详细信息
    $ nmcli conn show <conn-name>
    

    创建连接

    nmcli conn add type <ethernet> con-name <conn-name> ifname <dev-name> ip4 <192.168.100.100/24> [gw4 <192.168.100.1>]
    

    修改配置

    静态IP改动态IP

    nmcli conn mod <conn-name> /
        ipv4.method auto /
        ipv4.address "" /
        ipv4.gateway "" /
        ipv4.dns ""
    

    修改DNS

    $ nmcli conn mod <conn-name> ipv4.dns "1.2.4.8"
    

    设置自动连接

    # 查询和显示所有网络连接的自动连接属性的当前值
    $ nmcli -f name,autoconnect connection
    # 更改网络连接的属性值
    $ nmcli conn mod <conn-name> connection.autoconnect yes
    

    配置生效

    nmcli conn up <conn-name>
    

    激活连接

    $ nmcli conn up <conn-name>
    $ nmcli conn down <conn-name>
    
    $ nmcli dev connect <dev-name>
    $ nmcli dev disconnect <dev-name>
    

    建议使用 nmcli dev disconnect interface-name 命令,而不是 nmcli con down connection-name 命令,因为连接断开可将该接口放到“手动”模式,这样做用户让 NetworkManager 启动某个连接前,或发生外部事件(比如载波变化、休眠或睡眠)前,不会启动任何自动连接。

    CLI nmcli network Linux Created Mon, 17 Jan 2022 15:17:58 +0800