2021-09-11

安裝code-server

 code-server,也就是讓你可以自架雲端版的VS Code(或開源的VSCodium),可以像GitPod(基於Eclipse Theia)一樣用瀏覽器操作遠端vs code介面。

安裝可分三種方式(Linux based):

一、直接下載script安裝(自動安裝deb或rpm):curl -fsSL https://code-server.dev/install.sh | sh

二、手動下載deb或rpm安裝

三、安裝到雲端供應商

安裝上非常簡單,安裝完後,先執行code-server跑跑看,會出現連線網址及port(預設8080),按ctrl+c停止。

打code-server --help會顯示一些可用參數,若不想用參數可修改設定檔,設定檔用的參數名稱與執行命令的參數名稱都是一樣的,設定檔位置在 ~/.config/code-server/config.yaml,以下是範例(埠號10000,使用密碼及憑證):

bind-addr: 0.0.0.0:10000
auth: password
password: mypassword
cert: /path/to/your/fullchain.pem
cert-key: /path/to/your/privkey.pem

若設為 cert: false 會使用自簽憑證,若有正式憑證請把cert及cert-key檔案路徑打上去,
建議憑證一定要設,不然複製貼上等一些功能可能會有問題,免費憑證可以使用letsencrypt,參照cerbot官網並選擇平台照建議安裝即可。

還有一個問題就是預設的plugin maketplace並不是微軟的marketplace,而是open vsx,這是因為使用條款限制的關係而不能直接使用,若想切換成微軟的marketplace,可以新增環境變數,範例script如下(.sh):

#!/bin/bash
SERVICE_URL=https://marketplace.visualstudio.com/_apis/public/gallery ITEM_URL=https://marketplace.visualstudio.com/items CACHE_URL=https://vscode.blob.core.windows.net/gallery/index code-server > /dev/null 2>&1 &  echo "$!"

用以上script執行後會顯示執行的pid並背景執行,想要關閉程式的話,可直接kill -9 <pid>,若沒記下pid而清掉螢幕,就改用 ps -u username Sf | grep code-server 找出/usr/lib/code-server/lib/node /usr/lib/code-server...執行樹最上層的process pid,並用kill -15 <pid>停止程序。

若想用systemd的方式開機自動以某user身份執行的話,可用
sudo systemctl enable --now code-server@username

記得要更換extension marketplace的話,把以下變數設定加到
/lib/systemd/system/code-server@.service
的 [Service] 裡:
Environment=SERVICE_URL=https://marketplace.visualstudio.com/_apis/public/gallery Environment=ITEM_URL=https://marketplace.visualstudio.com/items Environment=CACHE_URL=https://vscode.blob.core.windows.net/gallery/index 

執行code-server加執行參數時,可寫在
~/.local/share/code-server/argv.json 或 ~/.config/code-server/config.yaml
但像要加上 enable-proposed-api 時,寫在config.yam只能指定單個extension,用list形式指定的話無法生效(bug?),只好改寫在 argv.json,譬如:
{"enable-proposed-api": ["genuitecllc.codetogether", "ms-toolsai.jupyter"]}

ref:
https://coder.com/docs/code-server/v3.12.0/collaboration#installing-the-codetogether-extension
https://code.visualstudio.com/api/advanced-topics/using-proposed-api#sharing-extensions-using-the-proposed-api
https://coder.com/docs/code-server/v3.12.0/FAQ#how-can-i-reuse-my-vs-code-configuration


2021-09-09

vscode的鳥bug,跑python預設都只會在powershell跑

在windows上,不管怎麼修改terminal default profile為command prompt(cmd),只要點右上的執行按鈕,就是只會用powershell執行python,而且還無法正常載入conda env環境(還是linux好)。

解決方式:

一、先用管理員進powershell執行 set-executionpolicy remotesigned

二、再到vscode建立python的launch.json,加入如下(參考開始選單中conda呼叫poweshell的捷徑參數修改):

"configurations": [

    {

        "name": "Python: 目前檔案",

        "type": "python",

        "request": "launch",

        "program": "${file}",

        "console": "integratedTerminal",

        "args": [

            "-ExecutionPolicy",

            "ByPass" ,

            "-NoExit",

            "-Command",

            "& 'C:\\Users\\User\\miniconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\User\\miniconda3' "

        ]

    }

]

 ref:
https://stackoverflow.com/questions/54828713/working-with-anaconda-in-visual-studio-code
https://stackoverflow.com/questions/56450606/how-to-add-anaconda-powershell-to-vscode/61879466#61879466
https://docs.microsoft.com/zh-tw/powershell/scripting/dev-cross-plat/vscode/using-vscode?view=powershell-7.1#workspace-debugging

2021-09-08

安裝jetbrains projector (Server Side)

 1.先依照github頁面步驟安裝projector
