OpenVPN with Ubuntu 20.04 LTS

shtzeng Post in 系統設定,Tags: , , ,
0

Install Ubuntu 20.04 Server or Desktop

  • Update all packages
    • sudo apt update
    • sudo apt upgrade -y
    • sudo apt dist-upgrade -y
    • sudo apt autoremove
    • sudo shutdown -r now
  • Install easy-rsa for easy setup certifications
    • sudo apt update
    • sudo apt install easy-rsa -y
  • Install OpenVPN
    • sudo apt update
    • sudo apt install openvpn
  • Install OpenVPN with stable version (Option)
    • Ref: https://community.openvpn.net/openvpn/wiki/OpenvpnSoftwareRepos
    • wget -O – https://swupdate.openvpn.net/repos/repo-public.gpg | sudo apt-key add –
    • echo “deb http://build.openvpn.net/debian/openvpn/statble focal main” > /etc/apt/sources.list.d/openvpn-aptrepo.list
    • sudo apt update
    • sudo apt install openvpn
  • Config OpenVPN Server
    • setup certifications using easyrsa for OpenVPN
      • but you can still use openssl command … XD
      • Ref: https://community.openvpn.net/openvpn/wiki/EasyRSA3-OpenVPN-Howto
      • go to /etc/openvpn/ and run `sudo make-cadir easy-rsa`
      • go to easy-rsa dir, you must change to root account or change this dir permission for entry
      • edit vars file, or left default value
      • build require files
        • ./easyrsa init-pki
        • ./easyrsa build-ca
          • it will ask a password for this ca key, please remember it
        • ./easyrsa gen-dh
        • ./easyrsa gen-req openvpn-server nopass
        • ./easyrsa sign-req server openvpn-server
        • ./easyrsa gen-req client-user nopass
        • ./easyrsa sign-req client client-user
        • openvpn –genkey –secret tls-auth.key
      • now, you have files to config OpenVPN Server and Client under /etc/openvpn/ dir
        • easy-rsa/pki/ca.crt
        • easy-rsa/pki/issued/openvpn-server.crt
        • easy-rsa/pki/private/openvpn-server.key
        • easy-rsa/pki/issued/client-user.crt
        • easy-rsa/pki/private/client-user.key
        • easy-rsa/dh.pem
        • tls-auth.key
      • you can copy all of them to /etc/openvpn/ dir for less editing
    • setup OpenVPN config file
      • Ref: https://openvpn.net/community-resources/how-to/
      • back to /etc/openvpn/ dir
      • copy example file for less editing :)
        • cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz ./
        • gunzip server.conf.gz
      • minimal modify server.conf, this section just use ssl certification to auth, if you want user / password for auth, see next part
        • push “redirect-gateway def1 bypass-dhcp”
          • uncomment it for route all traffic to vpn server
        • push “dhcp-option DNS 8.8.8.8”
        • ca ca.crt
        • cert openvpn-server.crt
        • key openvpn-server.key
        • dh dh.pem
        • tls-auth tls-auth.key 0
        • duplicate-cn
        • log-append /var/log/openvpn/openvpn.log
      • add following for user / password auth
        • Ref: https://openvpn.net/community-resources/how-to/#auth
        • use simple script
          • auth-user-pass-verify auth-user.sh via-env
          • script-security 3
          • username-as-common-name
        • use pam
          • auth-user-pass-verify auth-pam.pl via-file
            • auth-pam.pl at /usr/share/doc/openvpn/examples/sample-scripts/auth-pam.pl
            • sudo cp /usr/share/doc/openvpn/examples/sample-scripts/auth-pam.pl /etc/openvpn/
          • script-security 3
          • username-as-common-name
      • now OpenVPN Server configuration completed!
  • Config OpenVPN Client
    • go to /etc/openvpn/
    • copy example files for less editing :)
      • cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ./
    • mininal modify client.conf
      • Since we want just one config file, remove
        • ca ca.crt
        • cert client.crt
        • key client.key
        • tls-auth ta.key 1
      • Add or Modify
        • remote your-ip/your-fqdn 1194

        • —–BEGIN CERTIFICATE—–
          put your ca.crt content here
          —–END CERTIFICATE—–

        • —–BEGIN CERTIFICATE—–
          put your client-user.crt content here
          —–END CERTIFICATE—–

        • —–BEGIN PRIVATE KEY—–
          put your client-user.key content here
          —–END PRIVATE KEY—–

        • —–BEGIN OpenVPN Static key V1—–
          put you tls-auth.key content here
          —–END OpenVPN Static key V1—–
        • key-direction 1
    • Add for user / password auth
      • auth-user-pass
  • Start OpenVPN
    • check for OpenVPN ‘server.conf’ start at boot
      • sudo systemctl enable openvpn@server
    • start OpenVPN ‘server.conf’
      • sudo systemctl start openvpn@server
  • Ensure NAT traffic route correctly
    • put ip forward for NAT
      • add file /etc/sysctl.d/30-openvpn.conf
        • net.ipv4.ip_forward = 1
      • reload sysctl
        • sudo sysctl -p /etc/sysctl.d/30-openvpn.conf
    • setup iptable for NAT
      • sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -o ens5 -j MASQUERADE
    • save iptable for boot
      • sudo apt-get install iptables-persistent -y
      • sudo iptables-save > /etc/iptables/rules.v4
  • Test for Connect
    • open browser with https://ipinfo.io to check the ip you change to

ubuntu munin network traffic no data

shtzeng Post in 系統設定
0

Since old Ubuntu LTS doesn’t update package anymore,
some patches need add by yourself,
like munin if_ error.

: plugins/node.d.linux/if_

    # iwlist first)
    if [[ -r /sys/class/net/$INTERFACE/speed ]]; then
            SPEED=$(cat /sys/class/net/$INTERFACE/speed 2>/dev/null)
-           if [[ "$SPEED" -gt 0 ]]; then
+           if [ -n "$SPEED" -a "$SPEED" -gt "0" ]; then
                echo $SPEED
                return
            fi

GCP computer engine ubuntu disk size increase

shtzeng Post in 系統設定
0

Resize the persistent disk in the Google Cloud Platform Console:

1. Go to the Compute Engine page.
2. Click on “Disks” in the sidebar
3. Click the name of the disk that you want to resize.
4. At the top of the disk details page, click “Edit”.
5. In the “Size” field, enter the new size for your disk.
6. At the bottom of the disk details page, click “Save” to apply your changes to the disk.
7. Start server, sudo growpart /dev/sda 1 (if increase root disk)
8. sudo resize2fs /dev/sda1

Ref. https://stackoverflow.com/questions/22381686/how-can-size-of-the-root-disk-in-google-compute-engine-be-increased

Ubuntu 更新 ip 的方法

shtzeng Post in 系統設定,Tags: ,
0

平常裝 server 版本時,
更新 ip 就是去 `/etc/network/interfaces` 更新,
但是裝了 Desktop 版本以後,
發現 `/etc/network/interfaces` 根本是空的,
所以找了一下更新方法。

記得 Desktop 裝好後,會在 Unity 的介面右上角更動網路連線,
所以在 command line 下要改 ip,要去 `/etc/NetworkManager/system-connections/Wired connection X` 更動,
那個 X 只是設定檔的編號,找看看你寫在哪個設定就好~

然後呢,Ubuntu 12.04 更改完儲存馬上就會偵測到然後換 IP 了,ssh 就卡住了 WTF。
而 Ubuntu 16.04 只會在你 stop / start 後才變更,這樣才人性嘛~

記下來,反正下次還是會忘記

date 轉換時間格式在 ubuntu 及 macOS 的不同

shtzeng Post in 系統設定,Tags: , ,
0

常常用又常常碰壁 = =凸
寫一寫記錄下來

