Ubuntu

查看記憶體時脈及世代 Linux or Ubuntu

方法一:

sudo dmidecode -t memory

輸出結果:

# dmidecode 3.1
Getting SMBIOS data from sysfs.
SMBIOS 2.8 present.

Handle 0x0039, DMI type 16, 23 bytes
Physical Memory Array
	Location: System Board Or Motherboard
	Use: System Memory
	Error Correction Type: None
	Maximum Capacity: 64 GB
	Error Information Handle: Not Provided
	Number Of Devices: 4

Handle 0x0047, DMI type 17, 84 bytes
Memory Device
	Array Handle: 0x0039
	Error Information Handle: Not Provided
	Total Width: 64 bits
	Data Width: 64 bits
	Size: 8192 MB
	Form Factor: DIMM
	Set: None
	Locator: ChannelA-DIMM0
	Bank Locator: BANK 0
	Type: DDR4
	Type Detail: Synchronous
	Speed: 2667 MT/s
	Manufacturer: 04CB
	Serial Number: 6EF60200
	Asset Tag: 9876543210
	Part Number: DDR4 2666 2OZ       
	Rank: 1
	Configured Clock Speed: 2667 MT/s
	Minimum Voltage: Unknown
	Maximum Voltage: Unknown
	Configured Voltage: 1.2 V

Handle 0x0048, DMI type 17, 84 bytes
Memory Device
	Array Handle: 0x0039
	Error Information Handle: Not Provided
	Total Width: 64 bits
	Data Width: 64 bits
	Size: 8192 MB
	Form Factor: DIMM
	Set: None
	Locator: ChannelA-DIMM1
	Bank Locator: BANK 1
	Type: DDR4
	Type Detail: Synchronous
	Speed: 2667 MT/s
	Manufacturer: 04CB
	Serial Number: 2CF20200
	Asset Tag: 9876543210
	Part Number: DDR4 2666 2OZ       
	Rank: 1
	Configured Clock Speed: 2667 MT/s
	Minimum Voltage: Unknown
	Maximum Voltage: Unknown
	Configured Voltage: 1.2 V

Handle 0x0049, DMI type 17, 84 bytes
Memory Device
	Array Handle: 0x0039
	Error Information Handle: Not Provided
	Total Width: 64 bits
	Data Width: 64 bits
	Size: 8192 MB
	Form Factor: DIMM
	Set: None
	Locator: ChannelB-DIMM0
	Bank Locator: BANK 2
	Type: DDR4
	Type Detail: Synchronous
	Speed: 2667 MT/s
	Manufacturer: 04CB
	Serial Number: 04F40200
	Asset Tag: 9876543210
	Part Number: DDR4 2666 2OZ       
	Rank: 1
	Configured Clock Speed: 2667 MT/s
	Minimum Voltage: Unknown
	Maximum Voltage: Unknown
	Configured Voltage: 1.2 V

Handle 0x004A, DMI type 17, 84 bytes
Memory Device
	Array Handle: 0x0039
	Error Information Handle: Not Provided
	Total Width: 64 bits
	Data Width: 64 bits
	Size: 8192 MB
	Form Factor: DIMM
	Set: None
	Locator: ChannelB-DIMM1
	Bank Locator: BANK 3
	Type: DDR4
	Type Detail: Synchronous
	Speed: 2667 MT/s
	Manufacturer: 04CB
	Serial Number: DBEF0200
	Asset Tag: 9876543210
	Part Number: DDR4 2666 2OZ       
	Rank: 1
	Configured Clock Speed: 2667 MT/s
	Minimum Voltage: Unknown
	Maximum Voltage: Unknown
	Configured Voltage: 1.2 V

方法二:

sudo lshw -short -C memory

輸出結果:

H/W path               Device          Class          Description
=================================================================
/0/0                                   memory         64KiB BIOS
/0/39                                  memory         32GiB System Memory
/0/39/0                                memory         8GiB DIMM DDR4 Synchronous 2667 MHz (0.4 ns)
/0/39/1                                memory         8GiB DIMM DDR4 Synchronous 2667 MHz (0.4 ns)
/0/39/2                                memory         8GiB DIMM DDR4 Synchronous 2667 MHz (0.4 ns)
/0/39/3                                memory         8GiB DIMM DDR4 Synchronous 2667 MHz (0.4 ns)
/0/43                                  memory         384KiB L1 cache
/0/44                                  memory         1536KiB L2 cache
/0/45                                  memory         9MiB L3 cache
/0/100/14.2                            memory         RAM memory

讓 Nginx 支援 Brotli 壓縮 – 適用 Ubuntu 18.04 及 Ubuntu 20.04

