Hero Image
如何設定時區與NTP服務在RHEL7/CentOS7

如何設定時區與 NTP 服務在 RHEL7/CentOS7 chrony 包含兩個程序,chronyd 是一個可以在啟動時啟動的守護進程,chronyc 是一個命令行界面程序,可用於監控 chronyd 的性能並在運行時更改各種運行參數。 注意 ntpd 和 chronyd 擇一就可,不要同時運作。 設定時區 ~# timedatectl set-timezone Asia/Taipei ~# timedatectl Local time: Tue 2018-03-27 14:13:38 CST Universal time: Tue 2018-03-27 06:13:38 UTC RTC time: Tue 2018-03-27 06:13:40 Time zone: Asia/Taipei (CST, +0800) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: n/a 設定 chronyd # 安裝 ~# yum install -y chrony # 配置設定檔 ~# cat /etc/chrony.conf # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server 0.tw.pool.ntp.org iburst --->改成本地的伺服器 server 1.tw.pool.ntp.org iburst --->改成本地的伺服器 server 2.tw.pool.ntp.org iburst --->改成本地的伺服器 server 3.tw.pool.ntp.org iburst --->改成本地的伺服器 # 啟動服務和設為開機時啟動 ~# systemctl enable chronyd ~# systemctl start chronyd racking 參數顯示有關系統時間效能 ~# chronyc tracking Reference ID : 3DD8996B (61-216-153-107.hinet-ip.hinet.net) --->表示現在同步的時間伺服器,如果沒有id表示沒有同步 Stratum : 4 --->表示計算機有多少"跳hop" 表示本地的是第四層 Ref time (UTC) : Tue Mar 27 06:03:38 2018 --->最後一次測量的時間 System time : 0.000040356 seconds fast of NTP time --->調整系統時間 Last offset : +0.000163738 seconds RMS offset : 0.000163738 seconds Frequency : 21.384 ppm fast Residual freq : +0.000 ppm Skew : 675.319 ppm Root delay : 0.008527911 seconds Root dispersion : 0.066466033 seconds Update interval : 2.0 seconds Leap status : Normal --->Normal要顯示此值, Insert second, Delete second or Not synchronised. ~# chronyc sources -v 210 Number of sources = 4 .-- Source mode '^' = server, '=' = peer, '#' = local clock. / .- Source state '*' = current synced, '+' = combined , '-' = not combined, | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable. || .- xxxx [ yyyy ] +/- zzzz || Reachability register (octal) -. | xxxx = adjusted offset, || Log2(Polling interval) --. | | yyyy = measured offset, || \ | | zzzz = estimated error. || | | \ MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 59-124-29-241.hinet-ip.h> 3 6 37 24 -1462us[-2363us] +/- 49ms ^+ 61-216-153-107.hinet-ip.> 3 6 37 23 -556us[ -556us] +/- 64ms ^? 59-125-122-217.hinet-ip.> 0 7 0 - +0ns[ +0ns] +/- 0ns ^- 61-216-153-105.hinet-ip.> 3 6 37 23 -280us[ -280us] +/- 64ms 看同步源頭的資訊 ~# chronyc sourcestats -v 210 Number of sources = 4 .- Number of sample points in measurement set. / .- Number of residual runs with same sign. | / .- Length of measurement set (time). | | / .- Est. clock freq error (ppm). | | | / .- Est. error in freq. | | | | / .- Est. offset. | | | | | | On the -. | | | | | | samples. \ | | | | | | | Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev ============================================================================== 59-124-29-241.hinet-ip.h> 6 5 135 -0.454 4.553 -784us 66us 61-216-153-107.hinet-ip.> 6 6 135 +4.455 19.761 +622us 247us 59-125-122-217.hinet-ip.> 0 0 0 +0.000 2000.000 +0ns 4000ms 61-216-153-105.hinet-ip.> 6 4 136 +8.965 42.440 +1250us 495us 將系統時間寫到硬體(主機板上的時間)上 ~# hwclock --systohc ~# date ; hwclock Tue Mar 27 14:07:57 CST 2018 Tue 27 Mar 2018 02:07:58 PM CST -0.938012 seconds

Hero Image
Install PowerDNS and PowerDNS-Admin on Ubuntu 22.04|20.04|18.04