像是 Feb 26 23:59:59 2022 GMT 這串要轉成 unix time stamp 的時候
在 ubuntu 下可以直接用 date --date="Feb 26 23:59:59 2022 GMT" +%s 來轉
在 mac 下就得用 LANG=en_us_8859_1 date -j -f "%b %d %T %Y %Z" "Feb 26 23:59:59 2022 GMT" +%s
對的,因為我 mac 語系是 zh_TW.UTF-8,用來顯示時間的格式不同 zzz
shtzeng@shtzengde-MacBook-Air [~] [16:54:42] $ date "+%b %d %T %Y %Z"
1 31 16:55:44 2020 CST
shtzeng@shtzengde-MacBook-Air [~] [16:55:44] $

所以要帶正確的 LANG 環境變數進去使用

在 AWS 管理 SSL 憑證

shtzeng Post in 系統設定,Tags: , , , ,
0

以前就覺得 AWS 上要管理 SSL 憑證沒有一個好的介面
這幾天要上傳一個更新的憑證,就覺得 …. sucks
太多產品又沒好好整合

最一開始的是 IAM 管理憑證
我記得最初接觸到的點是 EC2 Load Balancer 上傳憑證
上傳後就找不到介面管理了
對,他就是一直都沒開發網頁介面給你,只能用 aws cli 去處理

shtzeng@jump [~] [03:43:16] $ aws iam list-server-certificates
{
"ServerCertificateMetadataList": [
{
"ServerCertificateId": "XXX",
"ServerCertificateName": "CCC",
"Expiration": "2020-02-27T23:59:59Z",
"Path": "/",
"Arn": "arn:aws:iam::QQQ:server-certificate/CCC",
"UploadDate": "2019-01-29T07:17:38Z"
},
{
"ServerCertificateId": "XXX",
"ServerCertificateName": "CCC",
"Expiration": "2021-10-24T23:59:59Z",
"Path": "/",
"Arn": "arn:aws:iam::QQQ:server-certificate/CCC",
"UploadDate": "2019-09-26T04:33:28Z"
},
{
"ServerCertificateId": "XXX",
"ServerCertificateName": "CCC",
"Expiration": "2022-02-26T23:59:59Z",
"Path": "/",
"Arn": "arn:aws:iam::QQQ:server-certificate/CCC",
"UploadDate": "2020-01-30T10:22:15Z"
}
]
}
shtzeng@jump [~] [03:43:19] $

而相關的指令有

shtzeng@jump [~] [03:45:24] $ aws iam help | grep server-certificate
o delete-server-certificate
o get-server-certificate
o list-server-certificates
o update-server-certificate
o upload-server-certificate
shtzeng@jump [~] [03:45:26] $

大概查一下 help 就能了解怎麼整理了,這大概就是第一代憑證管理

再來就是 ACM 了
這就有介面可以用了
但是…但是….
像是 Load Balancer 裡面要用的,那個區 ACM 就要放對應的憑證
婀對,像是我在 ap-northeast-1 要用到,ACM 的 ap-northeast-1 就要放一組
如果 us-west-2 也要用,那 us-west-2 那邊也得放
然後
api-gateway 如果自訂網址去用憑證,他只會抓 us-east-1 的資料來用 (不支援 IAM 就是了)
上面這個是走 cloudfront 體系的,我猜 cloudfront 應該也是只支援 us-east-1 的憑證了 zzz

就是很麻煩啦 \_/
不過 ACM 可以請 AWS 幫你產生憑證,還會自己 renew,這是很不錯的服務就是了 =w=

感謝看我的廢話 xd

在大陸使用 Ubuntu 安裝套件的加速技巧

shtzeng Post in Research,Tags: , , , ,
0

