M$ Win 系统管理笔记

Win 系统使用心得与经验记录。

本地 AD 密码同步到 Azure Ad 的问题

本地修改密码后,应等待 5 分钟到本地 AD 密码同步到 Azure AD 后,再登录激活 Office365。

Wins 安装过程中,激活码的问题

微软有专门的 KMS Client Setup Keys

Win11 22h2 安装过程中跳过 “Let's connect you to a network”

在这个界面,按下 Shift + F10,于命令行中输入 OOBE\BYPASSNRO,之后机器将重启,且 out-of-box eperience, OOBE 将再次启动。

Win 系统下软件安装的几种方式

这里要讨论的,并非习以为常的经由下载安装程序并运行,或是从应用商店 App Store 安全软件。而是一些另类的软件安装方式。

chocolatey.exe 方式

chocolatey.org,提出 Win 系统包管理器的概念,并声称其为 “现代的软件自动化”。实际使用起来,也较为方便。

PS C:\Windows\system32> choco help
Chocolatey v1.1.0
This is a listing of all of the different things you can pass to choco.

DEPRECATION NOTICE

The shims `chocolatey`, `cinst`, `clist`, `cpush`, `cuninst` and `cup` are deprecated.
We recommend updating all scripts to use their full command equivalent as these will be
removed in v2.0.0 of Chocolatey.

Options and Switches

 -v, --version
     Version - Prints out the Chocolatey version. Available in 0.9.9+.

Commands

 * find - searches remote or local packages (alias for search)
 * list - lists remote or local packages
 * search - searches remote or local packages
 * help - displays top level help information for choco
 * info - retrieves package information. Shorthand for choco search pkgname --exact --verbose
 * install - installs packages using configured sources
 * pin - suppress upgrades for a package
 * outdated - retrieves information about packages that are outdated. Similar to upgrade all --noop
 * upgrade - upgrades packages from various sources
 * uninstall - uninstalls a package
 * pack - packages nuspec, scripts, and other Chocolatey package resources into a nupkg file
 * push - pushes a compiled nupkg to a source
 * new - creates template files for creating a new Chocolatey package
 * sources - view and configure default sources (alias for source)
 * source - view and configure default sources
 * config - Retrieve and configure config file settings
 * feature - view and configure choco features
 * features - view and configure choco features (alias for feature)
 * setapikey - retrieves, saves or deletes an apikey for a particular source (alias for apikey)
 * apikey - retrieves, saves or deletes an apikey for a particular source
 * unpackself - re-installs Chocolatey base files
 * export - exports list of currently installed packages
 * template - get information about installed templates
 * templates - get information about installed templates (alias for template)


Please run chocolatey with `choco command -help` for specific help on
 each command.

choco 下使用代理安装软件示例:

choco install -y baidupcs-go --proxy="http://192.168.30.51:3128" --force

MSYS2 方式

MSYS2 亦可经由 choco.exe 加以安装。

msys2.org: MSYS2 是为构建、安装及运行原生 Windows 软件的易于上手环境,而提供的一些工具与库的集合。

其包含了叫做 mintty 的一个命令行终端、bash、诸如 gitsubversion, svn 等版本管理工具,以及像是 tarawk,甚至像 autotools 这样的构建系统,全部都是基于修改版的 Cygwin

MSYS2 终端

在 MSYS2 中添加有空格的路径到 PATH 环境变量时,应采取以下的处理:

export PATH=$PATH:/c/Users/Lenny.Peng/.cargo/bin:/c/Program\ Files/Eclipse\ Adoptium/jre-11.0.19.7-hotspot/bin
export JAVA_HOME=/c/Program\ Files/Eclipse\ Adoptium/jre-11.0.19.7-hotspot

需要在路径中的空格前添加 \ 进行转义。

参见:Declaring space characters in PATH for MSYS2 - Windows

将 MSYS2 的 SHELLL 修改为 ZSH

  • 先在 MSYS2 中安装 ZSH:pacman -S zsh

  • 然后在桌面建立快捷方式:C:\msys64\msys2_shell.cmd -mingw64 -shell zsh

  • 或在 msys2.ini 配置文件中,加入 SHELL=/usr/bin/zsh

参见:Change default shell on MSYS2

获得完整的 ZSH 体验

  • 利用 zsh-users/Antigen 项目获得完整的 ZSH 体验;

  • 其中会报出安装 zsh-users/zsh-syntax-hightlighting 失败,此时可手动运行:

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc

安装这个主题。

winget 方式

首先使用 winget search foobar 搜索要安装的软件,比如:

> winget search obs
尝试更新源失败: winget
名称                          ID                                 版本         匹配             源
------------------------------------------------------------------------------------------------------
OBS Studio                    XPFFH613W8V6LV                     Unknown                       msstore
NASA Earth Observatory        9WZDNCRFJ252                       Unknown                       msstore
European Southern Observatory 9WZDNCRDF64K                       Unknown                       msstore
Obscure Extractor             9NXLQD97BT9Z                       Unknown                       msstore
OBS Studio                    OBSProject.OBSStudio               29.1.3       Moniker: obs     winget
哔哩哔哩直播姬                Bilibili.Livehime                  4.48.0.5057  Tag: obs         winget
StreamlabsOBS                 Streamlabs.StreamlabsOBS           0.23.2                        winget
Stinker                       PatentLobster.stinker              0.2.3                         winget
OpenObserve                   openobserve.openobserve            0.4.7                         winget
AMD Encoder for OBS Studio    OBSProject.obs-amd-encoder         2.6.0.0                       winget
MonkeyMote 4 Foobar           ObsidiumSoftware.MonkeyMote4Foobar 3.4.0.3                       winget
Hindsight                     obsidianforensics.HindsightGUI     2023.03                       winget
Obsidian                      Obsidian.Obsidian                  1.3.5                         winget
OBS RTSP Server Plugin        iamscottxu.obs-rtspserver          3.0.0                         winget
obs-virtualcam                Fenrirthviti.obs-virtual-cam       2.0.5                         winget
Mobster                       dillonkearns.mobster               0.0.48                        winget
obs-asio                      Andersama.obs-asio                 3.1.0                         winget
blobsaver                     Airsquared.Blobsaver               3.5.1                         winget
OBS Studio Beta               OBSProject.OBSStudio.Pre-release   29.1.0-beta3                  winget
Streamlabs Desktop            Streamlabs.Streamlabs              1.11.1                        winget
Deckboard                     RivaFarabi.Deckboard               2.1.3        Tag: obs-remote  winget
Quarto                        Posit.Quarto                       1.3.433      Tag: observable  winget
.NET Reactor                  Eziriz.DotNetReactor               6.8.0.0      Tag: obsfucation winget
Aladin                        CDS.AladinDesktop                  11.024       Tag: observatory winget

然后使用打算安装软件的 ID 字段,运行下面的命令安装该软件。

winget install OBSProject.OBSStudio

winget 的命令:Commands

scoop.sh 方式

scoop.sh 是另一个 Windows 的命令行安装程序。

Scoop 从命令行中安装咱们所知道和喜爱的程序,而且摩擦最小。他:

  • 消除权限弹出窗口
  • 隐藏 GUI 向导式安装程序
  • 防止安装大量程序造成 PATH 污染
  • 避免因安装和卸载程序而产生的意外副作用
  • 自动查找并安装依赖项
  • 自行执行所有额外的设置步骤以获得工作的程序

删除用户配置文件

在删除 Win 系统用户时,除了要在 “计算机管理” 中删除用户,还要删除用户配置文件。有两种方式:

  • 前往 C:/Users/ 目录,删除对应用户名的文件夹;
  • “我的电脑” -> “属性” -> "高级系统设置" -> “用户配置文件” -> “设置”

首选第二种方式;因为第一种删除文件夹的方式,可能出现权限问题删除不了,而第二种方式则没有这方面的问题。

安装 OpenSSH-server

参考:Installing SFTP/SSH Server on Windows using OpenSSH

安装 SFTP/SSH Server

  • 在 Windows 11 上

    • 前往 “设置” -> “应用” -> “可选功能”;
    • 找到 “OpenSSH 服务器”,选中他,然后点击安装。
  • 在 Windows 10 上

    • 前往 “设置” -> “应用及功能” -> “添加功能”;
    • 找到 “OpenSSH 服务器”,选中他,然后点击安装。

二进制程序会被安装到 %WINDIR%\System32\OpenSSH。配置文件 sshd_config 及主机密钥会安装到 %ProgramData%\ssh(只有在服务器首次启动后才会有)。

配置 SSH 服务器

  • 在 Windows 防火墙中放行到 SSH 服务器的入站连接:
    • 在作为可选功能安装完成时,防火墙规则 OpenSSH SSH Server (sshd) 应已被自动创建。若没有自动创建此规则,请如下创建并启用此规则。
    • 作为管理员运行下面的命令:
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH SSH Server' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 -Program "C:\Windows\System32\OpenSSH\sshd.exe"

请将 C:\Windows\System32\OpenSSH\sshd.exe 替换为到 sshd.exe 的实际路径(若手动安装 OpenSSH-Server, 即为:C:\Program Files\OpenSSH\ssh.exe)。

  • 或前往 Windows 安全 -> 防火墙与安全 -> 管理工具,并打开 服务。找到 OpenSSH SSH 服务器 服务。
  • 启动服务及/或配置自动启动:
    • 前往 控制面板 -> 系统与安全 -> 管理工具 并打开 服务。找到 OpenSSH SSH 服务器 服务;
    • 若咱们希望在机器启动时服务器自动启动:请前往 操作 -> 属性(或直接双击该服务)。在属性对话框中,修改 启动类型自动确定.
    • 通过点击 启动 按钮启动 OpenSSH SSH 服务器,或者点击菜单中的 操作 -> 启动

AD 下用户证书申请与导出

申请新证书

在 “管理控制台” (按下 Win + R 输入 mmc 打开)中,“文件” -> “添加/删除管理单元” -> “证书” -> “我的用户账户”,在 “个人” 上点击右键,选择 “所有任务” -> “申请新证书”

导出证书

需要选择 “导出私钥” 选项,必要时为证书设置密码。

“Internet 选项” 中 SSL/TLS 相关设置被修改的问题

参考:

  1. IE高级配置中支持的SSL/TLS协议对应注册表值
  2. Turn off encryption support

Win 系统中 “Internet 选项” 中,SSL/TLS 设置不当,会导致无法连接到相关服务。有的时候这些设置会被未知程序修改。可在 “管理员终端,Windows Powershell(Admin)” 里,运行注册表命令快速设置。

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v SecureProtocols /t REG_DWORD /d 2728 /f

其中的 2728 是表示 SSL/TLS 设置组合的代码,不同组合有不同的代码。

使用 sshfs-win 映射网络位置

参考:winfsp/sshfs-win

安装 sshfs-win

choco install -y sshfs

winget install WinFsp.WinFsp; winget install SSHFS-Win.SSHFS-Win

然后映射一个网络驱动器到指定位置:

\\sshfs\REMUSER@HOST[\PATH]

Windows 下 Docker 运行的一个问题

Windows 系统上通过运行:

choco install -y docker-for-windows

即可安装 Docker 环境。

报出:

> docker image ls -a
error during connect: in the default daemon configuration on Windows, the docker client must be run with elevated privileges to connect: Get "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/images/json?all=1": open //./pipe/docker_engine: The system cannot find the file specified.

此问题在 docker: error during connect: In the default daemon configuration on Windows, the docker client must be run with elevated privileges to connect 上有讨论,并按照 这个帖子 操作并重启计算机后,即可解决。所需执行的操作如下(均需在管理员终端下运行):

  1. 更新 wsl 内核,显然这是必须的,因为 docker 需要他,而他在默认情况下是不安装的:
wsl --update
  1. 运行下面的命令:
& 'C:\Program Files\Docker\Docker\DockerCli.exe' -SwitchDaemon

重启系统后,Docker Desktop 会随系统启动自动运行。

Docker Desktop

注意docker-desktop 需要管理员权限运行,否则在启动容器时会报错。

O365 激活操作

摘录参考自kms-activate

O365 Retail 版转换为 VOL 版并激活的步骤(以下命令需再管理员 CMD 窗口执行,管理员 Powershell 终端窗口无效):

  1. 进入 ospp.vbs 所在的目录
cd C:\Program Files\Microsoft Office\Office16
  1. 将 Retail 版转化为 VOL 版
for /f %x in ('dir /b ..\root\Licenses16\proplusvl_kms*.xrm-ms') do cscript ospp.vbs /inslic:"..\root\Licenses16\%x"
for /f %x in ('dir /b ..\root\Licenses16\proplusvl_mak*.xrm-ms') do cscript ospp.vbs /inslic:"..\root\Licenses16\%x"
  1. 安装 KMS 激活密钥
cscript ospp.vbs /inpkey:XQNVK-8JYDB-WJ9W3-YJ8YR-WFG99

关于 KMS 激活密钥,请参考:Microsoft Office 2019 RTM GVLK Keys

  1. 激活
cscript ospp.vbs /sethst:kms.03k.org && cscript ospp.vbs /act
  1. 可用的 KMS 服务器地址汇总

可同时激活 Windows 和 Office。

kms.03k.org
kms.chinancce.com
kms.luody.info
kms.lotro.cc
kms.luochenzhimu.com
kms8.MSGuides.com
kms9.MSGuides.com

参考:适用于windows && office 可用kms服务器激活地址汇总

msys2/home 位置

执行 pwd -W 命令,即可找出 /home 的位置:

 MSYS ~
$ pwd -W
C:/tools/msys64/home/Lenny.Peng

OneDrive 无法登录问题

在 OneDrive 遇到无法登录问题时,需暂时打开全局代理(并关闭 AutoProxy PAC),让 OneDrive 完成登录,得到相应登录凭据,随后便可以关闭全局代理。

限制 wsl 内存使用

# 关闭全部 wsl 示例,比如 docker-desktop
wsl --shutdown
notepad "$env:USERPROFILE/.wslconfig"

设置咱们希望的 CPU 核数与内存值:

[wsl2]
memory=3GB   # Limits VM memory in WSL 2 up to 3GB
processors=2 # Makes the WSL 2 VM use two virtual processors

Powershell 中设置环境变量

例如:

$env:JENKINS_URL = 'https://ci.xfoss.com'

检查环境变量:

> dir env:

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\Lenny.Peng\AppData\Roaming
ChocolateyInstall              C:\ProgramData\chocolatey
ChocolateyLastPathUpdate       133328263824380565
ChocolateyToolsLocation        C:\tools
CommonProgramFiles             C:\Program Files\Common Files
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
CommonProgramW6432             C:\Program Files\Common Files
COMPUTERNAME                   PC-SSZ063-1
ComSpec                        C:\WINDOWS\system32\cmd.exe
DEFLOGDIR                      C:\ProgramData\McAfee\Endpoint Security\Logs
DriverData                     C:\Windows\System32\Drivers\DriverData
GROOVY_HOME                    C:\tools\groovy-3.0.14
HOMEDRIVE                      C:
HOMEPATH                       \Users\Lenny.Peng
JENKINS_URL                    https://ci.xfoss.com
JMETER_HOME                    C:\ProgramData\chocolatey\lib\jmeter\tools\apache-jmeter-5.5
LOCALAPPDATA                   C:\Users\Lenny.Peng\AppData\Local
LOGONSERVER                    \\DC-SZ2
NUMBER_OF_PROCESSORS           8
NVM_HOME                       C:\Users\Lenny.Peng\AppData\Roaming\nvm
NVM_SYMLINK                    C:\Program Files\nodejs
OS                             Windows_NT
Path                           C:\Program Files\Eclipse Adoptium\jre-11.0.19.7-hotspot\bin;C:\Python311\Scripts\;C:\Python311\;C:\S2C\PlayerPro_Runtime\bin;C:\WINDOWS\system32;C:\WINDOWS...
PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW;.CPL
PROCESSOR_ARCHITECTURE         AMD64
PROCESSOR_IDENTIFIER           Intel64 Family 6 Model 140 Stepping 2, GenuineIntel
PROCESSOR_LEVEL                6
PROCESSOR_REVISION             8c02
ProgramData                    C:\ProgramData
ProgramFiles                   C:\Program Files
ProgramFiles(x86)              C:\Program Files (x86)
ProgramW6432                   C:\Program Files
PSModulePath                   C:\Users\Lenny.Peng\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
PUBLIC                         C:\Users\Public
RTHome                         C:\S2C\PlayerPro_Runtime
S2C_Security                   1
SESSIONNAME                    Console
SystemDrive                    C:
SystemRoot                     C:\WINDOWS
TEMP                           C:\Users\LENNY~1.PEN\AppData\Local\Temp
TMP                            C:\Users\LENNY~1.PEN\AppData\Local\Temp
USERDNSDOMAIN                  XFOSS.COM
USERDOMAIN                     XFOSS
USERDOMAIN_ROAMINGPROFILE      XFOSS
USERNAME                       Lenny.Peng
USERPROFILE                    C:\Users\Lenny.Peng
windir                         C:\WINDOWS
ZES_ENABLE_SYSMAN              1

Windows 10 LTSC 企业评估版升级到完整版并激活

如果咱们下载的是 Windows 10 Enterprise LTSC 2021 评估版,就没有密钥可以激活它。

Windows 10 Enterprise LTSC 的 SKU 提取到 C:\Windows\System32\spp\tokens\skus, 然后以管理员权限,在 CMD 命令行窗口中运行一下命令:

cscript.exe %windir%\system32\slmgr.vbs /rilc
cscript.exe %windir%\system32\slmgr.vbs /upk >nul 2>&1
cscript.exe %windir%\system32\slmgr.vbs /ckms >nul 2>&1
cscript.exe %windir%\system32\slmgr.vbs /cpky >nul 2>&1
cscript.exe %windir%\system32\slmgr.vbs /ipk M7XTQ-FN8P6-TTKYV-9D4CC-J462D
sc config LicenseManager start= auto & net start LicenseManager
sc config wuauserv start= auto & net start wuauserv
clipup -v -o -altto c:\
echo

Hyper-V 中 Linux 虚拟机 .vhdx 文件收缩

  1. 登入客户机 Linux 系统,删除不需要的文件与文件夹(如有必要,将 Linux 引导至 GParted,调整其文件系统);

  2. 关闭 Linux 客户机。在 Hyper-V 管理器中建立一块虚拟磁盘,将其挂载到 Linux 客户机;

  3. 设置 Linux 客户机从光盘启动 Clonezilla,在 Clonezilla 中备份该 Linux 客户机整个磁盘;

  4. 在 Hyper-V 管理器中,参考原 Linux 客户机磁盘大小,建立一个新的虚拟机,然后使用 Clonezilla 将备份恢复到这个新的虚拟机。

完成上述操作后,成功实现虚拟机磁盘的压缩。

注意:在关闭原 Linux 主机时,需要手动停止(stop) Docker 里的各个容器,并在原 Linux 主机下,停止包括 Docker 在内的各个服务,否则会出现 Error response from daemon: stat /var/lib/docker/btrfs/subvolumes/[...] 错误,导致恢复后,重启启动一些 Docker 容器出现问题。

PowerShell 命令备忘录

  • ls | grep "mubs" 等价物
ls | Out-String -Stream | Select-String "mubs"

wsl 无法配置 networkingMode Nat)( 网络

wsl 无法配置 networkingMode Nat)( 网络

需要在 “启用或关闭 Windows 功能”(按下视窗按键,输入 “windows features”)中,开启 “虚拟机平台”。

参见:Can't start distro on WSL 2.0.5 with any networkingMode besides None

Last change: 2023-11-17, commit: 2dc9b95