Install PowerDNS and PowerDNS-Admin on Ubuntu 22.04|20.04|18.04 Master-Master PowerDNS with Galera Replication https://www.scaleway.com/en/docs/installing-powerdns-server-on-ubuntu-bionic/ Install PowerDNS $ sudo apt update $ sudo apt install mariadb-server -y $ sudo mysql -u root CREATE DATABASE powerdns; GRANT ALL ON powerdns.* TO 'powerdns'@'localhost' IDENTIFIED BY 'Str0ngPasswOrd'; FLUSH PRIVILEGES; USE powerdns; CREATE TABLE domains ( id INT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, master VARCHAR(128) DEFAULT NULL, last_check INT DEFAULT NULL, type VARCHAR(6) NOT NULL, notified_serial INT UNSIGNED DEFAULT NULL, account VARCHAR(40) CHARACTER SET 'utf8' DEFAULT NULL, PRIMARY KEY (id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE UNIQUE INDEX name_index ON domains(name); CREATE TABLE records ( id BIGINT AUTO_INCREMENT, domain_id INT DEFAULT NULL, name VARCHAR(255) DEFAULT NULL, type VARCHAR(10) DEFAULT NULL, content VARCHAR(64000) DEFAULT NULL, ttl INT DEFAULT NULL, prio INT DEFAULT NULL, change_date INT DEFAULT NULL, disabled TINYINT(1) DEFAULT 0, ordername VARCHAR(255) BINARY DEFAULT NULL, auth TINYINT(1) DEFAULT 1, PRIMARY KEY (id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE INDEX nametype_index ON records(name,type); CREATE INDEX domain_id ON records(domain_id); CREATE INDEX ordername ON records (ordername); CREATE TABLE supermasters ( ip VARCHAR(64) NOT NULL, nameserver VARCHAR(255) NOT NULL, account VARCHAR(40) CHARACTER SET 'utf8' NOT NULL, PRIMARY KEY (ip, nameserver) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE TABLE comments ( id INT AUTO_INCREMENT, domain_id INT NOT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(10) NOT NULL, modified_at INT NOT NULL, account VARCHAR(40) CHARACTER SET 'utf8' DEFAULT NULL, comment TEXT CHARACTER SET 'utf8' NOT NULL, PRIMARY KEY (id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE INDEX comments_name_type_idx ON comments (name, type); CREATE INDEX comments_order_idx ON comments (domain_id, modified_at); CREATE TABLE domainmetadata ( id INT AUTO_INCREMENT, domain_id INT NOT NULL, kind VARCHAR(32), content TEXT, PRIMARY KEY (id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE INDEX domainmetadata_idx ON domainmetadata (domain_id, kind); CREATE TABLE cryptokeys ( id INT AUTO_INCREMENT, domain_id INT NOT NULL, flags INT NOT NULL, active BOOL, content TEXT, PRIMARY KEY(id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE INDEX domainidindex ON cryptokeys(domain_id); CREATE TABLE tsigkeys ( id INT AUTO_INCREMENT, name VARCHAR(255), algorithm VARCHAR(50), secret VARCHAR(255), PRIMARY KEY (id) ) Engine=InnoDB CHARACTER SET 'latin1'; CREATE UNIQUE INDEX namealgoindex ON tsigkeys(name, algorithm); $ sudo systemctl disable systemd-resolved $ sudo systemctl stop systemd-resolved $ ls -lh /etc/resolv.conf lrwxrwxrwx 1 root root 39 Jul 24 15:50 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf $ sudo unlink /etc/resolv.conf $ echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf Add official PowerDNS repository for Ubuntu 22.04|20.04|18.04.

Hero Image
openvpn部署之部署基於AD域認證

