diff --git a/source/_posts/Linux系统监控.md b/source/_posts/Linux系统监控.md index 1845905..93a2e51 100644 --- a/source/_posts/Linux系统监控.md +++ b/source/_posts/Linux系统监控.md @@ -183,16 +183,121 @@ lspci | grep -i 'eth' ifconfig -a ``` +### 查看网络接口详细信息 + +```shell +ip link show +``` + **输出示例**: ``` -docker0: flags=4163 mtu 1500 - inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 - ether 02:42:66:fe:52:a2 txqueuelen 0 (Ethernet) +1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 +2: em1: mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 + link/ether 2c:ea:7f:a9:fc:76 brd ff:ff:ff:ff:ff:ff +``` -em1: flags=4163 mtu 1500 - inet 192.168.6.20 netmask 255.255.255.0 broadcast 192.168.6.255 - ether 2c:ea:7f:a9:fc:76 txqueuelen 1000 (Ethernet) +### 查看网络流量统计 + +```shell +cat /proc/net/dev +``` + +**输出示例**: + +``` +Inter-| Receive | Transmit + face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed + em1: 589404500 4189635 0 245895 0 0 0 966587 3040611778 3751409 0 0 0 0 0 0 +``` + +### 查看网卡详细参数 + +```shell +ethtool em1 +``` + +**输出示例**: + +``` +Settings for em1: + Supported ports: [ TP ] + Supported link modes: 10baseT/Half 10baseT/Full + 100baseT/Half 100baseT/Full + 1000baseT/Half 1000baseT/Full + Speed: 100Mb/s + Duplex: Full + Port: Twisted Pair + Auto-negotiation: on + Link detected: yes +``` + +--- + +## PCI信息 + +### 查看PCI设备 + +```shell +lspci +``` + +**输出示例**: + +``` +00:00.0 Host bridge: Intel Corporation Sky Lake-E DMI3 Registers (rev 07) +00:14.0 USB controller: Intel Corporation C620 Series Chipset Family USB 3.0 xHCI Controller +04:00.0 Ethernet controller: Broadcom NetXtreme BCM5720 2-port Gigabit Ethernet PCIe +``` + +### 查看详细信息 + +```shell +lspci -v # 详细信息 +lspci -vv # 更详细信息 +``` + +### 查看设备树 + +```shell +lspci -t +``` + +--- + +## USB信息 + +### 查看USB设备 + +```shell +lsusb +``` + +**输出示例**: + +``` +Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub +Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub +``` + +### 查看USB拓扑 + +```shell +lsusb -t +``` + +**输出示例**: + +``` +/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/10p, 5000M +/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/16p, 480M +``` + +### 查看详细信息 + +```shell +lsusb -v ``` --- diff --git a/source/_posts/Linux软件安装.md b/source/_posts/Linux软件安装.md index b5a8d1b..de61e11 100644 --- a/source/_posts/Linux软件安装.md +++ b/source/_posts/Linux软件安装.md @@ -189,4 +189,1130 @@ export PATH=$PATH:$JAVA_HOME/bin ```shell source /etc/profile +``` + +--- + +## MySQL 安装 + +### CentOS 安装(yum方式) + +#### 1. 卸载 MariaDB + +有些 Linux 会自带 MariaDB 数据库,需要先卸载。 + +```shell +# 列出已安装的 MariaDB 包 +rpm -qa | grep mariadb + +# 卸载 MariaDB +rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 +``` + +#### 2. 下载并安装 MySQL yum 库 + +```shell +cd /home/ +yum install wget -y +wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm +rpm -Uvh mysql80-community-release-el7-1.noarch.rpm +``` + +#### 3. 选择 MySQL 版本 + +编辑 `/etc/yum.repos.d/mysql-community.repo`: + +```shell +vim /etc/yum.repos.d/mysql-community.repo +``` + +选择要安装的 MySQL 版本: + +```shell +[mysql57-community] +name=MySQL 5.7 Community Server +baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ +enabled=0 # 安装5.7时改为1 +gpgcheck=1 +gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql + +[mysql80-community] +name=MySQL 8.0 Community Server +baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/ +enabled=1 # 安装5.7时改为0 +gpgcheck=1 +gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql +``` + +> `enabled=0` 为禁用,`enabled=1` 为启用。 + +#### 4. 安装并启动 MySQL + +```shell +yum install mysql-community-server +service mysqld start +``` + +如果出现 GPG 错误,导入密钥: + +```shell +# CentOS +rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 + +# Ubuntu +wget -q -O - https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 | apt-key add - + +yum install mysql-community-server +``` + +#### 5. 配置 MySQL + +```shell +# 查看初始密码 +sudo grep 'temporary password' /var/log/mysqld.log + +# 登录 MySQL +mysql -u root -p + +# 设置密码验证强度等级 +set global validate_password_policy=LOW; + +# 设置密码长度为 6 位 +set global validate_password_length=6; + +# 修改密码 +ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; + +# 开启远程登录 +use mysql; +select user,host from user; +update user set host = '%' where user = 'root'; +flush privileges; +``` + +--- + +### AlmaLinux 安装(dnf方式) + +#### 1. 更新系统 + +```shell +sudo dnf clean all +sudo dnf update +sudo dnf groupinstall "Development Tools" +``` + +#### 2. 安装 MySQL + +```shell +sudo dnf install mysql mysql-server +``` + +#### 3. 设置表名不区分大小写 + +编辑 `/etc/my.cnf.d/mysql-server.cnf`: + +```shell +vim /etc/my.cnf.d/mysql-server.cnf + +# 在 [mysqld] 中添加 +lower_case_table_names=1 +``` + +初始化后验证: + +```mysql +show global variables like '%lower_case%'; +# lower_case_table_names 应为 1 +``` + +#### 4. 启动并设置开机自启 + +```shell +sudo systemctl enable --now mysqld +sudo systemctl status mysqld +``` + +确认安装版本: + +```shell +mysql --version +``` + +#### 5. 安全配置 + +```shell +mysql_secure_installation +``` + +按提示操作: + +```shell +Enter current password for root (enter for none): 直接按 Enter +Set root password? [Y/n]: Y +New password: 输入密码 +Re-enter new password: 再次输入密码 +Remove anonymous users? [Y/n]: Y +Disallow root login remotely? [Y/n]: Y +Remove test database and access to it? [Y/n]: Y +Reload privilege tables now? [Y/n]: Y +``` + +#### 6. 创建数据库和用户 + +```shell +sudo mysql -u root -p +``` + +```mysql +CREATE DATABASE test_db; +CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'your-password'; +GRANT ALL ON test_db.* TO 'test_user'@'localhost'; +FLUSH PRIVILEGES; +EXIT +``` + +--- + +### AlmaLinux 安装(rpm手动方式) + +#### 1. 下载并解压安装包 + +```shell +mkdir mysql_install +cd mysql_install +wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.41-1.el7.x86_64.rpm-bundle.tar +tar -xvf mysql-5.7.41-1.el7.x86_64.rpm-bundle.tar +``` + +#### 2. 安装依赖 + +```shell +rpm -ivh mysql-community-common-5.7.41-1.el7.x86_64.rpm + +rpm -ivh mysql-community-libs-5.7.41-1.el7.x86_64.rpm + +rpm -ivh mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm +``` + +如果出现依赖错误: + +```shell +# 缺少 libcrypto.so.10 +dnf install -y https://repo.almalinux.org/almalinux/8/AppStream/x86_64/os/Packages/compat-openssl10-1.0.2o-4.el8_6.x86_64.rpm +``` + +```shell +rpm -ivh mysql-community-devel-5.7.41-1.el7.x86_64.rpm +``` + +如果出现依赖错误: + +```shell +# 缺少 pkg-config +dnf install openssl-devel -y +``` + +```shell +rpm -ivh mysql-community-client-5.7.41-1.el7.x86_64.rpm +``` + +如果出现依赖错误: + +```shell +# 缺少 libncurses +dnf install libncurses* -y +dnf install epel-release -y +dnf install ncurses-compat-libs -y +``` + +```shell +rpm -ivh mysql-community-server-5.7.41-1.el7.x86_64.rpm +``` + +如果出现提示: + +```shell +# 修改 /usr/lib/tmpfiles.d/mysql.conf +vim /usr/lib/tmpfiles.d/mysql.conf +# 将 /var/run/mysqld 改为 /run/mysqld + +# 缺少 libcrypt +dnf install -y libxcrypt-compat + +# 缺少 perl +dnf install -y perl.x86_64 + +# 缺少 net-tools +dnf install net-tools -y +``` + +```shell +rpm -ivh mysql-community-embedded-compat-5.7.41-1.el7.x86_64.rpm +``` + +#### 3. 编辑配置文件 + +```shell +vim /etc/my.cnf +``` + +配置示例: + +```shell +[client] +port = 3306 +user = mysql + +[mysqld] +basedir=/usr/local/mysql +datadir=/var/lib/mysql +socket=/var/lib/mysql/mysql.sock +symbolic-links=0 +log-error=/var/log/mysqld.log +pid-file=/var/run/mysqld/mysqld.pid +max_connections = 400 +character-set-server = utf8mb4 +explicit_defaults_for_timestamp = true +lower_case_table_names = 1 +``` + +#### 4. 初始化并启动 + +```shell +mysqld --defaults-file=/etc/my.cnf --initialize-insecure --user=mysql + +# 给 mysql 用户添加数据目录权限 +chown mysql:mysql /var/lib/mysql -R + +systemctl start mysqld +systemctl enable mysqld + +# 查看随机密码(如果有) +grep 'temporary password' /var/log/mysqld.log + +# 若没有提示密码,可直接登录 +mysql -uroot +``` + +--- + +## Redis 安装 + +### dnf 方式 + +#### 1. 更新软件包缓存 + +```shell +sudo dnf makecache +``` + +#### 2. 安装 Redis + +```shell +sudo dnf install redis +``` + +#### 3. 启动并设置开机自启 + +```shell +sudo systemctl start redis +sudo systemctl enable redis +``` + +#### 4. 验证服务状态 + +```shell +sudo systemctl is-enabled redis +sudo systemctl status redis +redis-server +``` + +#### 5. 配置 Redis + +编辑配置文件: + +```shell +sudo vim /etc/redis.conf +``` + +#### 6. Redis-CLI 使用 + +```shell +redis-cli +auth +``` + +### 源码编译方式 + +#### 1. 下载并解压 + +```shell +wget https://download.redis.io/releases/redis-6.2.14.tar.gz +tar -zxvf redis-6.2.14.tar.gz +cd redis-6.2.14 +``` + +#### 2. 编译安装 + +```shell +# 编译并安装,默认安装在 /usr/local/bin/ +make && make install + +# 编译并指定安装目录 +make && make PREFIX=/test/www/server/redis-6.2.14 install + +# 测试 +make test +``` + +--- + +## Node.js 安装 + +### AlmaLinux dnf 方式 + +```shell +dnf update -y +dnf install nodejs -y +``` + +### 编译包方式(Prebuilt Binaries) + +#### 1. 解压并移动 + +```shell +tar -xf node-v16.20.2-linux-x64.tar.xz +mv node-v16.20.2-linux-x64 /var/lib +``` + +#### 2. 创建软链接 + +```shell +ln -s /var/lib/node-v16.20.2-linux-x64/bin/node /usr/bin/node +ln -s /var/lib/node-v16.20.2-linux-x64/bin/npm /usr/bin/npm +ln -s /var/lib/node-v16.20.2-linux-x64/bin/npx /usr/bin/npx +``` + +### 手动安装方式 + +#### 1. 解压并移动 + +```shell +tar -xvf node-v18.12.1-linux-x64.tar.xz +mv node-v18.12.1-linux-x64 nodejs +mv nodejs /usr/local +``` + +#### 2. 配置环境变量 + +**方式一:软链接** + +```shell +ln -s /usr/local/nodejs/bin/npm /usr/local/bin/ +ln -s /usr/local/nodejs/bin/node /usr/local/bin/ +``` + +**方式二:环境变量** + +```shell +vim /etc/profile +# 添加以下行 +export PATH=$PATH:/usr/local/nodejs/bin +``` + +#### 3. 验证安装 + +```shell +node -v +npm -v +``` + +--- + +## .Net SDK 安装 + +### CentOS 7 + +```shell +sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm +sudo yum install dotnet-sdk-6.0 +dotnet --info +``` + +--- + +## 7zip 安装 + +```shell +# 更新系统数据库 +sudo dnf update -y + +# 启用 Epel repository +sudo dnf install epel-release + +# 安装 7-Zip +sudo dnf install p7zip p7zip-plugins + +# 检验是否安装成功 +7z +``` + +### 使用方法 + +```shell +# 创建压缩文件(选项 a 用于压缩) +7z a data.7z data.txt + +# 显示存档文件详细信息列表 +7z l data.7z + +# 解压缩 +# 注意:-o 用来指定解压缩目录,-o 后没有空格,直接接目录 +7z x data.7z -r -o./data +``` + +--- + +## Supervisor 安装 + +Supervisor 是进程守护工具,用于管理和监控进程。 + +### 安装 + +```shell +sudo dnf update -y +sudo dnf install epel-release -y +sudo dnf install supervisor -y +``` + +### 配置 + +编辑 `/etc/supervisord.conf`: + +```shell +sudo vim /etc/supervisord.conf + +# 开启 web 服务管理界面 +# 修改 port 中的 ip 为 0.0.0.0,以允许任何 ip 访问 +# 修改用户名密码 +# 去掉行首的 ; 以使配置生效 +[inet_http_server] +port=0.0.0.0:9001 +username=user +password=123 + +# 修改包含子配置文件,文件类型为 .conf,默认为 .ini +[include] +files = supervisord.d/*.conf +``` + +### 常用命令 + +```shell +# 启动 supervisord +sudo systemctl start supervisord + +# 开机启动 +sudo systemctl enable supervisord + +# 检查是否开机启动 +sudo systemctl is-enabled supervisord + +# 检查状态 +sudo systemctl status supervisord + +# 更新新配置(不会重启原来已运行的程序) +sudo supervisorctl update + +# 载入所有配置并重启所有进程 +sudo supervisorctl reload + +# 启动某个进程 +sudo supervisorctl start xxx + +# 重启某个进程 +sudo supervisorctl restart xxx + +# 停止某个进程 +sudo supervisorctl stop xxx + +# 停止全部进程 +sudo supervisorctl stop all + +# 查看服务状态 +sudo supervisorctl status +``` + +### 程序配置示例 + +创建 `/etc/supervisord.d/ckadminnetcore.conf`: + +```conf +[program:ckadminnetcore] +command=dotnet CK.Admin.WebApi.dll --urls http://[*]:8888 +directory=/root/www/ckadminnetcore/publish +environment=ASPNETCORE_ENVIRONMENT=Production +user=root +autostart=true +autorestart=true +stderr_logfile=/var/log/ckadminnetcore/err.log +stdout_logfile=/var/log/ckadminnetcore/out.log +stopasgroup=true +``` + +--- + +## ohmyzsh 安装 + +ohmyzsh 是一个管理 Zsh 配置的框架,提供丰富的主题和插件。 + +### 安装 Zsh + +```shell +dnf install -y zsh +``` + +### 安装 ohmyzsh + +| 方式 | 命令 | +|-----|------| +| curl | `sh -c "$(curl -fsSL https://install.ohmyz.sh/)"` | +| wget | `sh -c "$(wget -O- https://install.ohmyz.sh/)"` | +| 国内镜像(curl) | `sh -c "$(curl -fsSL https://gitee.com/pocmon/ohmyzsh/raw/master/tools/install.sh)"` | +| 国内镜像(wget) | `sh -c "$(wget -O- https://gitee.com/pocmon/ohmyzsh/raw/master/tools/install.sh)"` | + +> 注意:同意使用 oh-my-zsh 的配置模板覆盖已有的 `.zshrc`。 + +### 从 .bashrc 迁移配置(可选) + +```shell +# 查看 bash 配置文件 +cat ~/.bashrc + +# 编辑 zsh 配置文件并粘贴自定义配置 +vim ~/.zshrc + +# 启动新的 zsh 配置 +source ~/.zshrc +``` + +### 配置主题 + +```shell +vim ~/.zshrc + +# 修改主题 +ZSH_THEME='agnoster' + +source ~/.zshrc +``` + +### 切换为默认 Shell + +```shell +dnf install util-linux-user -y +chsh -s /bin/zsh + +# 查看默认 Shell +echo $SHELL +``` + +### 推荐主题 + +#### Powerlevel10K + +```shell +git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k + +# 中国用户可以使用 gitee 镜像 +git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k +``` + +在 `~/.zshrc` 设置: + +```shell +ZSH_THEME="powerlevel10k/powerlevel10k" +``` + +若是 AlmaLinux minimal 系统,需先安装 `"Development Tools"`: + +```shell +dnf groupinstall "Development Tools" -y +``` + +### 推荐插件 + +#### zsh-autosuggestions + +命令提示插件,按下右键可快速采用建议。 + +```shell +git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions + +# 国内镜像 +git clone https://gitee.com/pocmon/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions +``` + +#### zsh-syntax-highlighting + +命令语法校验插件,合法命令显示绿色,非法显示红色。 + +```shell +git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting + +# 国内镜像 +git clone https://gitee.com/pocmon/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting +``` + +#### z(内置) + +文件夹快捷跳转插件,输入目标文件夹名称即可快速跳转。 + +#### extract(内置) + +解压任何压缩文件,使用 `x` 命令即可。 + +#### web-search(内置) + +在命令行中使用搜索引擎搜索。 + +### 启用插件 + +修改 `~/.zshrc`: + +```shell +plugins=(git zsh-autosuggestions zsh-syntax-highlighting z extract web-search) +``` + +执行 `source ~/.zshrc` 生效。 + +### 卸载 + +```shell +uninstall_oh_my_zsh +``` + +### 手动更新 + +```shell +upgrade_oh_my_zsh +``` + +--- + +## ElasticSearch 安装 + +### 创建仓库文件 + +```shell +cd /etc/yum.repos.d +vim elasticsearch.repo +``` + +内容: + +```shell +[elasticsearch] +name=Elasticsearch repository for 8.x packages +baseurl=https://artifacts.elastic.co/packages/8.x/yum +gpgcheck=1 +gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch +enabled=0 +autorefresh=1 +type=rpm-md +``` + +### 安装 + +```shell +dnf install --enablerepo=elasticsearch elasticsearch -y +``` + +--- + +## Jenkins 安装 + +### 安装 Java + +Jenkins 需要 Java JRE v11 或 v17: + +```shell +sudo dnf install java-17-openjdk +java -version +``` + +### 添加 Jenkins 仓库 + +```shell +wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo +rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key +``` + +### 安装 Jenkins + +```shell +sudo dnf makecache +sudo dnf install fontconfig +sudo dnf install jenkins +``` + +### 启动并验证 + +```shell +sudo systemctl start jenkins +sudo systemctl status jenkins +``` + +### 修改端口 + +编辑 `/usr/lib/systemd/system/jenkins.service`: + +```shell +Environment="JENKINS_PORT=16060" + +# 重新加载配置 +systemctl daemon-reload +``` + +### 修改用户及用户组 + +```shell +User=root +Group=root + +systemctl daemon-reload +``` + +### 解决汉化不全问题 + +添加 `-Duser.language=C.UTF-8`: + +```shell +Environment="JAVA_OPTS=-Djava.awt.headless=true -Duser.language=C.UTF-8" + +systemctl daemon-reload +``` + +### 升级 + +升级前备份配置文件: + +```shell +cp -r /usr/lib/systemd/system/jenkins.service /usr/lib/systemd/system/jenkins.service.bak +dnf upgrade jenkins +``` + +--- + +## Certbot 安装 + +Certbot 是用于自动化管理 Let's Encrypt SSL 证书的工具。 + +### 安装 snapd + +#### 1. 添加 EPEL 仓库 + +```shell +dnf install epel-release +dnf upgrade +``` + +#### 2. 安装 snapd + +```shell +dnf install snapd +``` + +#### 3. 启用 systemd 单元 + +```shell +systemctl enable --now snapd.socket +``` + +#### 4. 创建符号链接 + +```shell +ln -s /var/lib/snapd/snap /snap +``` + +退出并重新登录或重启系统。 + +### 安装 Certbot + +```shell +snap install --classic certbot +ln -s /snap/bin/certbot /usr/bin/certbot +``` + +--- + +## frp 安装 + +frp 是内网穿透工具,用于将内网服务暴露到公网。 + +### 配置服务端 (frps) + +在公网服务器上部署。 + +#### 1. 创建服务文件 + +```shell +sudo vim /etc/systemd/system/frps.service +``` + +#### 2. 写入配置 + +```toml +[Unit] +Description=FRP Server Daemon +After=network.target syslog.target +Wants=network.target + +[Service] +Type=simple +ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.toml +Restart=on-failure +RestartSec=5s + +[Install] +WantedBy=multi-user.target +``` + +#### 3. 启动并设置开机自启 + +```shell +sudo systemctl daemon-reload +sudo systemctl start frps +sudo systemctl enable frps +sudo systemctl status frps +``` + +### 配置客户端 (frpc) + +在内网机器上部署。 + +#### 1. 创建服务文件 + +```shell +sudo vim /etc/systemd/system/frpc.service +``` + +#### 2. 写入配置 + +```toml +[Unit] +Description=FRP Client Daemon +After=network.target syslog.target +Wants=network.target + +[Service] +Type=simple +ExecStart=/usr/local/frp/frpc -c /usr/local/frp/frpc.toml +Restart=on-failure +RestartSec=5s + +[Install] +WantedBy=multi-user.target +``` + +#### 3. 启动并设置开机自启 + +```shell +sudo systemctl daemon-reload +sudo systemctl start frpc +sudo systemctl enable frpc +sudo systemctl status frpc +``` + +--- + +## 字体安装 + +### 查看已安装字体 + +```shell +fc-list +``` + +### 下载字体 + +```shell +git clone https://gitee.com/mirrors/nerd-fonts.git --depth 1 +``` + +### 系统级安装 + +#### 1. 导航到字体目录 + +```shell +cd /usr/share/fonts +# 若不存在则创建 +sudo mkdir /usr/share/fonts +``` + +#### 2. 复制字体 + +```shell +sudo cp -r ~/nerd-fonts/patched-fonts/Hack /usr/share/fonts +``` + +#### 3. 更新字体缓存 + +```shell +sudo fc-cache -f -v +``` + +### 用户级安装 + +#### 1. 创建用户字体目录 + +```shell +mkdir ~/.fonts +``` + +#### 2. 复制字体 + +```shell +cp -r ~/nerd-fonts/patched-fonts/Hack ~/.fonts +``` + +#### 3. 更新字体缓存 + +```shell +fc-cache -f -v +``` + +--- + +## Neofetch / Fastfetch 安装 + +系统信息展示工具。 + +### Neofetch + +```shell +dnf install epel-release +dnf install neofetch +neofetch +``` + +### Fastfetch + +```shell +dnf update -y +wget https://github.com/fastfetch-cli/fastfetch/releases/download/2.49.0/fastfetch-linux-amd64.rpm -O fastfetch.rpm +dnf install -y fastfetch.rpm +``` + +### Screenfetch + +```shell +dnf install git +git clone https://github.com/KittyKatt/screenFetch.git +cp screenFetch/screenfetch-dev /usr/bin/screenfetch +chmod +x /usr/bin/screenfetch +screenfetch +``` + +--- + +## Edge / Chrome 安装 + +### Microsoft Edge + +#### 1. 更新源 + +```shell +sudo dnf update -y +``` + +#### 2. 添加 Edge 源 + +```shell +sudo dnf config-manager --add-repo https://packages.microsoft.com/yumrepos/edge +``` + +#### 3. 再次更新源 + +```shell +sudo dnf update -y +``` + +#### 4. 安装 Edge + +```shell +sudo dnf install microsoft-edge-stable -y +``` + +### Google Chrome + +#### 1. 下载安装文件 + +```shell +wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm +``` + +#### 2. 安装 + +```shell +sudo dnf install ./google-chrome-stable_current_x86_64.rpm -y +``` + +--- + +## FastGithub 安装 + +GitHub 加速工具。 + +```shell +wget https://gitee.com/chcrazy/FastGithub/releases/download/2.1.4/fastgithub_linux-x64.zip +dnf install -y libicu + +# 配置系统代理 +vim /etc/profile +# 添加以下行 +export http_proxy="http://127.0.0.1:38457" +export https_proxy="http://127.0.0.1:38457" + +# 生效 +source /etc/profile + +# 解压并运行 +unzip fastgithub_linux-x64.zip +cd fastgithub_linux-x64 +./fastgithub +``` + +--- + +## Rsync 安装 + +文件同步工具。 + +### 安装 + +```shell +dnf install rsync -y +``` + +### 配置 + +编辑 `/etc/rsyncd.conf`: + +```shell +uid = root +gid = root +ignore errors +hosts allow = 10.0.3.0/24 +secrets file = /etc/rsyncd.secrets + +[syncwspswwwroot] +path = /root/wwwroot +log file = /var/log/rsync.log +read only = yes + +[mysql_bakup] +path = /root/mysql_bakup +read only = yes ``` \ No newline at end of file diff --git a/source/_posts/Nginx.md b/source/_posts/Nginx.md index f112303..db669af 100644 --- a/source/_posts/Nginx.md +++ b/source/_posts/Nginx.md @@ -5,6 +5,52 @@ tags: [Nginx, 反向代理, Web服务器, 负载均衡] categories: [运维, Web服务器] --- +# Nginx 安装 + +## 源码编译安装 + +```shell +tar -zxvf nginx-1.21.4.tar.gz +cd nginx-1.21.4/ +./configure +make +make install +``` + +## AlmaLinux/RHEL 安装 + +```shell +# 确保软件是最新的 +sudo dnf clean all +sudo dnf update +sudo dnf groupinstall "Development Tools" + +# 安装 Nginx +sudo dnf install nginx + +# 启动并设置开机自启 +sudo systemctl restart nginx +sudo systemctl status nginx +sudo systemctl enable nginx + +# 配置防火墙 +sudo firewall-cmd --permanent --add-service=http +sudo firewall-cmd --permanent --add-service=https +sudo firewall-cmd --reload +``` + +## 目录结构 + +| 目录/文件 | 说明 | +|-----------|------| +| `/etc/nginx` | 包含所有 Nginx 配置文件的主目录 | +| `/etc/nginx/nginx.conf` | 主要的 Nginx 配置文件 | +| `/etc/nginx/sites-available` | 定义各个网站的目录 | +| `/etc/nginx/sites-enabled` | Nginx 积极服务的网站列表 | +| `/var/log/nginx` | Nginx 日志目录 | + +--- + ## Nginx 隐藏版本号 nginx配置文件`nginx.conf`里增加 server_tokens off; diff --git a/source/_posts/Vim.md b/source/_posts/Vim.md index 50efbc6..93cf274 100644 --- a/source/_posts/Vim.md +++ b/source/_posts/Vim.md @@ -4,54 +4,90 @@ date: 2023-09-21 15:00:20 tags: [Vim, 编辑器, Linux, 开发工具] categories: [开发工具, Vim] --- +# Vim 编辑器 +## 简介 +Vim 是 Linux 系统中常用的文本编辑器,是 vi 的增强版本。它具有强大的编辑功能和可扩展性。 + +> **特点**:语法高亮、多级撤销/重做、插件系统 + +## 安装 Vim +### CentOS/RHEL/AlmaLinux +```shell +yum install -y vim +``` +### Ubuntu/Debian +```shell +sudo apt install vim +``` +### Arch Linux +```shell +sudo pacman -S vim +``` +## vi 基础命令 +vi 是 Vim 的前身,以下是 vi 的基本操作命令: +### 进入编辑模式 +| 按键 | 功能 | +|-----|------| +| `i` | 在光标前插入 | +| `I` | 在光标当前行的行头插入 | +| `a` | 在光标后插入 | +| `A` | 在光标当前行的行尾插入 | +| `o` | 在当前行的下方插入一个新行 | +| `O` | 在当前行的上方插入一个新行 | +### 保存与退出 +| 匉键 | 功能 | +|-----|------| +| `:wq` | 保存并退出 | +| `:q` | 不保存退出 | +| `:w` | 保存 | +| `:q!` | 强制不保存退出 | +| `:wq!` | 强制保存退出 | +| `Shift + zz` | 保存退出(与 `:wq` 相同) | +### 其他常用命令 +| 按键 | 功能 | +|-----|------| +| `:set nu` | 显示行号 | +| `:set nonu` | 取消显示行号 | +| `gg` | 到文本的第一行 | +| `G` | 到文本的最后一行 | +| `u` | 后退一步(相当于 Ctrl + z) | +| `Ctrl + r` | 前进一步 | +| `起始行号,结束行号 del` | 删除对应范围内的行 | +> 💡 **提示**:更多详细的 Vim 使用方法请参考下面的章节。 # Vim 的工作模式 - -vim 有6种工作模式。 - +vim 有6种工作模式: - 普通模式:使用 vim 打开一个文件时默认模式。也叫命令模式,运行用户通过各种命令浏览代码、滚屏等操作。 - 插入模式:也可以叫做编辑模式,在普通模式下敲击i、a 或 o 就进入插入模式,允许用户通过键盘输入、编辑。 -- 命令行模式:在普通模式下,先输入冒号`:`,接着输入命令 ,就可以通过配置命令对 vim 进行配置了,如改变颜色主题、显示行号等,这些配置命令也可以保存到/etc/vim/vimrc配置文件中,每次打开默认配置执行。 -- 可视化模式:在普通模式下敲击键盘上的 v 键,就进入可视化模式,然后移动光标就可以选中一块文本,常用来完成文本的复制、粘贴、删除等操作。 -- 替换模式:如果我们想修改某个字符,不需要先进入插入模式,删除,然后再输入新的字符,直接在普通模式下,敲击`R`键就可以直接替换。 -- EX模式:类似于命令行模式,可以一次运行多个命令。 - +- 命令行模式:在普通模式下,先输入冒号`:`,接着输入命令 ,就可以通过配置命令对 vim 进行配置了,如改变颜色主题、显示行号等,这些配置命令也可以保存到/etc/vim/vimrc配置文件中,每次打开默认配置执行 +- 可视化模式:在普通模式下敲击键盘上的 v 键,就进入可视化模式,然后移动光标就可以选中一块文本,常用来完成文本的复制、粘贴、删除等操作 +- 替换模式:如果我们想修改某个字符,不需要先进入插入模式,删除,然后再输入新的字符,直接在普通模式下,敲击`R`键就可以直接替换 +- EX模式:类似于命令行模式,可以一次运行多个命令 vim 的各种工作模式可以通过不同的键进行切换,用户统一使用`ESC`键返回到普通模式。 - ## 命令模式 - **用户刚刚启动vim,便进入里命令模式** - 此状态下敲击键盘动作会被 Vim 识别为命令,而非输入字符,比如我们此时按下`i`,并不会输入一个字符,`i`被当作了一个命令。 - 以下是普通模式常用的几个命令: - - `i` -- 切换到输入模式,在光标当前位置开始输入文本。 -- `x` -- 删除当前光标所在处的字符。 -- `:` -- 切换到底线命令模式,以在最底一行输入命令。 -- `a` -- 进入插入模式,在光标下一个位置开始输入文本。 -- `o` -- 在当前行的下方插入一个新行,并进入插入模式。 -- `O` -- 在当前行的上方插入一个新行,并进入插入模式。 -- `dd` -- 删除当前行。 -- `yy` -- 复制当前行。 -- `p`(小写)-- 粘贴剪贴板内容到光标下方。 -- `P`(大写)-- 粘贴剪贴板内容到光标上方。 -- `u` -- 撤销上一次操作。 -- `Ctrl + r` -- 重做上一次撤销的操作。 -- `:w` -- 保存文件。 +- `x` -- 删除当前光标所在处的字符 +- `:` -- 切换到底线命令模式,以在最底一行输入命令 +- `a` -- 进入插入模式,在光标下一个位置开始输入文本 +- `o` -- 在当前行的下方插入一个新行,并进入插入模式 +- `O` -- 在当前行的上方插入一个新行,并进入插入模式 +- `dd` -- 删除当前行 +- `yy` -- 复制当前行 +- `p`(小写)-- 粘贴剪贴板内容到光标下方 +- `P`(大写)-- 粘贴剪贴板内容到光标上方 +- `u` -- 撤销上一次操作 +- `Ctrl + r` -- 重做上一次撤销的操作 +- `:w` -- 保存文件 - `:q` -- 退出 Vim 编辑器 -- `:q!` -- 强制退出 Vim 编辑器,不保存修改。 - -若要编辑文本,只需要启动 Vim,进入了命令模式,按下`i`切换到输入模式即可。 - +- `:q!` -- 强制退出 Vim 编辑器,不保存修改 +若要编辑文本,只需要启动 Vim,进入了命令模式,按下`i`切换到输入模式即可 命令模式只有一些最基本的命令,因此仍要依靠**底线命令行模式**输入更多命令。 - ## 输入模式 - -在命令模式下按下`i`就进入了输入模式,使用`Esc`键可以返回到普通模式。 - -在输入模式中,可以使用以下按键: - +在命令模式下按下`i`就进入了输入模式,使用`Esc`键可以返回到普通模式 +在输入模式中,可以使用以下按键: - **字符按键以及Shifi组合**,输入字符 - **ENTER**,回车键,换行 - **BACK SPACE**,退格键,删除光标前一个字符 @@ -61,145 +97,128 @@ vim 的各种工作模式可以通过不同的键进行切换,用户统一使 - **Page Up/Page Down**,上/下翻页 - **Insert**,切换光标输入为输入\替换模式,光标将变成竖线/下划线 - **ESC**,退出输入模式,切换到命令模式 - ## 底线命令模式 - -在命令模式下按下`:`(英文冒号)就进入了底线命令模式。 - +在命令模式下按下`:`(英文冒号)就进入了底线命令模式 底线命令模式可以输入单个或多个字符的命令,可用的命令非常多。 - 在底线命令模式中,基本的命令有(已经省略了冒号): - - :w 保存文件 - :q 退出 Vim 编辑器 - :wq 保存文件并退出 Vim 编辑器 - :q! 强制退出 Vim 编辑器,不保存修改 - :set nu 开启行号 - :set nonu 关闭行号 - -按`ESC`键可随时退出底线命令模式。 - +按`ESC`键可随时退出底线命令模式 简单的说,我们可以将这三个模式想成底下的图标来表示: - ![img](https://www.runoob.com/wp-content/uploads/2014/07/vim-vi-workmodel.png) - # Vim 的按键说明 - ## 第一部分:命令模式可用的光标移动、复制粘贴、搜索替换等 -
- - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - - + +
移动光标的方法
移动光标的方法
h 或 向左箭头键(←) 光标向左移动一个字符
j 或 向下箭头键(↓)j 或 向下箭头键(↓) 光标向下移动一个字符
k 或 向上箭头键(↑)k 或 向上箭头键(↑) 光标向上移动一个字符
l 或 向右箭头键(→)l 或 向右箭头键(→) 光标向右移动一个字符
如果你将右手放在键盘上的话,你会发现 hjkl 是排列在一起的,因此可以使用这四个按钮来移动光标。如果想要进行多次移动的话,例如向下移动 30 行,可以使用 "30j" 或 "30↓" 的组合按键,亦即加上想要进行的次数(数字)后,按下动作即可!如果你将右手放在键盘上的话,你会发现 hjkl 是排列在一起的,因此可以使用这四个按钮来移动光标。如果想要进行多次移动的话,例如向下移动 30 行,可以使用 "30j" 或 "30↓" 的组合按键,亦即加上想要进行的次数(数字)后,按下动作即可!
Ctrl + fCtrl + f 屏幕『向下』移动一页,相当于 Page Down 按键 (常用)
Ctrl + bCtrl + b 屏幕『向上』移动一页,相当于 Page Up 按键 (常用)
Ctrl + dCtrl + d 屏幕『向下』移动半页
Ctrl + uCtrl + u 屏幕『向上』移动半页
++ 光标移动到非空格符的下一行
-光标移动到非空格符的上一行光标移动到非空格符的上一行
n『空格』n『空格』 那个 n 表示『数字』,例如 20 。按下数字后再按空格键,光标会向右移动这一行的 n 个字符。例如 20『空格』 则光标会向后面移动 20 个字符距离。
0 或功能键[Home]0 或功能键[Home] 这是数字『 0 』:移动到这一行的最前面字符处 (常用)
$ 或功能键[End]$ 或功能键[End] 移动到这一行的最后面字符处(常用)
HH 光标移动到这个屏幕的最上方那一行的第一个字符
MM 光标移动到这个屏幕的中央那一行的第一个字符
LL 光标移动到这个屏幕的最下方那一行的第一个字符
GG 移动到这个档案的最后一行(常用)
nGnG n 为数字。移动到这个档案的第 n 行。例如 20G 则会移动到这个档案的第 20 行(可配合 :set nu)
gg移动到这个档案的第一行,相当于 1G 啊! (常用)gg移动到这个档案的第一行,相当于 1G 啉! (常用)
n『 Enter 』n『 Enter 』 n 为数字。光标向下移动 n 行(常用)
搜索替换
搜索替换
# Vim配置文件 - -## 当前用户配置文件(~/.vimrc) - +## 当前用户配置文件(~/.vimrc) 若在当前用户目录下没有`.vimrc`,则手动新建即可 - ```bash vim ~/.vimrc ``` - 其中可添加如命令模式中的命令,比如`set nu`,之后再打开vim则默认开启行号 - ``` set nu ``` - -`:wq`保存退出后,需使用`source ~/.vimrc`使改动生效 +`:wq`保存退出后,需使用`source ~/.vimrc`使改动生效 \ No newline at end of file diff --git a/source/_posts/WSL2.md b/source/_posts/WSL2.md new file mode 100644 index 0000000..8c9a600 --- /dev/null +++ b/source/_posts/WSL2.md @@ -0,0 +1,260 @@ +--- +title: WSL2 +date: 2026-04-17 10:00:22 +author: 文永达 +tags: [WSL, Windows, Linux, 子系统] +categories: [操作系统, WSL] +--- + +# WSL2 (Windows Linux 子系统) + +WSL2 是 Windows 的 Linux 子系统,允许在 Windows 上运行 Linux 环境,无需使用虚拟机。 + +--- + +## 安装 + +### 1. 启用 Windows 功能 + +打开控制面板 → 程序和功能 → 启用或关闭 Windows 功能,勾选"适用于 Linux 的 Windows 子系统"。 + +也可通过运行窗口快速打开: + +```powershell +Win + R → 输入 appwiz.cpl → 回车 +``` + +![image-20250728082044688](https://rustfs.wenyongdalucky.club:443/hexo/image-20250728082044688.png) + +> 此步骤也可解决安装发行版时的报错: +> `错误代码: Wsl/InstallDistro/Service/RegisterDistro/CreateVm/HCS/HCS_E_SERVICE_NOT_AVAILABLE` + +### 2. 安装 WSL + +打开 Windows Terminal PowerShell: + +```powershell +wsl --install +``` + +> 微软官方文档:[安装 WSL | Microsoft Docs](https://docs.microsoft.com/zh-cn/windows/wsl/install) + +默认安装 Ubuntu 20.04 LTS 版。 + +### 3. 更改默认发行版 + +```powershell +wsl --install -d +``` + +### 4. 访问 Windows 磁盘 + +`/mnt` 目录下是 Windows 系统的挂载盘,可直接访问 Windows 磁盘文件。 + +--- + +## 迁移 + +将 WSL 发行版迁移到其他目录: + +```powershell +wsl --manage Ubuntu-24.04 --move d:\ubuntu +``` + +--- + +## 导出 + +### 1. 查看当前分发版 + +```powershell +wsl -l +``` + +输出示例: + +```powershell +适用于 Linux 的 Windows 子系统分发: +archlinux (默认值) +``` + +### 2. 停止运行中的 WSL + +```powershell +wsl --terminate archlinux +``` + +### 3. 导出镜像 + +使用 `wsl --export` 命令将分发版导出为 `.tar` 文件: + +```powershell +wsl --export archlinux E:\Backup\archlinux.tar +``` + +--- + +## 导入 + +使用 `wsl --import` 命令导入镜像: + +```powershell +wsl --import <发行版名称> <安装位置> +``` + +--- + +## 通过 FinalShell 连接 WSL2 + +### 方式一:重装 SSH + +#### 1. 删除并重装 SSH + +```shell +apt-get remove --purge openssh-server +apt-get install openssh-server +rm /etc/ssh/ssh_config +service ssh --full-restart +``` + +#### 2. 修改配置文件 + +```shell +vim /etc/ssh/sshd_config + +Port 6666 # 指定连接端口 6666 +ListenAddress 0.0.0.0 # 指定连接的 IP +PasswordAuthentication yes # 开启密码认证 +PermitRootLogin yes # 开启 root 用户登录 +``` + +#### 3. 重启 SSH(每次重启 WSL 都要执行) + +```shell +service ssh --full-restart +``` + +#### 4. 重新生成 host key + +```shell +dpkg-reconfigure openssh-server +``` + +### 方式二:端口转发 + +#### 1. 查看 WSL 地址 + +安装 `ifconfig` 工具: + +```shell +apt install net-tools +``` + +查看 IP 地址: + +```shell +ifconfig +``` + +![image-20250310130413226](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20250310130413226.png) + +#### 2. 端口转发 + +在 PowerShell 中执行,将 `[IP]` 和 `[PORT]` 替换为 WSL 的 IP 和 SSH 端口: + +```powershell +netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=2222 connectaddress=[IP] connectport=[PORT] +``` + +--- + +## 启用 systemctl + +进入当前发行版,编辑 `/etc/wsl.conf`: + +```shell +vim /etc/wsl.conf +``` + +内容如下: + +```shell +[boot] +systemd=true +``` + +重启 WSL: + +```powershell +wsl --shutdown +``` + +--- + +## 取消密码复杂度及长度限制 + +编辑 `/etc/pam.d/system-auth`: + +```shell +vim /etc/pam.d/system-auth +``` + +修改密码要求: + +```shell +password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= minlen=6 ucredit=1 lcredit=1 ocredit=1 dcredit=1 +``` + +![image-20250310125937362](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20250310125937362.png) + +--- + +## WSL 玄学 Bug:SSH 连接找不到 nvidia-smi + +本地正常,但通过 SSH 连接 WSL 时执行 `nvidia-smi` 找不到。 + +### 解决方式 + +在 `.bashrc` 中加入: + +```shell +export PATH=/usr/lib/wsl/lib:$PATH +``` + +> 参考:[https://github.com/microsoft/WSL/issues/8794](https://github.com/microsoft/WSL/issues/8794) + +--- + +## 常用命令 + +```powershell +# 查看所有分发版 +wsl -l --all + +# 查看正在运行的分发版 +wsl -l --running + +# 设置默认分发版 +wsl -s + +# 启动指定分发版 +wsl -d + +# 关闭指定分发版 +wsl -t + +# 关闭所有分发版 +wsl --shutdown + +# 卸载分发版 +wsl --unregister + +# 更新 WSL +wsl --update + +# 查看状态 +wsl --status + +# 查看版本 +wsl --version +``` \ No newline at end of file