在大陸使用 Ubuntu 安裝套件時,
總是會覺得很慢,
慢一下還好,持續的慢就會想殺人了 (遞刀子
不查還好,一查不得了,
cn.archive.ubuntu.com 居然 CNAME 到 us,

;; ANSWER SECTION:
cn.archive.ubuntu.com. 600 IN CNAME us.archive.ubuntu.com.
us.archive.ubuntu.com. 60 IN A 91.189.91.24
us.archive.ubuntu.com. 60 IN A 91.189.91.23
us.archive.ubuntu.com. 60 IN A 91.189.91.26
us.archive.ubuntu.com. 60 IN A 91.189.91.14

然後大陸到 canonical 的 routing 其實也不是很好,
到底~到底~是哪個王八蛋,難道都沒有好用的機器?
網路上找了一下,清華大學開源軟體鏡像站維護的不錯,
這時候去修改 /etc/apt/source.list 把網址都換成 mirror.tuna.tsinghua.edu.cn 就可以了,
跑一次 apt update 把本機的資料庫更新一下,
下載安裝就可以享受到飛快的速度了 ~~~


在處理 python 套件安裝時也一樣悲劇,
這時候就加上 “-i https://pypi.tuna.tsinghua.edu.cn/simple” 參數來加速
像是這樣

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple m3u8

速度馬上就飛起來了

幹,是中國網路太差好嗎 >_<

在 Ubuntu 14.04 更新 jenkins

shtzeng Post in Research,Tags: , ,
0

軟體要定期更新
才能保持沒有bug
雖然說也常常用最新版就壞掉了XD
這邊來說在 Ubuntu 14.04 上面更新 jenkins 的問題
jenkins 在某個版本之後 從 java7 改到 java8
但是 Ubuntu 14.04 官方不支援 java8
從官方的 apt-get 上怎麼升都弄不上去
所以就要自己想辦法啦

1. 安裝 ppa
sudo add-apt-repository ppa:openjdk-r/ppa
2. 更新啊更新
sudo apt-get update
3. 安裝新的 java8
sudo apt-get install openjdk-8-jre-headless
4. java 改 link
將 /etc/alternatives/java -> /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 改成 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
5. 升級 jenkins
sudo apt-get install jenkins
6. 升級/修正 jenkins 套件
進 jenkins 把能按的該按的都按一按,又是一尾活龍

-完-

轉換 Upstart 到 Systemd

shtzeng Post in Research,Tags: , ,
0

終於有時間寫 blog 了 (汗
應該說最近都在轉換東西,沒有新的技術可以玩

不確定什麼時候,應該是 Ubuntu 16.04 LTS 或附近,決不是 Ubuntu 14.04 LTS 就對了,系統就預設從 Upstart 系統更換到 Systemd 系統
聽說 Systemd 更厲害更快速,但是沒有比較好寫,指令也比較長 T_T
之前會抗拒使用 Ubuntu 16.04 LTS 是因為 Systemd 系統,感覺非常的陌生
不過呢,都是得轉換的,就嘗試著把 Upstart 的設定檔改成 Systemd 的設定檔了

大概簡介一下
最初期的是 SysV
使用的是 /etc/init.d/ 下的設定檔
拜託,很難寫 XD

再來是使用了一陣子的 Upsatrt
設定檔在 /etc/init/ 下
只要將必要的狀況敘述清楚即可
甚至不用自己考慮 pid 之類的
使用上也是很方便,start xxx,stop xxx
對就是這麼無腦

最後就是新的 Systemd
設定檔在 /lib/systemd/system/ 下
在 /etc/system.d/system/ 會有一些檔案,用來描述什麼時候啟動結束之類的,就像 /etc/rcX.d 一樣
使用上呢都得要 systemctl 開頭,什麼 systemctl start xxx,什麼 systemctl stop xxx 的,按 tab 想要打快一點還不行,因為會有 sysctl 很類似會卡住 (棍

算了,抱怨歸抱怨,還是得要用
直接用範例講解比較快
如果原本 Upstart 有一隻設定檔 /etc/init/bigtree.conf

description "BigTree"
author "Big Tree "

start on runlevel [2345]
stop on runlevel [!2345]

respawn
respawn limit 20 5

script
  export NODE_ENV=production
  export PORT=8080
  exec node /tmp/bigtree.js
end script

轉換成 Systemd /lib/systemd/system/bigtree.service

[Unit]
Description=Big Tree

[Service]
Type=simple
Environment="NODE_ENV=production"
Environment="PORT=8080"
ExecStart=/bin/sh -ec "exec node /tmp/bigtree.js"
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=bigtree.target

加上 /etc/systemd/system/bigtree.target

[Install]
WantedBy=multi-user.target

先注意的是,副檔名有改變,Upstart 使用 conf 但 Systemd 使用 service
對照的部分
Unit 對到描述部分
Service 對到 Script 部分,也有包含 respawn 等
Install 掌管 enable 跟 disable 的部分
檔案都正確後,執行 systemctl enable bigtree 就會幫你產生 /etc/systemd/system/bigtree.target.wants/bigtree.service 到 /lib/systemd/system/bigtree.service 的 soft link,這樣就可以開機時啟動

還有其他參數
因為我沒用到,就沒寫了
請參考https://wiki.ubuntu.com/SystemdForUpstartUsers

剩下的,我也不是很熟
剛教~沒讀~

NAT、IPsec 及 iptables 大亂鬥

shtzeng Post in 系統設定,Tags: , , , ,
0

之前在研究 IPsec 的時候
都馬是網頁寫什麼就抄什麼
這個有理也會通就沒特別追究其他問題了
然後也不知道哪裡抄來的 iptables 設定
hmmm….沒問題,但就是哪裡怪怪的
沒問題就先這樣吧

先來看看我遇到的狀況
NAT 跟 IPsec 在同一台
iptables 長這樣 (遮臉)

iptables -t nat -A POSTROUTING -o eth0 -s 10.6.0.0/16 -j MASQUERADE

hmmm…這 VPC 下的機器可以連外
也可以連到其他 VPC
看似沒問題
但是連到其他 VPC 機器為什麼顯示的 ip 是這台 NAT + IPsec 的 ip ?
可是拿掉這段 iptables 設定,VPC 就無法連外了啊~~~~
所以看起來奇怪,還是先放著吧
過了幾個月,最近在弄服務偵測,需要動防火牆讓某些 monitor server ip 可以連過來
可是連不到,發現他是用上面的方式連線的時候,決定要來追一下問題

問題就出在 iptable POSTROUTING 部分寫錯了
NAT server 才需要將 ip 偽裝後連出去,而 ipsec 互連的部分卻不用
因為我的設定錯誤,導致以上的錯誤 T______T

那如果 NAT + IPsec 弄在一起呢
就讓 destination 不是 VPC 的網段被偽裝就好
例如

iptables -t nat -A POSTROUTING -o eth0 ! -d 10.0.0.0/8 -j MASQUERADE

不是通往 10.0.0.0/8 的都被偽裝
hmmm….成功了
可是我們家有很奇怪的兩個網段耶 (該死的 AWS EC2 Classic)
iptables 無法支援 multi destination ,所以寫成 10.0.0.0/8, 172.31.0.0/16 之類的都無法
幸好有鄉民指點迷津,在這邊說了一個方法

iptables -t nat -N NETMAP_dest
iptables -t nat -A NETMAP_dest -d 10.0.0.0/8 -j RETURN
iptables -t nat -A NETMAP_dest -d 172.31.0.0/16 -j RETURN
iptables -t nat -A NETMAP_dest -j MASQUERADE
iptables -t nat -A POSTROUTING -o eth0 -j NETMAP_dest

利用 chain 的方式判斷,是否偽裝 ip
經證實可以使用,實在高明
也讓我的網管能力更上一層樓 (一樓到二樓 ? xd)

多錯多學
才能了解錯在哪