Ubuntu NTP 校時

timedatectl 時間管理工具

  • 顯示目前的設定狀態
    1
    2
    # 顯示目前狀態
    timedatectl
1
2
3
4
5
6
7
               Local time: 三 2023-04-26 11:00:22 CST
Universal time: 三 2023-04-26 03:00:22 UTC
RTC time: 三 2023-04-26 03:00:22
Time zone: Asia/Taipei (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
  • 啟動網路校時
    輸入下面指令後,就會打開網路校時功能

    1
    2
    # 啟用 NTP 校時
    timedatectl set-ntp yes

    Ubuntu使用的是systemd-timesyncd,因此等一下要設定systemd-timesyncd

  • 檢查systemd-timesyncd服務狀態

    1
    2
    # 檢查 systemd-timesyncd 服務狀態
    systemctl status systemd-timesyncd
1
2
3
4
5
6
7
8
9
10
● systemd-timesyncd.service - Network Time Synchronization
Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-04-26 10:58:34 CST; 1min 19s ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 221565 (systemd-timesyn)
Status: "Synchronized to time server 91.189.89.198:123 (ntp.ubuntu.com)."
Tasks: 2 (limit: 38317)
Memory: 1.2M
CGroup: /system.slice/systemd-timesyncd.service
└─221565 /lib/systemd/systemd-timesyncd
  • 設定校時伺服器
    要設定校時伺服器可以用root權限編輯以下檔案/etc/systemd/timesyncd.conf
1
2
3
4
5
6
[Time]
# NTP 伺服器(以空白分隔多個伺服器)
NTP=tw.pool.ntp.org jp.pool.ntp.org

# 備用 NTP 伺服器(以空白分隔多個伺服器)
FallbackNTP=sg.pool.ntp.org ntp.ubuntu.com
  • 重新啟動systemd-timesyncd服務
    重啟服務讓更改生效

    1
    2
    # 重新啟動 systemd-timesyncd 服務
    systemctl restart systemd-timesyncd
  • 檢查一下是不是有跟NTP校時了
    用指令systemctl status systemd-timesyncd看一下目前服務狀態,如果有更新成功會顯示出來

錯誤排除

  • 出現Server has too large root distance. Disconnecting.訊息
    表示機器跟ntp server之間回應的時間太久,因此可以去修改/etc/systemd/timesyncd.conf並加入RootDistanceMaxSec=,通常30秒已經很夠用了
    1
    2
    3
    4
    5
    6
    7
    # See timesyncd.conf(5) for details.
    [Time]
    NTP=10.10.1.30
    #FallbackNTP=
    RootDistanceMaxSec=30
    #PollIntervalMinSec=32
    #PollIntervalMaxSec=2048

timedatectl的NTP service為active的時候查看目前使用的是哪一個NTP服務

  • systemd-timesyncd
    1
    systemctl status systemd-timesyncd.service

ntpd 或 chronyd

1
ps -e | grep ntp
1
ps -e | grep chrony

chronyc說明書

https://chrony-project.org/doc/4.6.1/chronyc.html

詳細說明如下
https://unix.stackexchange.com/a/655489

參考:
https://www.cnblogs.com/pipci/p/12833228.html
https://officeguide.cc/ubuntu-linux-timedatectl-time-synchronization-tutorial/
https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-ubuntu-20-04
https://www.tenable.com/audits/items/CIS_Ubuntu_18.04_LTS_Server_v2.1.0_L1.audit:26286d27c59292cfdb9c7b04593edbed
https://serverfault.com/questions/1024770/ubuntu-20-04-time-sync-problems-and-possibly-incorrect-status-information

FastAPI 印出422 unprocessable entity詳細資料

1
2
3
4
5
6
7
8
9
10
11
12
13
import logging
from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

app = FastAPI()

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
exc_str = f'{exc}'.replace('\n', ' ').replace(' ', ' ')
logging.error(f"{request}: {exc_str}")
content = {'status_code': 10422, 'message': exc_str, 'data': None}
return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)

參考:
https://github.com/tiangolo/fastapi/issues/3361#issuecomment-1002120988

X11 Server顯示遠端GUI

X Window 的Client和Server角色