https://github.com/JetBrains/projector-installer#Installation

2.幫機器設定一個domain,並記得修改/etc/hostname

3.幫domain上letsencrypt憑證(不需要裝http server,因為我們需要的只是憑證而已),對外的80 port防火牆記得開。譬如OS是Ubuntu 20.04就照著選,伺服器選「None of the above」(cerbot已自帶http伺服器可自動認證)
https://certbot.eff.org/lets-encrypt/ubuntufocal-other

且certbot會產生systemd service及timer以後會自動更新憑證:
/etc/systemd/system/snap.cerbot.renew.service
/etc/systemd/system/snap.cerbot.renew.timer

4.安裝ide: projector install,再照著數字選擇你要安裝的ide跟版本,可以裝好幾個ide,所有ide安裝好後設定檔會放在 ~/.projector/configs/<config_name>
安裝時建議channel選tested,表示是有測試過能正常在projector運行的ide,否則裝非tested的有時會有些奇奇怪怪的問題。

5.設定ide:projector config edit,再照著數字選擇個別ide設定檔並設定(如ide要開在哪個port、要不要設token...等), 其中有一項會問你要不要設定ssl,選是的話會先使用jetbrains的自簽憑證。注意設定完port後也要記得開防火牆

6.幫projector裝上letsencrypt憑證,記住每裝一個ide就要重複一次上憑證的動作:
projector install-certificate <config_name> --certificate /etc/letsencrypt/live/your.domain.com.tw/cert.pem --key /etc/letsencrypt/live/your.domain.com.tw/privkey.pem

7.執行ide:projector run <config_name>
會出現連線的網址,如果有設定token,網址會帶token參數,把網址ip換成domain即可

8.可用(1)瀏覽器(2)projector client app (3)chrome的projector應用程式
等三種方式連線

9.若不想在server安裝projector, 也可以直接在本機的jetbrains ide裝projector plugin後成為本機伺服器,但每次有人從外部連線時需要點擊確認才能連線。

10.若多人共用同一台伺服器,建議修改 /et/clogin.defs 的umask值為 027,這樣使用者(ide)之後新建的檔案權限other(別人)就會是不可讀不可寫。

11.每次離開(exit)IDE, 那麼就等同於projector停止,需再project run <config_name>一次才能再啟動並連進ide,若要保持ide及projector運作,就不要在IDE點擊exit,或寫個 systemd 或用watchdog讓projector每次關閉後會再自動重啟。

2021-08-31

發現CPU及HD溫度太高,用cpulimit降低吃cpu程式的頻率,hdparm讓硬碟待命(standby, sleep)

最近發現在linux上用wine跑LINE非常吃CPU(近期發生,可能是這個原因,發生在wine6.8+以上,後來換回wine6.5就沒問題了),CPU溫度飆到快90度,於是用cpulimit降低wineserver這支程式的cpu頻率,先安裝cpulimit,並寫一支script執行,限制最高cpu最高吃到10%:

#/bin/bash
cpulimit --path /home/username/.PlayOnLinux/wine/linux-x86/6.13/bin/wineserver --limit 10

但有個缺點,打字輸入line變得非常慢還會掉字,開圖跟捲動也很慢,只好要跟人溝通時先關掉cpulimit,沒要輸入文字時再打開。

ref:
https://blog.gtwang.org/linux/limit-cpu-usage-of-a-process-in-linux-with-cpulimit-tool/
http://cpulimit.sourceforge.net/

另新裝了一顆從NSA的退下的3TB機械硬碟,查smart資訊發現溫度異常的高,我也不確定這樣正不正常,怕它提早掛掉,只好想辦法讓它平時待命以降溫。

1.方法一:修改 /etc/hdparm.conf,設定10分鐘後無存取待命,加入:

command_line {
hdparm -S 120 /dev/sdb
}

2.方法二:用systemd,新增或修改/etc/systemd/system/hdparm.service:

[Unit]
Description=hdparm sleep

[Service]
Type=oneshot
ExecStart=/usr/bin/hdparm -q -S 120 -y /dev/sdb

[Install]
WantedBy=multi-user.target

3.方法三:

直接叫出GUI磁碟工具,選擇要設定的硬碟,再按視窗右上角的「磁碟機設定值」,設定進入待命時間:


ref:
https://wiki.archlinux.org/title/hdparm#Putting_a_drive_to_sleep_directly_after_boot
https://askubuntu.com/questions/39760/how-can-i-control-hdd-spin-down-time
http://elmaz.blogspot.com/2009/08/ubuntu-linux-hdd-suspend.html

2021-08-29

Linux Mint的登入視窗(greeter)方向錯誤 & grub解析度(resolution)錯誤

 最近升級了辦公室電腦,把系統移到新電腦後,發現登入視窗的螢幕方向是左旋90度的(但登入後就變正常了),由於一直是使用雙螢幕但之前是沒這個問題的,即使重新設定設定中的「登入視窗」指定螢幕也是沒有用,於是只好直接修改/etc/lightdm/lightdm.conf,加入:

[Seat:*]
display-setup-script=xrandr -o right

強迫再右旋回來。

2022/02/22 updated:
原因是換了GTX 1660顯示卡後,/etc/X11/xorg.conf並末重新產生,導致還是舊的GT 710的設定,
可利用nvidia-xconfig或nvidia-settings重新產生xorg.conf:
https://wiki.archlinux.org/title/NVIDIA#Xorg_configuration

用nvidia-settings的話,點左邊X server Display Configuration,再點右下的Save to X Configuration File,但若是無法成功寫入的話,那麼就自行複製Preview的內容貼到xorg.conf。

GRUB的登入解析度也是變成640x480,因無法先載入nvidia驅動的關係,修改 /etc/default/grub,加入:

GRUB_GFXMODE=1024x768
GRUB_GFXPAYLOAD_LINUX=keep

再存檔執行 sudo update-grub 重開機。
(先執行sudo hwinfo --framebuffer或進grub執行videoinfo確定支援的framebuffer有哪些)

ref:
https://askubuntu.com/questions/408302/rotated-monitor-login-screen-needs-rotation
https://www.binarytides.com/ubuntu-fix-nvidia-graphics/
https://askubuntu.com/questions/54067/how-to-safely-change-grub2-screen-resolution
https://askubuntu.com/questions/217758/how-to-make-an-xorg-conf-file
https://zhuanlan.zhihu.com/p/33983810

2021-08-25

tomcat啟用jstatd及jmx (VisualVM)

因為jmx及jstatd二個用VisualVM連線時,jmx無法使用Visual GC,jstatd無法使用Mbeans(怎麼這麼煩啊),所以二個乾脆都給它同時啟用。


1.新增 /usr/lib/systemd/system/tomcat-jstatd.service:(jstatd預設port開在1099)
#Tomcat jstatd monotor service
[Unit]
Description=jstatd monitor for tomcat
Wants==tomcat.service
After=tomcat.service

[Service]
Type=simple
ExecStartPre=/bin/bash -c "echo -e 'grant { permission java.security.AllPermission; };' > /tmp/jstatd.policy"
ExecStart=/usr/bin/jstatd -p 1099 -J-Djava.security.policy=/tmp/jstatd.policy -J-Djava.rmi.server.hostname=<your_public_ip>
ExecStopPost=/bin/bash -c "rm /tmp/jstatd.policy"
User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target

2.修改  /usr/lib/systemd/system/tomcat.service: (rmi port固定在1100,若不設定的話會隨機; jmx port開在1101,一定要設)

[Service]
...
#JMX form VisualVMEnvironment='JAVA_OPTS=-Djava.rmi.server.hostname=<your_public_ip> -Dcom.sun.management.jmxremote.rmi.port=1101 -Dcom.sun.management.jmxremote.port=1100 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false'


3.防火牆規則記得修改開通1099, 1100, 1101,不過我是直接把自已的網段加入zone=trusted,這樣所有的port都可以連:

sudo firewall-cmd --zone=trusted --add-source=100.101.102.0/24 --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-all --zone=trusted

ref:
https://tomcat.apache.org/tomcat-10.1-doc/monitoring.html#Enabling_JMX_Remote

2021-08-24

升級JDK 1.8至最新版本後,MySQL Connector/J (TOMCAT JDBC)無法正常連線MySQL

升級JDK後,發現MYSQL JDBC無法連到資料庫,查了一下log,發現以下錯誤訊息:

javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)

看來又是TLS無法自動降級使用的問題,先到mysql下指令看一下目前使用的tls版本:

mysql> SHOW GLOBAL VARIABLES LIKE 'tls_version';
+---------------+-----------------------+
| Variable_name | Value                 |
+---------------+-----------------------+
| tls_version   | TLSv1,TLSv1.1      |
+---------------+-----------------------+

1.首先嘗試修改JDBC連線字串,加入
jdbc:mysql://<host>:<port>/<dbname>?enabledTLSProtocols=TLSv1,TLSv1.1(...後略)

然後在tomcat.service的Java啟動選項加入:
‑Djdk.tls.client.protocols="TLSv1,TLSv1.1,TLSv1.2" (但不清楚是不是會影響到tomcat網站的tls 1.3連線)

結果無效。
  
2.直接去jre修改/jre/lib/security/java.security,把以下統統mark起來
#jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, \
#    DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
#    include jdk.disabled.namedCurves

好了,JDBC連線字串跟tomcat.service復原回原來也沒問題。
  
由於試了太多次,還被mysql server block了,用以下指令可以解除:
mysqladmin -h 192.168.1.1 -u root -p flush-hosts

ref: