使用 update-alternatives 命令切换软件版本

update-alternatives 的功能,类似于在桌面系统上设置默认打开程序。

# update-alternatives --help
用法:update-alternatives [<选项> ...] <命令>

命令:
  --install <链接> <名称> <路径> <优先级>
    [--slave <链接> <名称> <路径>] ...
                           在系统中加入一组候选项。
  --remove <名称> <路径>   从 <名称> 替换组中去除 <路径> 项。
  --remove-all <名称>      从替换系统中删除 <名称> 替换组。
  --auto <名称>            将 <名称> 的主链接切换到自动模式。
  --display <名称>         显示关于 <名称> 替换组的信息。
  --query <名称>           机器可读版的 --display <名称>.
  --list <名称>            列出 <名称> 替换组中所有的可用候选项。
  --get-selections         列出主要候选项名称以及它们的状态。
  --set-selections         从标准输入中读入候选项的状态。
  --config <名称>          列出 <名称> 替换组中的可选项,并就使用其中
                           哪一个,征询用户的意见。
  --set <名称> <路径>      将 <路径> 设置为 <名称> 的候选项。
  --all                    对所有可选项一一调用 --config 命令。

<链接> 是指向 /etc/alternatives/<名称> 的符号链接。
    (如 /usr/bin/pager)
<名称> 是该链接替换组的主控名。
    (如 pager)
<路径> 是候选项目标文件的位置。
    (如 /usr/bin/less)
<优先级> 是一个整数,在自动模式下,这个数字越高的选项,其优先级也就越高。

选项:
  --altdir <目录>          改变候选项目录。
                             (默认是 /etc/alternatives)。
  --admindir <目录>        设置 statoverride 文件的目录。
                             (默认是 /var/lib/dpkg/alternatives)。
  --instdir <目录>         改变安装目录。
  --root <目录>            改变文件系统根目录。
  --log <文件>             改变日志文件。
  --force                  允许使用候选项链接替换文件。
  --skip-auto              在自动模式中跳过设置正确候选项的提示
                           (只与 --config 有关)
  --quiet                  安静模式,输出尽可能少的信息。
  --verbose                启用详细输出。
  --debug                  调试输出,信息更多。
  --help                   显示本帮助信息。
  --version                显示版本信息。

注册软件: update-alternatives –install

以jdk为例,安装了jdk以后,先要在update-alternatives工具中注册;

# update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_91/bin/java 200
# update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_111/bin/java 300

其中:

  • 第一个参数:--install表示向 update-alternatives 注册服务名。
  • 第二个参数:是注册最终地址,成功后将会把命令在这个固定的目的地址做真实命令的软链,以后管理就是管理这个软链;
  • 第三个参数:服务名,以后管理时以它为关联依据。
  • 第四个参数:被管理的命令绝对路径。
  • 第五个参数:优先级,数字越大优先级越高。

常用示例

显示程序的可替换信息:update-alternatives –display <名称>

 # 显示PHP程序的可替换信息
update-alternatives --display php

php - manual mode
  link best version is /usr/bin/php8.3
  link currently points to /usr/bin/php8.2
  link php is /usr/bin/php
  slave php.1.gz is /usr/share/man/man1/php.1.gz
/usr/bin/php8.2 - priority 82
  slave php.1.gz: /usr/share/man/man1/php8.2.1.gz
/usr/bin/php8.3 - priority 83
  slave php.1.gz: /usr/share/man/man1/php8.3.1.gz

注册添加程序的可替换信息:update-alternatives –install <名称>

# 设置PHP
update-alternatives --install /usr/bin/php php /usr/local/lsws/lsphp74/bin/php 74
update-alternatives --install /usr/bin/php php /usr/local/lsws/lsphp81/bin/php 81
update-alternatives --install /usr/bin/php php /usr/local/lsws/lsphp82/bin/php 82

列出 程序替换组中所有的可用选项:update-alternatives –list <名称>