一般來說如果遠端連線到一台電腦,通常遠端那台電腦是Server,但是對於X Window來說,顯示畫面的才是Server,因此如果你要遠端觀看遠端的電腦畫面,你手上的電腦是X Server,遠端的電腦是X Client

X Server的Port

當你啟動X Server的時候,他會開始監聽6000port,更詳細的來說X Server會給定display number,而每個display number所監聽的port為6000+display number

X11 forwdarding

如果你的電腦在防火牆後,這時候6000port不一定有打開,而X11 forwdarding不只幫打開一條通道讓遠端電腦可以連到你的6000port,同時還幫你處理好X serve連線的事情。

step1. Ubuntu 安裝x11套件

1
sudo apt install x11-apps

step2. 設定遠端電腦開啟x11

修改遠端電腦的/etc/ssh/sshd_config檔案,確認檔案內X11Forwarding yes
enable x11{: w=”350” h=”200” }

step3. 檢查x11設定

用以下指令確認設定正確

1
sudo cat /etc/ssh/sshd_config |grep -i X11Forwarding

在Windows下安裝VcXsrv Windows X Server

在windows下必須安裝x server,可以到以下連結下載
https://sourceforge.net/projects/vcxsrv/

問題排除

  1. Warning: untrusted X11 forwarding setup failed: xauth key data not generated

https://serverfault.com/a/355986

測試

Docker container 透過ssh x11 forwarding傳送影像到遠端電腦

以DeepStream container 為例

1
docker run --gpus all -it --rm --net=host --privileged -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --volume="$HOME/.Xauthority:/root/.Xauthority:rw" -w /opt/nvidia/deepstream/deepstream-6.1 nvcr.io/nvidia/deepstream:6.1.1-devel 

參考:
X window解說
https://www.cs.odu.edu/~zeil/cs252/latest/Public/xtrouble/index.html

What You Need to Know About X11 Forwarding
https://goteleport.com/blog/x11-forwarding/

How to enable X11 forwarding from Red Hat Enterprise Linux (RHEL), Amazon Linux, SUSE Linux, Ubuntu server to support GUI-based installations from Amazon EC2
https://aws.amazon.com/tw/blogs/compute/how-to-enable-x11-forwarding-from-red-hat-enterprise-linux-rhel-amazon-linux-suse-linux-ubuntu-server-to-support-gui-based-installations-from-amazon-ec2/

Built-in SSH X11 forwarding in PowerShell or Windows Command Prompt
https://x410.dev/cookbook/built-in-ssh-x11-forwarding-in-powershell-or-windows-command-prompt/

https://dreamanddead.github.io/post/ssh-x11-forward/

https://juejin.cn/post/7009593663894323231

https://stackoverflow.com/questions/65468655/vs-code-remote-x11-cant-get-display-while-connecting-to-remote-server

https://zhuanlan.zhihu.com/p/461378596

X11 Forwarding on Windows with Cygwin
https://www.csusb.edu/sites/default/files/cse_x11_forwarding_on_windows_with_cygwin.pdf

Cygwin/X
https://x.cygwin.com/

ssh option
https://www.microfocus.com/documentation/rsit-server-client-unix/8-4-0/unix-guide/ssh_options_ap.html

What exactly is X/Xorg/X11?
https://www.reddit.com/r/linuxquestions/comments/3uh9n9/what_exactly_is_xxorgx11/

SSH X11 Forwarding Of Gnome-Boxes Under Wayland & Mixed Wayland & X11 Environments
https://www.dbts-analytics.com/notesxfwdgb.html

  • X11 forwarding docker container裡面的影像
    docker run 指令要多加 –volume=”$HOME/.Xauthority:/root/.Xauthority:rw”

X11 forwarding of a GUI app running in docker
https://stackoverflow.com/a/51209546

Run X application in a Docker container reliably on a server connected via SSH without “–net host”
https://stackoverflow.com/questions/48235040/run-x-application-in-a-docker-container-reliably-on-a-server-connected-via-ssh-w

git 筆記

  • git alias

    1
    git config --global alias.slog "log --graph --all --topo-order --pretty='format:%h %ai %s%d (%an)'"
  • 移動git tag 位置

    1
    2
    git tag --force <tag名稱>
    git push origin <tag名稱> --force #強迫推送到遠端

Python呼叫C++系列(二)scikit-build

第一篇連結