Brotli 是一個基於 LZ77 的無失真壓縮,其壓縮效能及 CPU 使用量上,整體平均來說大勝 gzip,然而 Nginx 官方並沒有支援,但是 Google 出了一個第三方的 module – ngx_brotli,讓 Nginx 可以實現 Brotli 壓縮。

Step 1 – 準備工作

確認 Nginx 版本

nginx -v
nginx version: nginx/1.19.5

安裝相依套件

sudo apt install -y libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev

Step 2 – 下載所需要的原始碼

首先下載 Nginx 原始碼

wget https://nginx.org/download/nginx-1.19.5.tar.gz && tar -zxf nginx-1.19.5.tar.gz && rm  nginx-1.19.5.tar.gz 

下載 ngx_brotli

git clone https://github.com/eustas/ngx_brotli.git && cd ngx_brotli && git submodule update --init

Step 3 – 編譯 ngx_brotli

cd ../nginx-1.19.5/
./configure --with-compat --add-dynamic-module=../ngx_brotli
make modules

更改檔案權限

cd objs/ && chmod 644 *.so

將 modules 複製到 Nginx 的 module 目錄

sudo cp *.so /usr/lib/nginx/modules/

Step 4 – 設定 Nginx

新增 ngx_brotli 的 conf 檔

sudo vim /usr/share/nginx/modules-available/mod-ngx_http_brotli.conf

於檔案內新增下列設定,新增完畢存檔離開。

load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

啟用 ngx_http_brotli 的 module

cd /etc/nginx/modules-enabled
sudo ln -s /usr/share/nginx/modules-available/mod-ngx_http_brotli.conf ./50-mod-ngx_http_brotli.conf
sudo vim /etc/nginx/nginx.conf

在 gzip 段落下面新增下列參數,如下圖:

##
# brotli
##
brotli on;
brotli_comp_level 5;
brotli_static on;
brotli_types text/css text/xml text/javascript application/json application/javascript application/x-javascript application/xml application/xml+rss application/xhtml+xml application/x-font-ttf font/opentype image/svg+xml image/x-icon application/font-woff text/plain;

重新啟動 Nginx

sudo service nginx restart

Step 5 – 驗收

可以看到支援的 MIME Type 都已經 br 壓縮了。

lxc/lxd container 建立與管理及佈署基本指令 – part. 1

首先登入 lxc

lxc exec lxc00 bash

或是

lxc shell lxc00

安裝及設定必備的環境

sudo cp /etc/apt/sources.list sources.list.default
sudo sed -i 's/archive.ubuntu.com/free.nchc.org.tw/g' /etc/apt/sources.list
sudo apt update && sudo apt dist-upgrade
sudo apt install bash-completion net-tools command-not-found byobu openssh-server curl man-db software-properties-common byobu dnsutils ppa-purge iotop sysstat sshfs virt-what htop git unzip rsync apt-transport-https

必備環境安裝說明:

  • 變更 apt server 到離你最近或是頻寬最佳的,可以參考這篇 挑選最佳的 Ubuntu apt mirror
  • 更新 apt db
  • 安裝基本環境軟體
    • bash-completion net-tools command-not-found byobu openssh-server curl man-db software-properties-common byobu dnsutils ppa-purge iotop sysstat sshfs virt-what

設定 auto completion (ubuntu 這個帳號也要一次)

echo "source /etc/profile.d/bash_completion.sh" >> ~/.bashrc
source /etc/profile.d/bash_completion.sh
su -l ubuntu && echo "source /etc/profile.d/bash_completion.sh" >> ~/.bashrc
source /etc/profile.d/bash_completion.sh

修改密碼

sudo passwd
sudo passwd ubuntu

產生金鑰不用密碼登入

ssh-keygen -t ed25519 -f ~/.ssh/192.168.1.2 -C ubuntu@192.168.1.2
ssh-copy-id -p 22 -i ~/.ssh/192.168.1.2.pub ubuntu@192.168.1.2
ssh ubuntu@192.168.1.2

修改 sshd 設定

sudo vim /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes

Ubuntu Desktop 多網卡下使用 Network Manager 變更 route metric

nmcli 變更 metric

sudo nmcli connection modify $connection-name ipv4.route-metric 100
sudo nmcli connection down $connection-name
sudo nmcli connection up $connection-name

參考文章

How to set metric in OS moderated with NetworkManager

nmcli — command-line tool for controlling NetworkManager

2.3. 使用 NETWORKMANAGER 命令行工具 NMCLI

Ubuntu 安裝 MySQL 8.0 (mysql-community-server)

有鑑於 MySQL 8 的效能跟各方面的精進,還有安全上的保護,慢慢將手上的機器都換成 MySQL 8。