update-alternatives --list php

/usr/bin/php8.2
/usr/bin/php8.3 

交互式修改:update-alternatives –config <名称> 显示可用选项的列表,选择对应的索引确认。

#设置默认浏览器
update-alternatives --config www-browser 

There is 1 choice for the alternative www-browser (providing /usr/bin/www-browser).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/w3m     25        auto mode
  1            /usr/bin/w3m     25        manual mode

Press <enter> to keep the current choice[*], or type selection number:

#设置默认使用的php版本
update-alternatives --config php

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/php8.3   83        auto mode
* 1            /usr/bin/php8.2   82        manual mode
  2            /usr/bin/php8.3   83        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

删除替代方案

用 remove 去掉编辑器组中的微替代品:

update-alternatives --remove editor /usr/bin/micro

可以直接使用下面的全部清除:

update-alternatives --remove-all java

解决 apt-get update 从 packages.sury.org 更新 apache2 与 php 时签名无效的错误

apt-get update 显示错误,从 packages.sury.org 更新 apache2 与 php 时签名无效,如下显示:

错误:6 https://packages.sury.org/apache2 bookworm InRelease
  下列签名无效: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key <deb@sury.org>

错误:7 https://packages.sury.org/php bookworm InRelease
  下列签名无效: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key <deb@sury.org>

------------------------------------------------------------------

Err:6 https://packages.sury.org/apache2 bookworm InRelease
The following signatures were invalid: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key deb@sury.org

Err:7 https://packages.sury.org/php bookworm InRelease
The following signatures were invalid: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key deb@sury.org

W: 校验数字签名时出错。此仓库未被更新,所以仍然使用此前的索引文件。GPG 错误:https://packages.sury.org/apache2 bookworm InRelease: 下列签名无效: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key deb@sury.org

W: 校验数字签名时出错。此仓库未被更新,所以仍然使用此前的索引文件。GPG 错误:https://packages.sury.org/php bookworm InRelease: 下列签名无效: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key deb@sury.org

W: 无法下载 https://packages.sury.org/apache2/dists/bookworm/InRelease 下列签名无效: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key deb@sury.org

W: 无法下载 https://packages.sury.org/php/dists/bookworm/InRelease 下列签名无效: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key deb@sury.org

W: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。

————————————————————————————————–

W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://packages.sury.org/apache2 bookworm InRelease: The following signatures were invalid: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key deb@sury.org

W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://packages.sury.org/php bookworm InRelease: The following signatures were invalid: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key deb@sury.org

W: Failed to fetch https://packages.sury.org/apache2/dists/bookworm/InRelease The following signatures were invalid: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key deb@sury.org

W: Failed to fetch https://packages.sury.org/php/dists/bookworm/InRelease The following signatures were invalid: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key deb@sury.org

W: Some index files failed to download. They have been ignored, or old ones used instead.

解决办法:

参考 packages.sury.org 站点 README.txt 文件的脚本。

apache2 https://packages.sury.org/apache2/README.txt
php https://packages.sury.org/php/README.txt

apache2

apt-get update
apt-get -y install lsb-release ca-certificates curl
curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
dpkg -i /tmp/debsuryorg-archive-keyring.deb
sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-apache2.gpg] https://packages.sury.org/apache2/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/apache2.list'
apt-get update

php

apt-get update
apt-get -y install lsb-release ca-certificates curl
curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
dpkg -i /tmp/debsuryorg-archive-keyring.deb
sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
apt-get update

【END】

使用 HttpRepl 浏览、测试 Web API

The HTTP Read-Eval-Print Loop (REPL) 

  • 一种轻量级跨平台命令行工具,在所有支持的 .NET Core 的位置都可得到支持。
  • 用于发出 HTTP 请求以测试 ASP.NET Core Web API(和非 ASP.NET Core web API)并查看其结果。
  • 可以在任何环境下测试托管的 web API,包括 localhost 和 Azure 应用服务。

安装:dotnet tool install -g Microsoft.dotnet-httprepl

(视频)使用 HttpRepl 浏览 Web API:

https://learn.microsoft.com/zh-cn/shows/beginners-series-to-web-apis/exploring-web-apis-with-the-httprepl-16-of-18–beginners-series-to-web-apis

使用 HttpRepl 测试 Web API

https://learn.microsoft.com/zh-cn/aspnet/core/web-api/http-repl/?view=aspnetcore-8.0&tabs=windows

HttpRepl:用于与 RESTful HTTP 服务交互的命令行工具

https://devblogs.microsoft.com/dotnet/httprepl-a-command-line-tool-for-interacting-with-restful-http-services/#configure-visual-studio-for-windows-to-launch-httprepl-on-f5

HttpRepl GitHub 存储库https://github.com/dotnet/HttpRepl

使用Certbot生成Let’s Encrypt证书,为OpenLiteSpeed 配置 SSL

使用 Let’s Encrypt 快速安装 SSL

手动certbot ,单独注册证书,最快获取Let’s Encrypt SSL证书的方式。。。

设置,先安装certbot

sudo apt-get update
sudo apt-get install certbot -y

以非交互式申请证书:

如要申请证书:example.com

certbot certonly --non-interactive --agree-tos -m demo@gmail.com --webroot -w /var/www/html -d example.com

要同时申请证书,并且:example.comwww.example.com

certbot certonly --non-interactive --agree-tos -m demo@gmail.com --webroot -w /var/www/html -d example.com -d www.example.com

OpenLiteSpeed 下的 SSL设置

  • 1. 如果我们只有一个证书,我们可以在侦听器级别设置它。

导航到 OpenLiteSpeed > Web 控制台> 侦听器> SSL > SSL 私钥和证书
设置以下值:

  • 私钥文件/etc/letsencrypt/live/YOUR_DOMAIN/privkey.pem
  • 证书文件/etc/letsencrypt/live/YOUR_DOMAIN/fullchain.pem

单击“保存”,然后执行“平滑重启”。

  • 2. 配置多个 SSL

虚拟主机中的SSL证书将覆盖侦听器,因此我们只需将证书添加到每个域的虚拟主机即可。

导航到 OpenLiteSpeed > Web 控制台>虚拟主机>您的虚拟主机> SSL > SSL 私钥和证书
设置以下值:

  • 私钥文件/etc/letsencrypt/live/YOUR_DOMAIN/privkey.pem
  • 证书文件/etc/letsencrypt/live/YOUR_DOMAIN/fullchain.pem

单击“保存”,然后执行平滑重启”。

OpenLiteSpeed 版本升级

CURRENT VERSION:OpenLiteSpeed 1.7.18   New Release: 1.7.19 (current branch)

登陆管理后台,左上角显示 【当前版本 OpenLiteSpeed 1.7.18 最新版本 1.7.19

使用apt-get upgrade openlitespeed 没有找到新版本,如何更新呢?

下载 lsup.sh:

wget https://raw.githubusercontent.com/litespeedtech/openlitespeed/master/dist/admin/misc/lsup.sh
./lsup.sh

或者 -v 指定版本安装:

/usr/local/lsws/admin/misc/lsup.sh -v 1.7.19

Running ./lsup.sh 将更新之最新版本。

screen后台运行|恢复会话|断开当前会话

screen 控制 ssh 远程会话命令不中断。避免终端窗口关闭/网络断开 后的 进程会话中断。

新建screen

screen -S your_screen_name

Ctrl + a, d :断开当前 screen 会话,但保持会话在后台运行。 Ctrl + a, k :关闭当前窗口或会话。

进入screen

screen -r your_screen_name

Ctrl+D # 在当前screen下,输入Ctrl+D,删除该screen
Ctrl+A,Ctrl+D # 在当前screen下,输入先后Ctrl+A,Ctrl+D,退出该screen

显示screen list

​​​​​​​screen -ls

连接状态为【Attached】的screen

解决恢复会话时出现 There is no screen to be resumed matching 的错误

screen -D -r your_screen_name # 解释:-D -r 先踢掉前一用户,再登陆

判断当前是否在screen中断下,Ubuntu系统,可以这样:

sudo vim /etc/screenrc

文件末尾追加一行即可允许设置screen标题

caption always "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%H %Y/%m/%d "

删除指定screen, your_screen_name为待删除的screen name

​​​​​​​screen -S your_screen_name -X quit

使用service –status-all 或 systemctl status 查询服务状态时卡顿

问题:

在一个运行多年的 debian 系统中,使用 service --status-all 检查服务列表
输出到项目 "[ + ]  cron" 时,会卡顿很久,才继续显示其他服务名。

起初不知道为什么卡顿,今天看到一文,他在 systemctl status 某服务名 时卡顿很久,后来发现这个问题和systemd-journald有关。我感觉这个问题也有可能一样,老系统运行多年,记录的服务信息过多。决定也尝试清理一下journald。

单独查看service --status-all 中卡顿的 cron

systemctl status cron

果真,翻页几千行,才发现看了不到一个月的记录,太多了

cron 是定时执行任务的服务。各种计划任务的执行都会产生大量的服务启动日志。

可以看出 systemctl 输出服务启动时的相关信息。都是从systemd-journald中读取的。

进入文件夹 /var/log/journal,里面有一个长文件名的文件夹:f25c9fe2e9554b54950734c0b92298e0,里面是用户的 system.journal 文件,还有user-1000.journal。

ls -lhm --full-time /var/log/journal/f25c9fe2e9554b54950734c0b92298e0/

查看这个文件夹下文件大小,21,001,486,336 bytes 有19.5 GiB。

下面清理 journal 文件

先停止 systemd-journald 服务

systemctl stop systemd-journald

Warning: Stopping systemd-journald.service, but it can still be activated by:
  systemd-journald-dev-log.socket
  systemd-journald.socket
  systemd-journald-audit.socket

提示警告,为了保险,把这几个都停止掉:
systemctl stop systemd-journald-dev-log.socket
systemctl stop systemd-journald.socket
systemctl stop systemd-journald-audit.socket

然后可以删除 journal 文件(我直接全部删除了,重要的生产服务器不建议这样)
 

最后是要限制systemd-journald日志的大小,避免以后出现这个问题。

编辑文件: /etc/systemd/journald.conf

修改以下两个配置参数:

SystemMaxUse=100M
RuntimeMaxUse=100M

限制全部日志文件加在一起最多可以占用多少空间,相对的问题是,以后的日志记录总量也变少了。

最后重启 systemd-journald

systemctl restart systemd-journald

再次使用 service –status-all 或 systemctl status 查询服务状态时,就不再出现卡顿了。

systemctl daemon-reload #重新加载服务配置文件

[end]

Debian 12 apt update GPG error: NO_PUBKEY

debian 11 版本更新 到 12,update出现两个错误。

,Hit:5 https://packages.microsoft.com/debian/12/prod bookworm InRelease

Err:5 https://packages.microsoft.com/debian/12/prod bookworm InRelease
The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY EB3E94ADBE1229CF

W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://packages.microsoft.com/debian/12/prod bookworm InRelease: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY EB3E94ADBE1229CF

解决办法:

1 下载 https://packages.microsoft.com/config/debian/12/prod.list 替换 /etc/apt/sources.list.d/microsoft-prod.list

2 直接修改 /etc/apt/sources.list.d/microsoft-prod.list 为以下内容:

deb [arch=amd64,arm64,armhf signed-by=/usr/share/keyrings/microsoft-prod.gpg] https://packages.microsoft.com/debian/12/prod bookworm main

Hit:6 https://packages.sury.org/php bookworm InRelease

W: https://packages.sury.org/php/dists/bookworm/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
W: Failed to fetch https://packages.microsoft.com/debian/12/prod/dists/bookworm/InRelease The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY EB3E94ADBE1229CF
W: Some index files failed to download. They have been ignored, or old ones used instead.

解决办法,执行以下命令:

sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

powershell相关

查看别名

  • 使用Get-Command查看Alias的命令: Get-Command -CommandType Alias
  • 查看此Session中已经设定的所有别名:Get-Aliasgal
  • 查看某别名的原命令,如ls的原命令:Get-Alias ls
  • 查看某原命令的别名,如Get-ChildItem的别名:Get-Alias -Definition Get-ChildItem
  • 按属性获取别名:Get-Alias | Where-Object {$_.Options -Match "ReadOnly"}

Windows 中的兼容性别名

PowerShell 具有多个别名,使 UNIX 和 cmd.exe 用户可以在 Windows 中使用熟悉的命令。 下表显示了常用命令、相关的 PowerShell cmdlet 和 PowerShell 别名:

cmd.exe命令UNIX 命令PowerShell CmdletPowerShell 别名
cdchdircdSet-Locationslcdchdir
clsclearClear-Hostcls clear
copycpCopy-Itemcpicpcopy
deleraserdrmdirrmRemove-Itemrideleraserdrmrmdir
dirlsGet-ChildItemgcidirls
echoechoWrite-Outputwrite echo
mdmkdirNew-Itemni
movemvMove-Itemmimovemi
popdpopdPop-Locationpopd
pwdGet-Locationglpwd
pushdpushdPush-Locationpushd
renmvRename-Itemrniren
typecatGet-Contentgccattype

 备注

此表中的别名特定于 Windows。 某些别名在其他平台上不可用。 这是为了使本机命令能够在 PowerShell 会话中工作。 例如,未在 macOS 或 Linux 上将 ls 定义为 PowerShell 别名,以便运行本机命令而不是 Get-ChildItem

Windows 下GUI编辑工具 PowerShell ISE

单击“开始”,选择“Windows PowerShell”,然后单击“Windows PowerShell ISE”。 或者,可以在任何命令外壳或“运行”框中键入内容。powershell_ise.exe

Linux下安装 powershell

如果你已安装 .NET Core SDK,则可以轻松地安装 PowerShell 作为 .NET 全局工具

sh复制

dotnet tool install --global PowerShell

dotnet 工具安装程序将 ~/.dotnet/tools 添加到 PATH 环境变量中。 但是,当前运行的 shell 没有更新的 PATH。 应该可以通过键入 pwsh 从新 shell 启动 PowerShell。

systemctl is-enabled 检查服务是否开启自启动

systemctl 命令之后有很多选项和命令。罗列几个常用的命令。

#service --status-all

#列出全部服务
systemctl list-units --type=service

#查看一个服务的状态,列出有没有正在执行、开机时是否启动等信息。
systemctl status 服务名.service

#开启服务自启动,下次开机服务启动
systemctl enable 服务名.service

#禁用服务自启动,下次开机时,服务不会被启动
systemctl disable 服务名.service

#检查一个服务是否正在运行
systemctl is-active 服务名.service

#检查一个服务的设置,是否为自启动的
systemctl is-enabled 服务名.service


systemctl [command] [unit]

systemctl start  #立刻启动后面接的 unit。
systemctl stop  #立刻关闭后面接的 unit。
systemctl restart  #重启动服务,先关闭再启动的意思。
systemctl reload  #不关闭 unit 的情况下,重新载入配置文件,让设置生效。
systemctl kill  #杀死一个服务的所有子进程。
systemctl show  #列出 unit 的配置。
systemctl mask  #注销 unit,注销后你就无法启动这个 unit 了。
systemctl unmask  #取消对 unit 的注销。

systemctl daemon-reload #重新加载服务配置文件