Skip to content

linux

plantuml常用汇总

linux 上中文文字层叠现象

ubuntu

sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy

centos

sudo yum install -y wqy-microhei-fonts
sudo yum install -y adobe-source-han-sans-cn-fonts

测试

400. PlantUML 1.2025.3beta4[From string (line 3) ] @startuml<style>root {--common-background: #f1f1f1;--note-background: #FEFFDD;...... ( skipping 485 lines )...BackgroundColor: var(--grey-blue);FontSize 12}server {FontSize 12}group {FontSize 12BackGroundColor #e7e7e7}arrow {FontSize 11}} .person, .system, .container, .component, .external_person, .external_system, .external_container {HorizontalAlignment center}</style>listfonts 你好Syntax Error? (Assumed diagram type: sequence)
400. PlantUML 1.2025.3beta4[From string (line 3) ] @startuml<style>root {FontName SansSerif  HyperLinkUnderlineThickness 1...... ( skipping 548 lines )...BackGroundColor #5E5E5ELineColor #5E5E5E} .external_system {BackGroundColor #8F8F8FLineColor #8F8F8F} .external_container {BackGroundColor #8B8B8BLineColor #8B8B8B} .node {BackgroundColor transparentFontColor white}</style>listfonts 你好Syntax Error? (Assumed diagram type: sequence)

字体

linux 字体安装

流程

1.将字体下载并拷贝到对应文件夹中,并进入字体文件夹

cp *.ttf /usr/share/fonts
cd /usr/share/fonts

2.字体安装器

sudo apt install ttf-mscorefonts-installer 
# (创建字体的fonts.scale文件,它用来控制字体旋转缩放)
sudo mkfontscale

# (创建字体的fonts.dir文件,它用来控制字体粗斜体产生)
sudo mkfontdir

3.建立字体缓存

sudo apt install fontconfig

# 建立字体缓存信息
sudo fc-cache -fv

# 查看已经安装的
fc-list

ohmyzsh 安装

参考

https://github.com/ohmyzsh/ohmyzsh

插件参考

https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins
  • 第三方插件

1.zsh-autosuggestions 自动补全插件

git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions

2.高亮

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

3.autojump

sudo apt-get install autojump
  • 我的插件配置
plugins=(
        git
        z
        autojump
        zsh-autosuggestions
        zsh-syntax-highlighting
        sudo
)

主题参考

https://github.com/ohmyzsh/ohmyzsh/wiki/Themes

第三方主题参考

powerlevel10k

https://github.com/romkatv/powerlevel10k

centos7 git 环境配置

安装最新git

如果使用 EPEL 存储库中的 dnf 安装 Git 仍然无法得到最新版本的 Git,您可以考虑从源代码编译安装 Git。以下是在 CentOS 7 上编译安装最新版本 Git 的步骤:

  1. 打开终端并使用 root 用户登录系统。

  2. 安装必需的编译工具和依赖项:

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker -y
  1. 前往 Git 官网的下载页面(https://git-scm.com/downloads) 并找到最新版本的 Git。复制该版本的下载链接。

  2. 在终端中使用以下命令下载 Git 的源代码压缩包(请将命令中的 URL 替换为第 3 步中复制的下载链接):

curl -LO https://github.com/git/git/archive/refs/tags/v2.40.1.tar.gz
  1. 使用以下命令解压缩源代码压缩包:
tar xzf *.gz
  1. 进入解压后的 Git 源代码目录:
cd git-*
  1. 使用以下命令编译和安装 Git:
make prefix=/usr/local/git all
make prefix=/usr/local/git install
  1. 将 Git 添加到 PATH 环境变量中:
echo 'export PATH=$PATH:/usr/local/git/bin' >> ~/.bashrc
source ~/.bashrc
  1. 使用以下命令验证 Git 是否已成功安装:
git --version

如果 Git 版本号显示为最新版本,则表示 Git 已经成功安装在 CentOS 7 上。请注意,由于 CentOS 7 默认使用较旧的软件版本,因此您可能需要升级其他依赖项来满足 Git 的要求。如果在编译过程中遇到任何问题,请检查错误消息并尝试解决它们。

git

git 环境

windows git 上有分支名称,一下加入可让 linux 也支持分支名称

vim ~/.bashrc

  • 方案1:
function git-branch-name {
  git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3
}
function git-branch-prompt {
  local branch=`git-branch-name`
  if [ $branch ]; then printf " [%s] " $branch; fi
}

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(git-branch-prompt)\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(git-branch-prompt)\$ '
fi
  • 方案2:

git env

显示分支名称

parse_git_branch() {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

PS1="\[\033[36m\]\u@\h\[\033[m\]:\[\033[32m\]\w\[\033[m\]\$(parse_git_branch)\$ "

配色

配置文件

vim /home/[username]/.gitconfig

[color]
    status = auto
    diff = auto
    ui = true
    interactive = auto
    branch = auto

修改后立马生效

命令设置

git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.ui true
git config --global color.interactive auto

1 远程操作

1.1 获取远程仓库地址信息

git remote -v

1.2 本地新建项目情况

本地建立仓库,远程后建立,拉取时会失败,使用以下命令强制拉取

git pull <远程仓库地址> -allow-unrelated-histories 

1.3 更新当前分支远程仓库分支地址

git branch --set-upstream-to=origin/<远程分支名称>

1.4 删除远程仓库

git push origin --delete origin/<远程分支名称>

1.5 远程分支回滚

  1. 要回滚的版本号
git reset --hard <commit> 
  1. 提交回退
// 方式1
git push origin HEAD --force   
// 方式2
git push --force

1.6 回滚作为一个更改项,避免已提交到远程分支后同步造成冲突

git revert <commit>     // 回退作为一个动作保留
git push                // 这样作为一个同步提交不会造成服务器的冲突了

1.7 更新远程仓库地址

git remote set-url origin 【url】

或者

git remote remove origin
git remote add origin【url】

1.8 克隆单个分支

git clone -b <branch> <remote_repo>

2 本地操作

2.1 拉取其他仓库所有分支作自己分支

fetch <仓库路径>

2.2 当前分支获取其他分支某个提交的改动

git cherry-pick <版本号> # 也可以是分支名称

3 打标签

3.1 显示标签
$ git tag
3.2 创建标签
$ git tag -a v1.0 -m "xxx"
3.3 显示标签所有前缀标签

```\ $ git tag -l "v1.0*"

#### 3.4 显示对应标签所有信息
$ git show v1.0
#### 3.5 删除标签
$ git tag -d v1.0
## git submodule

### 添加子模块

```sh
git submodule add <submodule> <path>

拉取整个项目

git submodule update --init --recursive

删除子模块

git submodule deinit <path>
git rm <path>

如果已经update 了,还要删除 .git里面module的对应内容

更换子模块

先删除再添加

  1. git submodule deinit -f {submodule_name}
  2. 删除 .gitsubmodule 中对应条目
  3. rm -rf .git/modules/{submodule_name}
  4. 如果 .git/config 中有对应信息需删除
  5. 添加新的子模块

更新子模块路径

git 1.8.5 之后新增, 建议先删除再新增子模块

git mv {oldPath} {newPath}
# 如果要更换submodule 名称与 path 一致
mv .git/modules/{oldPath} .git/modules/{newPath}

nvs环境配置

nvs 详细安装可以去 github:

https://github.com/jasongin/nvs

windows

gitbase 如果找不到 nvs

如果使用 git base 环境, 在 ~/.bash_profile 加入以下命令

export NVS_HOME=$LOCALAPPDATA/nvs
. $NVS_HOME/nvs.sh

linux

wsl 解决与windows中 nvs 冲突

如果为wsl环境, 如果windows中无nvs环境可跳过此步骤

vim ~/.bashrc

在文件末尾加入,排出windows nvs 环境变量, 具体路径参考自己 windows 的 nvs 环境

export PATH=$(echo $PATH | sed -e 's;:/mnt/c/Users/<user>/AppData/Local/nvs/default;;' -e 's;:/mnt/c/Users/<user>/AppData/Local/nvs/;;')

公共配置

解决全局模块在切换版本后需要重装问题

mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global

推荐直接使用 pnpm 替代

pnpm config set prefix ~/.npm-global

配置到环境变量到 ~/.bashrc, windows类似在系统配置中添加

export PATH=~/.npm-global/bin:$PATH

配置镜像地址

查看 remote

nvs remote

配置镜像地址

nvs remote node https://npmmirror.com/mirrors/node/

解决linux内存不足引起的问题

线上linux内存不足导致无法ssh连接

  • 线上linux服务器未配置虚拟内存,之后运行 pnpm i 都会直接卡死

解决方式: 1. 配置虚拟内存 2. 调整内核参数

调整内核参数解决

在 Linux 系统中,可以通过调整内核参数来加快内存回收,从而提高系统的性能和响应速度。以下是一些常用的内核参数及其调整方法:

  1. vm.swappiness vm.swappiness 参数控制系统使用 Swap 的倾向。值越低,系统越倾向于保留内存而不是将数据换出到 Swap。

默认值:60 调整建议: 降低值:如果希望系统尽量减少使用 Swap,可以将 vm.swappiness 设置为较低的值,例如 10 或 20。 提高值:如果希望系统更积极地使用 Swap,可以将 vm.swappiness 设置为较高的值,例如 80 或 100。

# 临时调整
sudo sysctl vm.swappiness=10

# 永久调整(编辑 /etc/sysctl.conf)
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
  1. vm.vfs_cache_pressure vm.vfs_cache_pressure 参数控制文件系统缓存的回收压力。值越高,系统越倾向于回收文件系统缓存。

默认值:100 调整建议: 降低值:如果希望系统保留更多的文件系统缓存,可以将 vm.vfs_cache_pressure 设置为较低的值,例如 50。 提高值:如果希望系统更积极地回收文件系统缓存,可以将 vm.vfs_cache_pressure 设置为较高的值,例如 150。

# 临时调整
sudo sysctl vm.vfs_cache_pressure=50

# 永久调整(编辑 /etc/sysctl.conf)
echo "vm.vfs_cache_pressure=50" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
  1. vm.dirty_ratio 和 vm.dirty_background_ratio 这两个参数控制脏页(未同步到磁盘的缓存数据)的数量。

vm.dirty_ratio:脏页占总内存的最大百分比。超过此值时,系统会开始同步脏页到磁盘。

vm.dirty_background_ratio:脏页占总内存的背景阈值。超过此值时,系统会开始异步同步脏页到磁盘。

默认值:

vm.dirty_ratio:20 vm.dirty_background_ratio:10 调整建议:

降低值:如果希望系统更频繁地同步脏页,可以将这两个值设置为较低的值,例如 10 和 5。 提高值:如果希望系统减少同步频率,可以将这两个值设置为较高的值,但需谨慎,以免影响系统稳定性。

# 临时调整
sudo sysctl vm.dirty_ratio=10
sudo sysctl vm.dirty_background_ratio=5

# 永久调整(编辑 /etc/sysctl.conf)
echo "vm.dirty_ratio=10" | sudo tee -a /etc/sysctl.conf
echo "vm.dirty_background_ratio=5" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
  1. vm.drop_caches vm.drop_caches 参数可以手动清除各种类型的缓存,包括页面缓存、目录项缓存和inode缓存。

值: 1:清除页面缓存 2:清除目录项和inode缓存 3:清除所有缓存

# 清除所有缓存
sudo sync
echo 3 | sudo tee /proc/sys/vm/drop_caches
  1. vm.overcommit_memory 和 vm.overcommit_ratio 这两个参数控制内存过量分配的行为。

vm.overcommit_memory:

0:内核根据可用内存和当前使用情况决定是否允许内存过量分配(默认值)。 1:内核总是允许内存过量分配。 2:内核根据 vm.overcommit_ratio 计算允许的最大内存分配量。 vm.overcommit_ratio:当 vm.overcommit_memory 设置为 2 时,此参数表示允许的最大内存分配量占物理内存的百分比。

默认值:

vm.overcommit_memory:0 vm.overcommit_ratio:50 调整建议:

设置为 2:如果希望严格控制内存分配,可以将 vm.overcommit_memory 设置为 2,并调整 vm.overcommit_ratio。

# 临时调整
sudo sysctl vm.overcommit_memory=2
sudo sysctl vm.overcommit_ratio=70

# 永久调整(编辑 /etc/sysctl.conf)
echo "vm.overcommit_memory=2" | sudo tee -a /etc/sysctl.conf
echo "vm.overcommit_ratio=70" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

挂载

挂载新硬盘

1.查看硬盘

root@ubuntu:~# fdisk -lu
Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x3c78b7a1

Device     Boot    Start      End  Sectors Size Id Type
/dev/sda1  *        2048 16777215 16775168   8G 83 Linux
/dev/sda2       16779262 20969471  4190210   2G  5 Extended
/dev/sda5       16779264 20969471  4190208   2G 82 Linux swap / Solaris

Partition 2 does not start on physical sector boundary.


Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

2.创建分区

root@ubuntu:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xdf96fde2.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-104857599, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599):

Created a new partition 1 of type 'Linux' and of size 50 GiB.

Command (m for help): p
Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xdf96fde2

Device     Boot Start       End   Sectors Size Id Type
/dev/sdb1        2048 104857599 104855552  50G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

3.格式化分区

root@ubuntu:~# mkfs.ext4 /dev/sdb1
mke2fs 1.42.13 (17-May-2015)
Discarding device blocks: 完成
Creating filesystem with 13106944 4k blocks and 3276800 inodes
Filesystem UUID: c0887faa-eed2-4f38-9f18-6e3059261202
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424

Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

4.挂载和查看分区表

root@ubuntu:~# mount /dev/sdb1 /data
root@ubuntu:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            2.0G     0  2.0G   0% /dev
tmpfs           395M  5.5M  389M   2% /run
/dev/sda1       7.8G  1.2G  6.2G  16% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
tmpfs           395M     0  395M   0% /run/user/0
/dev/sdb1        50G   52M   47G   1% /data

5.永久挂载

# 执行前一定要进行备份,以免误操作
echo '/dev/sdb1       /data   ext4    defaults        0       0' >> /etc/fstab

映射硬盘

eg: 将 docker lib 目录映射到 /data/lib/docker

echo '/data/lib/docker /var/lib/docker none defaults,bind 0 0'>>/etc/fstab

fstab 立即生效

mount -a

linux信息

查看系统版本

# 打印系统信息
$ uname -a
Linux LAPTOP-2181LTT2 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

# 获取发行版信息
$ cat /etc/issue
Ubuntu 18.04.6 LTS \n \l

$ cat /etc/issue.net 
Ubuntu 18.04.6 LTS

$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.6 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

# 根据上面 ID_LIKE 来获取version
$ cat /etc/debian_version
9.3

# 也可直接通过system 获取(如果有)
$ cat /etc/system-release
9.3

# 获取内核版本
$ cat /proc/version 
Linux version 5.4.72-microsoft-standard-WSL2 (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Wed Oct 28 23:40:43 UTC 2020

# dmesg 命令
$ dmesg | grep "Linux"
[    0.000000] Linux version 5.4.72-microsoft-standard-WSL2 (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Wed Oct 28 23:40:43 UTC 2020
[    3.522133] ACPI: Added _OSI(Linux-Dell-Video)
[    3.527064] ACPI: Added _OSI(Linux-Lenovo-NV-HDMI-Audio)
[    3.531431] ACPI: Added _OSI(Linux-HPI-Hybrid-Graphics)

# 其他 Yum/Dnf
$ yum info nano
$ yum repolist
$ dnf info nano
$ apt-cache policy nano
nano:
  Installed: 2.9.3-2
  Candidate: 2.9.3-2
  Version table:
 *** 2.9.3-2 500
        500 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages
        100 /var/lib/dpkg/status

nginx配置

nginx 配置单机器多域名

1.conf.d/default.conf 默认域名,若没命中,进入此处

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html/default;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

1.conf.d/www.my.com.conf www.my.com 和 my.com 进入会命中

server {
    listen       80;
    listen  [::]:80;
    server_name  www.my.com my.com;

    location / {
        root   /usr/share/nginx/html/my;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

常用功能

文件服务器

server {
    listen 80;  # 监听 HTTP 请求的端口,可以根据需要更改
    server_name files.localhost;  # 你的服务器名称或 IP 地址

    location / {
        alias /files/;  # 指定文件存储目录
        autoindex on;  # 启用自动列出目录内容
        autoindex_exact_size off;  # 显示文件大小的近似值
        charset utf-8;
    }
}

basic auth

  • htpasswd文件生成
printf "test:$(openssl passwd -crypt 123456)\n" >> /usr/local/nginx-1.5.1/conf/basicauth
  • 使用
server{
    listen 80;
    server_name  auth.localhost;

    location / {
        root   html;
        auth_basic "Please enter your username and password";
        auth_basic_user_file ../conf/basicauth; 
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}