首先到 MySQL Community Downloads 挑選 MySQL APT Repository,然後下載 mysql-apt-config_0.8.16-1_all.deb,接下來安裝:

wget https://repo.mysql.com//mysql-apt-config_0.8.16-1_all.deb
dpkg -i mysql-apt-config_0.8.16-1_all.deb

然後更新 apt repository list

sudo apt update

安裝 mysql-community-client mysql-community-server

sudo apt install mysql-community-client mysql-community-server

察看安裝的版本

ubuntu@guestOS:~$ dpkg -l | grep mysql-community
ii  mysql-community-client           8.0.23-1ubuntu20.04                                                amd64        MySQL Client
ii  mysql-community-client-core      8.0.23-1ubuntu20.04                                                amd64        MySQL Client Core Binaries
ii  mysql-community-client-plugins   8.0.23-1ubuntu20.04                                                amd64        MySQL Client plugin
ii  mysql-community-server           8.0.23-1ubuntu20.04                                                amd64        MySQL Server
ii  mysql-community-server-core      8.0.23-1ubuntu20.04                                                amd64        MySQL Server Core Binaires

接下來編輯 /etc/mysqlmysql.conf.d/mysqld.cnf

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

於 [mysqld] 之下,新增下列參數,不知道什麼原因,安裝好之後,只會預設 listen 在 IPv6

bind-address    = 127.0.0.1

重新啟動 MySQL

sudo systemctl restart mysql.service

接下來開始 MySQL 的設定。

sudo mysql_secure_installation

lxc/lxd container 建立與管理及佈署基本指令 – part. 0

查看有哪些 lxc

lxc list

查詢有哪些 image 可以安裝

lxc image list images: | grep -i ubuntu

建立 lxc 的 container

lxc launch images:ubuntu/focal/amd64 focal

啟用 lxc 的 container

lxc start focal

停用(關機) lxc 的 container

lxc stop focal

Container 的 info

lxc info focal

刪除 lxc 的 container

lxc delete focal

Login to your container

lxc exec $my-container bash

lxc 管理基本指令

# 查看有哪些 profile
lxc profile list
# 查看 profile - default 內容
lxc profile show default
# 編輯 profile - default 內容
lxc profile edit default

# 查看有哪些 network 可用
lxc network list
# 查看 br0 這個 network 內容
lxc network show br0

# 查看有哪些 storage
lxc storage list
# 查看 zfspool 這個 storage 內容
lxc storage show zfspool
# 編輯 zfspool 
lxc storage edit zfspool
# 建立 storage
lxc storage create pool1 zfs source=/dev/sdX zfs.pool_name=zfspool

Storage configuration

設定 Ubuntu 下 KVM 的 Bridge Networking

預設的 KVM 下,提供一個類 NAT 方式的 private network bridge,讓 VMs 可以跟外頭溝通,但是無法跟 host 溝通。但畢竟如果要方便的話當然是 guest VMs 可以跟 Host OS 直接透過 bridge 抽取(大量備份),速度跟方便性才會好。

我的環境是一台 Router,然後下面是 host OS(固定 ip),然後 Router 提供 DHCP 或固定 IP 給 guest VMs 使用,這樣網內互打速度才會快,被侷限的只有 Router 或是 Switch 的 backend 而已。以下有圖有真相,host 是 Ubuntu 18.04、guest VM 是 Ubuntu 20.04、網卡 virtio,選擇 e1000 或是 rtl8139 就不可能有這個速度了。

基本套件確認

sudo apt install qemu-system-x86 qemu-utils qemu-efi ovmf libvirt-clients libvirt-daemon-system virtinst bridge-utils

備註:qemu-efi 跟 ovmf 是如果 guest VMs 需要用 EFI 模式安裝(如 Windows 10)才使用。

關閉 bridge 的 netfilter

畢竟是要提昇內部網路的速度及降低 CPU 的使用量,所以把 bridge 的 netfilter 關閉。編輯或新增 /etc/sysctl.d/bridge.conf ,內容下:

net.bridge.bridge-nf-call-iptables=0
net.bridge.bridge-nf-call-ip6tables=0
net.bridge.bridge-nf-call-arptables=0

新增一個 udev 定義檔,告訴 kernel 說這個 bridge 不用 netfilter,定義檔:/etc/udev/rules.d/99-bridge.rules ,內容如下(整行沒斷句):

ACTION=="add", SUBSYSTEM=="module", KERNEL=="br_netfilter", RUN+="/sbin/sysctl -p /etc/sysctl.d/bridge.conf"

砍光原本 KVM 的網路界面

virsh net-destroy default
virsh net-undefine default

或是使用 ip 指令,以下預設安裝好的時候兩個界面分別是 virbr0 跟 virbr0-nic 。

ip link delete virbr0 type brigde
ip link delete virbr0-nic