本篇重點

我們將建立一個可安裝的c++ extension套件,可以用pip install 安裝,本篇教學將延續第一篇的範例example.cpp

準備環境

  • 安裝所需套件和scikit-build

  • 安裝編譯所需套件

    1
    sudo apt install build-essential
  • 安裝CMake

    1
    sudo apt install cmake

使用方式

  • 建立一個setup.py檔並且放入下面這行

  • 新增一個CMakeLists.txt並且放入下面內容

  • 新增一個pyproject.toml並且放入下面內容

  • 測試安裝套件
    pip install -v

Debug

Linux crontab 自動化例行性工作

查看例行性工作

1
2
# 查看自己的 crontab
crontab -l

建立例行性工作

1
2
# 編輯 crontab 內容
crontab -e

如果要用高權限執行例行性工作可以加上sudo

1
sudo crontab -e

工作設定的格式

每個例行工作會有時間搭配要下達的指令,*代表所對應的時間只要一改變就執行,例如下面範例每分鐘都會執行一次python3 test.py指令(因為全部是*號)

1
2
*        *           *        *        *            python3 test.py
MIN(分鐘) HOUR(小時) DOM(日) MON(月) DOW(星期幾) 指令(CMD)

指令

1
2
sudo crontab -e
01 14 * * * /home/joe/myscript >> /home/log/myscript.log 2>&1

參考:
https://ithelp.ithome.com.tw/articles/10293218
https://blog.gtwang.org/linux/linux-crontab-cron-job-tutorial-and-examples/
https://askubuntu.com/a/121560

CMake教學系列一使用CMake編譯專案

安裝最新版的CMake,越新越好,因為新版的CMake提供更多的工具幫你尋找函式庫或套件,尤其如果你需要用到CUDA,新版的CMake可以省去你許多的麻煩
{: .prompt-tip }

安裝CMake

Ubuntu可以跟著這篇官方教學安裝最新的CMake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sudo apt-get update
sudo apt-get install ca-certificates gpg wget

test -f /usr/share/doc/kitware-archive-keyring/copyright ||
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null

# Ubuntu 20.04
echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ focal main' | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null
sudo apt-get update

test -f /usr/share/doc/kitware-archive-keyring/copyright ||
sudo rm /usr/share/keyrings/kitware-archive-keyring.gpg

sudo apt-get install kitware-archive-keyring

sudo apt-get install cmake

嘗試用CMake編譯程式

讓我們嘗試用CMake編譯一個開源專案

1
2
git clone https://github.com/CLIUtils/CLI11.git # 下載專案
cd CLI11 #進入資料夾

然後我們就可以開始編譯,過程中你會發現CMake幫你建立了一個build資料夾,第一行指令-S表示source directoryB會幫你建立一個build資料夾如果他不存在的話,CMake產生的東西會全部放在build資料夾裡面,你隨時可以刪除他而且他也不需要被版本控制。第二行的-j讓你可以指定要用多少核心來加速編譯。第三行-t代表執行測試程式,如果你沒有測試程式也可以不需要這行指令。

1
2
3
cmake -S . -B build
cmake --build build -j 8
cmake --build build -t test

你很容易在網路上看到CMake傳統的編譯方式,不過在CMake 3.15之後你可以用前面提到的新的方法,他可以幫你處理掉一些小麻煩。

1
2
3
4
5
mkdir build
cd build
cmake ..
make
make test

{: .prompt-tip }

查看編譯選項

CLI11資料夾中,你可以用cmake -S . -L列出所有你可以設定的選項,有些是CMake內建的選項,有些是這著專案中設定讓你可以選的選項。cmake -S . -LH不但會列出所有選項,還把說明文字也印出來。在許多開源專案用CMake編譯的時候經常會需要知道他提供哪些選項,這個指令會非常有幫助。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ cmake -S . -B build -LH
-- CMake 3.22.1
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Doxygen not found, building docs has been disabled
-- Configuring done
-- Generating done
-- Build files have been written to: /home/steven/CLI11/build
-- Cache values
// Build the testing tree.
BUILD_TESTING:BOOL=OFF

// Path to a file.
Boost_INCLUDE_DIR:PATH=Boost_INCLUDE_DIR-NOTFOUND