openvpn 部署之部署基於 AD 域認證 OpenVPN + PAM + SSSD + Active Directory https://computingforgeeks.com/install-and-configure-openvpn-server-on-rhel-centos-8/ https://www.redhat.com/en/blog/consistent-security-crypto-policies-red-hat-enterprise-linux-8 https://medium.com/jerrynotes/linux-authentication-windows-ad-without-join-domain-7963c3fd44c5 # 安裝openvpn yum install openvpn -y yum -y install openssl openssl-devel -y yum -y install lzo lzo-devel -y yum install -y libgcrypt libgpg-error libgcrypt-devel # 安裝openvpn認證插件 yum install openvpn-auth-ldap -y # 安裝easy-rsa # 由於openvpn2.3之後,在openvpn裏面剔除了easy-rsa文件,所以需要單獨安裝 yum install easy-rsa cp -rf /usr/share/easy-rsa/2.0 /etc/opevpn/ # 生成openvpn的key及證書 # 修改 `/opt/openvpn/etc/easy-rsa/2.0/vars` 參數 export KEY_COUNTRY="CN" # 國家 export KEY_PROVINCE="ZJ" # 省份 export KEY_CITY="NingBo" # 城市 export KEY_ORG="TEST-VPN" # 組織 exportKEY_EMAIL="81367070@qq.com" # 郵件 export KEY_OU="baidu" # 單位 source vars ./clean-all ./build-ca ./build-dh ./build-key-server server ./build-key client1 # 編輯openvpn服務端配置文件:`/etc/openvpn/server.conf` port 1194 proto udp dev tun ca keys/ca.crt cert keys/server.crt key keys/server.key # This file should be kept secret dh keys/dh2048.pem server 10.8.0.0 255.255.255.0 //客戶端分配的ip地址 push "route 192.168.1.0 255.255.255.0" //推送客戶端的路由 push "redirect-gateway" //修改客戶端的網關,使其直接走vpn流量 ifconfig-pool-persist ipp.txt keepalive 10 120 comp-lzo persist-key persist-tun status openvpn-status.log verb 3 plugin /usr/lib64/openvpn/plugin/lib/openvpn-auth-ldap.so "/etc/openvpn/auth/ldap.conf" client-cert-not-required username-as-common-name log /var/log/openvpn.log # 修改openvpn-ldap-auth的配置文件 `/etc/openvpn/auth/ldap.conf` # /etc/openvpn/auth/ldap.conf <LDAP> # LDAP server URL # 更改爲 AD 服務器的 IP URL ldap://172.16.76.238:389 # Bind DN (If your LDAP server doesn't support anonymous binds) # BindDN uid=Manager,ou=People,dc=example,dc=com # 更改爲域管理的 DN, 可以通過 ldapsearch 進行查詢 # -h 的 ip 替換爲服務器 ip,-D 換爲管理員的 dn,-b 爲基礎的查詢 dn,* 爲所有 # ldapsearch -LLL -x -h 172.16.76.238 -D "administrator@xx.com" -W -b "dc=xx,dc=com" "*" BindDN "cn=administrator,cn=Users,dc=xx,dc=com" # Bind Password # Password SecretPassword # 域管理員的密碼 Password passwd # Network timeout (in seconds) Timeout 15 # Enable Start TLS TLSEnable no # Follow LDAP Referrals (anonymously) FollowReferrals no # TLS CA Certificate File # TLSCACertFile /usr/local/etc/ssl/ca.pem # TLS CA Certificate Directory # TLSCACertDir /etc/ssl/certs # Client Certificate and key # If TLS client authentication is required # TLSCertFile /usr/local/etc/ssl/client-cert.pem # TLSKeyFile /usr/local/etc/ssl/client-key.pem # Cipher Suite # The defaults are usually fine here # TLSCipherSuite ALL:!ADH:@STRENGTH </LDAP> <Authorization> # Base DN # 查詢認證的基礎 dn BaseDN "dc=boqii-inc,dc=com" # User Search Filter # SearchFilter "(&(uid=%u)(accountStatus=active))" # 其中 sAMAccountName=%u 的意思是把 sAMAccountName 的字段取值爲用戶名, # 後面 "memberof=CN=myvpn,DC=xx,DC=com" 指向要認證的 vpn 用戶組, # 這樣任何用戶使用 vpn,只要加入這個組就好了 SearchFilter "(&(sAMAccountName=%u)(memberof=CN=myvpn,DC=boqii-inc,DC=com))" # Require Group Membership RequireGroup false # Add non-group members to a PF table (disabled) # PFTable ips_vpn_users <Group> # BaseDN "ou=Groups,dc=example,dc=com" # SearchFilter "(|(cn=developers)(cn=artists))" # MemberAttribute uniqueMember # Add group members to a PF table (disabled) # PFTable ips_vpn_eng BaseDN "ou=vpn,dc=boqii-inc,dc=com" SearchFilter "(cn=openvpn)" MemberAttribute "member" </Group> </Authorization> 拷貝/etc/openvpn/key目錄下的ca.crt證書,以備客戶端使用。

Hero Image
SSH 证书登录教程

