Ubuntu

移除沒再使用的 snap 套件

什麼是 snap

參考 wikipedia 的 snap 說明官方網站說明。

snap 的優略就很多,可以自行 Google:// 如,Linux Mint決定不支援Ubuntu Snap

snap 不會自己移除舊版

Ubuntu 用久了之後會有一堆舊版的 snap 套件,如下圖:

當然可以工人智慧手動一個一個移除,但如果 snap 安裝很多的時候勒?!

sudo snap remove google-cloud-sdk – revision 187

使用 shell script 一次移除舊版 snap

shell script 如下:

#!/bin/bash                                                                                                                     
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu

LANG=C snap list – all | awk '/disabled/{print $1, $3}' | while read snapname revision; do sudo snap remove "$snapname" – revision="$revision"
done

第一段會顯示多少 snap 要移除

snap list – all | awk '/disabled/{print $1, $3}'
google-cloud-sdk 187
core18 2066

將 shell script 存檔

vim snap_remove_eol.sh && chmod +x snap_remove_eol.sh

snap purge 用法

sudo ./snap_remove_eol.sh

Ubuntu Linux 關閉 IPv6

現在預設裝好之後,都會啟用 IPv6,立意很好,但是某些服務會一直產生 error message

修改方式

編輯 /etc/sysctl.conf

vim /etc/sysctl.conf

新增下列

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

關閉 GRUB 的 IPv6 載入

Perform the following steps with root privileges to disable IPv6 in Ubuntu 18.04/16.04 Permanently using grub method.

編輯 /etc/default/grub

sudo vim /etc/default/grub

編輯下列兩行為:

GRUB_CMDLINE_LINUX="ipv6.disable=1"
GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1"

更新 grub 設定

update-grub

重新開機

避免 Postfix 出現 IPv6 Error Message

Postfix ipv6 error message

mail postfix/sendmail[79951]: warning: inet_protocols: disabling IPv6 name/address support: Address family not supported by protocol

如何修正

vim /etc/main.cf

#mynetworks = 127.0.0.1 [::1]
mynetworks = 127.0.0.1

How to make Windows VM guest recognize and run on more than 2 cores under KVM

Windows guests do not recognize all of the available cores under KVM and will usually detect only 2 cores. This happens since KVM exposes the available virtual CPU cores as physical CPUs (sockets). So, if the physical host running KVM has 2 CPUs with 4 cores each (a total of 8 cores), and the guest is configured for 8 CPUs, Windows will see 8 physical CPUs and will run only on 2, due to the hard coded limits in some of the editions of Windows Server.

To make Windows use all available cores, we need to configure the guest to expose the CPUs as cores and not as physical CPUs (sockets):

In virt-manager:

  1. Open the guest configuration screen
  2. Select Processor options tab
  3. Expand the “Topology” setting
  4. Set the sockets to 2
  5. Set the cores to 4 (for the guest to have a total of 8 cores) or 3 (for the guest to have a total of 6 cores).
  6. You can also expand the “Configration” settings and click on “copy host CPU configuration” to make the guest fully use all of the physical host’s CPU capabilities.

If you run the guest from command line, the KVM option for setting the CPU topology is: -smp 8,socket=2,cores=4

查看記憶體時脈及世代 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

回到頂端