// Turn on boost test (currently may fail with Boost 1.70)
CLI11_BOOST:BOOL=ON

// Build CLI11 examples
CLI11_BUILD_EXAMPLES:BOOL=ON

// Build CLI11 json example
CLI11_BUILD_EXAMPLES_JSON:BOOL=OFF

// Build the tests with NVCC to check for warnings there - requires CMake 3.9+
CLI11_CUDA_TESTS:BOOL=OFF

// Install the CLI11 folder to include during install process
CLI11_INSTALL:BOOL=OFF
...以下省略

如果你想要設定選項值,你可以在CMake指令中用-D加上選項名稱來設定,例如

1
cmake -DCLI11_INSTALL=ON -S . -B build

這時候被設定的值會被保存在build資料夾裡面的CMakeCache.txt。你可以打開這份文件並且找到這個選項被設定的值。你可以看到CLI11_INSTALL:BOOL=ON

1
2
3
4
5
6
7
8
9
10
11
...以上省略
//Build the tests with NVCC to check for warnings there - requires
// CMake 3.9+
CLI11_CUDA_TESTS:BOOL=OFF

//Install the CLI11 folder to include during install process
CLI11_INSTALL:BOOL=ON

//Value Computed by CMake
CLI11_IS_TOP_LEVEL:STATIC=ON
...以下省略

{: file=’CMakeCache.txt’}

如果在設定一次就可以發現他的值被改成OFF

1
cmake -DCLI11_INSTALL=OFF -S . -B build
1
2
3
4
5
6
7
8
9
10
11
...以上省略
//Build the tests with NVCC to check for warnings there - requires
// CMake 3.9+
CLI11_CUDA_TESTS:BOOL=OFF

//Install the CLI11 folder to include during install process
CLI11_INSTALL:BOOL=OFF

//Value Computed by CMake
CLI11_IS_TOP_LEVEL:STATIC=ON
...以下省略

{: file=’CMakeCache.txt’}
另外還有一些常見的CMake標準選項你可以設定

CMake除錯技巧

這節特別重要,如果要自己撰寫CMakeLists.txt話十分有用。--trace-source選項讓你指定你有興趣的檔案,並且依照他執行的順序依序印出他所執行的行數

1
cmake build --trace-source="CMakeLists.txt"

動手寫第一個CMakeLists.txt

我們將製作一個簡單的C語言程式,並且寫一個CMakeLists.txt

1
2
3
mkdir cmakeQuickStart
cd cmakeQuickStart
code .

cmakeQuickStart資料夾內建立一個CMakeLists.txt和一個simple.cpp

  1. cmake_minimum_required指定使用這份CMakeLists.txt最小所需的CMake版本。
  2. project指定專案名稱,而專案使用的語言預設為C++和C。如果要指定專案語言可以輸入LANGUAGES參數。
  3. 最後你要輸出執行檔或是library。add_executable指令輸出執行檔,add_library指令輸出library,輸出的執行檔或是library檔名在跟第一個參數一樣。在這裡特別注意,add_executable``add_library第一個參數為兩個東西命名,首先他命名了輸出檔案的檔案名稱,其次是他命名了一個CMake的targettarget在後面會很常用到。
1
2
3
4
5
6
7
/* simple.c or simple.cpp */
#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

{: file=’simple.cpp’}

1
2
3
4
5
cmake_minimum_required(VERSION 3.15)

project(MyProject)

add_executable(myexample simple.cpp)

{: file=’CMakeLists.txt’}

你也可以把CMakeLists.txt改成下面這樣,他指定了CMake的版本範圍,並且為專案增加說明和版本號

1
2
3
4
5
6
7
8
9
10
11
12
cmake_minimum_required(VERSION 3.15...3.21)

project(MyProject
VERSION
1.0
DESCRIPTION
"Very nice project"
LANGUAGES
CXX
)

add_executable(myexample simple.cpp)

{: file=’CMakeLists.txt’}

接下來你就可以編譯你的程式了,編譯後你會看到build資料夾出現一個myexample檔,跟你在add_executable指定的名稱一樣

1
2
cmake -S . -B build
cmake --build build

More Modern CMake
https://hsf-training.github.io/hsf-training-cmake-webpage/

An Introduction to Modern CMake
https://cliutils.gitlab.io/modern-cmake/