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
在 Ubuntu 22.04|20.04|18.04 安裝 PowerDNS 與 PowerDNS-Admin

在 Ubuntu 22.04|20.04|18.04 安裝 PowerDNS 與 PowerDNS-Admin 使用 Galera 複寫的 PowerDNS 主主架構 https://www.scaleway.com/en/docs/installing-powerdns-server-on-ubuntu-bionic/ 安裝 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 新增 Ubuntu 22.04|20.04|18.04 的 PowerDNS 官方套件庫。

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 # 如果封包沒有被標記 # 把封包標記為 10 (0xa) iptables -t mangle -A OUTPUT -j MARK --set-mark 10 # 每 2 個封包就把一個封包標記為 11 (0xb) 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 看一下規則是否正確,再用 Wireshark 驗證連線是否真的分流。