建立新的 bridge 給 KVM 內的 guest VMs 用

我的環境如下:

編輯或新增 /etc/netplan/00-installer-config.yaml ,內容如下:

network:                                                                                                                        
    ethernets:
        enp4s0:
            dhcp4: false
            dhcp6: false
    bridges:
        br0:
            interfaces: [ enp4s0 ]
            addresses: [192.168.1.1/24]
            gateway4: 192.168.1.254
            mtu: 1500
            nameservers:
                addresses: [127.0.0.1]
            parameters:
                stp: true
                forward-delay: 4
            dhcp4: false
            dhcp6: true
    version: 2

接著使用 netplan 建立起這個 network bridge

sudo netplan apply

告訴 KVM 有這個 br0 可以當 Network Bridge 使用了

首先建立一個 XML 檔案,標注清楚要通知 KVM 哪些資訊,host-bridge.xml 檔案如下:

<network>
  <name>host-bridge</name>
  <forward mode="bridge"/>
  <bridge name="br0"/>
</network>

接著使用 virsh 通知 KVM 啟用這個 Network Bridge

sudo virsh net-define host-bridge.xml
sudo virsh net-start bridge
sudo virsh net-autostart bridge

查詢是否已啟用

j7@hostOS:~$ virsh net-list --all
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 host-bridge               active     yes           y

KVM 內 Guest OS 開啟後查看有哪些網路界面

j7@hostOS:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:0a brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP group default qlen 1000
    link/ether 00:00:00:00:00:0a brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:00:00:00:00:0a brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 brd 192.168.1.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
    inet6 2001:b011:****:****:****:****:****:d3da/64 scope global temporary dynamic 
       valid_lft 598sec preferred_lft 598sec
    inet6 fe80::2d8:61ff:fe2c:d70a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
8: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq master br0 state UNKNOWN group default qlen 1000
    link/ether a00:00:00:0a:0a:0a brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe77:3785/64 scope link 

KVM 使用的 Guest Virtual Interfce

8: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq master br0 state UNKNOWN group default qlen 1000
link/ether a00:00:00:0a:0a:0a brd ff:ff:ff:ff:ff:ff
inet6 fe80::fc54:ff:fe77:3785/64 scope link

收割 virtio-net 威能

ubuntu@guestOS:~$ iperf3 -c 192.168.1.1
Connecting to host 192.168.1.1, port 5201
[  5] local 192.168.1.30 port 60208 connected to 192.168.1.1 port 5201
[ ID] Interval           Transfer     Bitrate         Retr  Cwnd
[  5]   0.00-1.00   sec  5.35 GBytes  46.0 Gbits/sec    0   3.14 MBytes       
[  5]   1.00-2.00   sec  5.38 GBytes  46.3 Gbits/sec    0   3.14 MBytes       
[  5]   2.00-3.00   sec  5.54 GBytes  47.5 Gbits/sec    0   3.14 MBytes       
[  5]   3.00-4.00   sec  5.36 GBytes  46.0 Gbits/sec    0   3.14 MBytes       
[  5]   4.00-5.00   sec  5.49 GBytes  47.1 Gbits/sec    0   3.14 MBytes       
[  5]   5.00-6.00   sec  5.62 GBytes  48.2 Gbits/sec    0   3.14 MBytes       
[  5]   6.00-7.00   sec  5.47 GBytes  47.0 Gbits/sec    0   3.14 MBytes       
[  5]   7.00-8.00   sec  5.29 GBytes  45.4 Gbits/sec    0   3.14 MBytes       
[  5]   8.00-9.00   sec  5.46 GBytes  46.9 Gbits/sec    0   3.14 MBytes       
[  5]   9.00-10.00  sec  5.33 GBytes  45.8 Gbits/sec    0   3.14 MBytes       
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate         Retr
[  5]   0.00-10.00  sec  54.3 GBytes  46.6 Gbits/sec    0             sender
[  5]   0.00-10.00  sec  54.3 GBytes  46.6 Gbits/sec                  receiver

參考文章:

Bridge Networking with KVM on Ubuntu

KVM: Creating a bridged network with NetPlan on Ubuntu bionic
How to Setup Bridge Networking with KVM on Ubuntu 20.04

設定及清除 /var/log/journal

每次看到肥肥的 /var/log/journal 資料夾就會很啊雜,沒事佔用了幾百 MB 的空間

清理 journal 檔

journalctl --vacuum-time 2d
journalctl --vacuum-size 10M

設定 journal 檔的大小

sudo vim /etc/systemd/journald.conf,更改這兩行

SystemMaxUse=10M
SystemMaxFileSize=10M

重新啟動 systemd-journald

sudo systemctl restart systemd-journald.service