SSH 证书登录教程 证书登录的流程 SSH 证书登录之前,如果还没有证书,需要生成证书。具体方法是: 用户和服务器都将自己的公钥,发给 CA CA 使用服务器公钥,生成服务器证书,发给服务器 CA 使用用户的公钥,生成用户证书,发给用户。 有了证书以后,用户就可以登录服务器了。整个过程都是 SSH 自动处理,用户无感知。 用户登录服务器时,SSH 自动将用户证书发给服务器。 服务器检查用户证书是否有效,以及是否由可信的 CA 颁发。 SSH 自动将服务器证书发给用户。 用户检查服务器证书是否有效,以及是否由信任的 CA 颁发。 双方建立连接,服务器允许用户登录。 生成 CA 的密钥 虽然 CA 可以用同一对密码签发用户证书和服务器证书,但是出于安全性和灵活性,最好用不同的密钥分别签发。所以,CA 至少需要两对密钥,一对是签发用户证书的密钥,假设叫做 user_ca,另一对是签发服务器证书的密钥,假设叫做 host_ca。 # 生成 CA 签发用户证书的密钥 # 会在~/.ssh目录生成一对密钥:user_ca(私钥)和user_ca.pub(公钥) # 各个参数含义如下 # -t rsa:指定密钥算法 RSA。 # -b 4096:指定密钥的位数是4096位。安全性要求不高的场合,这个值可以小一点,但是不应小于1024。 # -f ~/.ssh/user_ca:指定生成密钥的位置和文件名。 # -C user_ca:指定密钥的识别字符串,相当于注释,可以随意设置。 $ ssh-keygen -t rsa -b 4096 -f ~/.ssh/user_ca -C user_ca # 生成 CA 签发服务器证书的密钥 # 会在~/.ssh目录生成一对密钥:host_ca(私钥)和host_ca.pub(公钥) # 现在,~/.ssh目录应该至少有四把密钥。 # - ~/.ssh/user_ca # - ~/.ssh/user_ca.pub # - ~/.ssh/host_ca # - ~/.ssh/host_ca.pub $ ssh-keygen -t rsa -b 4096 -f host_ca -C host_ca 服务器安装 CA 公钥 # 为了让服务器信任用户证书,必须将 CA 签发用户证书的公钥`user_ca.pub`,拷贝到服务器 $ scp ~/.ssh/user_ca.pub root@host.example.com:/etc/ssh/ 然后,将下面一行添加到服务器配置文件 /etc/ssh/sshd_config TrustedUserCAKeys /etc/ssh/user_ca.pub 上面的做法是将user_ca.pub加到/etc/ssh/sshd_config,这会产生全局效果,即服务器的所有账户都会信任user_ca签发的所有用户证书。

Hero Image
用 iptables 和 ip rule 做负载均衡

用 iptables 和 ip rule 做负载均衡 操作 这里以一台通过有线 + 无线出口连接到互联网的 Arch Linux 设备为例。其中共有两个出口,分别使用网卡 eth0 和 eth1。大概的对应关系是: 标记 10 (0xa) - 路由表 #110 - 使用 eth0 出口 标记 11 (0xb) - 路由表 #111 - 使用 eth1 出口 我们会根据数据包上包含的标记值来判断它应该走什么出口。首先,使用 ip rule 为每个标记值指定一张使用的路由表。 通常默认路由表的权重是 32768。为了让我们的路由表用得上,我们需要把它们的权重调得高一些(例如 31000)。 # 让带标记 10 (0xa) 的数据包使用 110 号路由表,权重 31000 ip rule add fwmark 10 table 110 prio 31000 # 让带标记 11 (0xb) 的数据包使用 111 号路由表,权重 31000 ip rule add fwmark 11 table 111 prio 31000 # 如果你的连接更多,可以继续添加标记 <-> 路由表的对应关系 # #110 路由表的路由 ip route add 10.20.0.0/24 dev eth0 table 110 ip route add default via 10.20.0.254 table 110 # #111 路由表的路由 ip route add 10.25.0.0/24 dev eth1 table 111 ip route add default via 10.25.0.254 table 111 # 如果这条连接已经被标记,那么把标记设置到数据包上 iptables -t mangle -A OUTPUT -j CONNMARK --restore-mark # 如果数据包已经有标记,直接放行 iptables -t mangle -A OUTPUT -m mark ! --mark 0 -j ACCEPT # 如果数据包没被标记的话 # 把数据包包的标记设置为 11 (0xb)... iptables -t mangle -A OUTPUT -j MARK --set-mark 10 # 并且每 2 个包就把一个包的标记设置为 10 (0xa) iptables -t mangle -A OUTPUT -m statistic --mode nth --every 2 --packet 0 -j MARK --set-mark 11 # 如果你有三条出口的话,这里可以类似于 # iptables -t mangle -A OUTPUT -j MARK --set-mark 10 # iptables -t mangle -A OUTPUT -m statistic --mode nth --every 3 --packet 0 -j MARK --set-mark 11 # iptables -t mangle -A OUTPUT -m statistic --mode nth --every 3 --packet 1 -j MARK --set-mark 12 # 把数据包的标记存储到整条连接上,这样整个连接过程都会使用同一条出口 iptables -t mangle -A OUTPUT -j CONNMARK --save-mark # 我们还需要让数据包上标示的出口是我们为其选择的出口 iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE 之后可以用 iptables -L OUTPUT -t mangle 看一下自己所设置的规则是否正确。然后就可以用 Wiresharks 看一看自己的连接是不是真的分流啦。