Skip to content
Happyboat's Blog
Go back

Linux使用

Updated:
Edit page

Table of contents

Open Table of contents

进程管理

ps aux | grep NAME
ps -ef
pstree
pgrep -a NAME
top # 进去之后按/搜索
htop # 进去之后按F3搜索
kill PID #SIGTERM 15 礼貌地请进程退出
kill -9 PID #SIGKILL 9 强制关闭
pkill NAME

硬盘与存储

# 查看信息
df -h
du -sh *
du -sh /folder
du -h --max-depth=1 | sort -hr
lsblk
sudo fdisk -l

# 挂载
lsblk -f
sudo mkdir -p /mnt/usb_disk
sudo mount -t ntfs-3g /dev/sdX1 /mnt/usb_disk
# or sudo mount -t exfat /dev/sdX1 /mnt/usb_disk
sudo umount /mnt/usb_disk

日常杂项

# 打包并压缩(tar.gz)
tar -czvf archive.tar.gz /path/to/dir
# 解压 tar.gz
tar -xzvf archive.tar.gz

# 压缩目录(必须用 -r)
zip -r archive.zip /path/to/directory
# 解压到当前目录
unzip archive.zip

scp -r user@remote_ip:/home/user/my_folder  ./

sudo powertop
watch nvidia-smi
# 秒数 -n 1
# 高亮变化 -d
# 不显示标题 -t

pwd #print working directory 打印当前目录 
wget -c https://demo #断点续传  指定目录 -P /mnt/disk1/a/aa
ln -s /path/to/target link_name #软链接
fc-cache -fv #刷新字体设置

git

git init  # 在当前目录初始化一个新的 Git 仓库(生成 .git 目录)
git clone <url>  # 克隆远程仓库到本地
git config --global user.name "你的名字"  # 配置提交者姓名
git config --global user.email "你的邮箱"  # 配置提交者邮箱

git status  # 查看当前工作区状态(哪些文件修改了但未暂存)
git add <file>  # 将文件添加到暂存区
git add .  # 将所有修改添加到暂存区
git commit -m "提交信息"  # 将暂存区内容提交到本地仓库
git commit --amend  # 修改上一次提交(原理:产生一个新的 commit 对象替换旧的)

git branch  # 列出所有本地分支
git branch <name>  # 创建新分支
git checkout <name>  # 切换到指定分支(现代做法使用 git switch <name>)
git checkout -b <name>  # 创建并切换到新分支
git merge <name>  # 将指定分支合并到当前分支
git branch -d <name>  # 删除已合并的分支

git remote add origin https://github.com/用户名/仓库名.git  # 添加远程映射
git remote -v  # 查看远程仓库地址
git fetch  # 拉取远程更新但不合并
git pull  # 拉取远程更新并尝试自动合并(等于 fetch + merge)
git push origin <branch>  # 将本地分支推送到远程

SSH

服务器免密登录

ssh-keygen -t ed25519 #-f 指定名字,也可以直接生成,后续可以指定
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@你的服务器IP

本地编辑 ~/.ssh/config

Host myvps
    HostName 
    User root
    IdentityFile ~/.ssh/id_ed25519

tmux

tmux new -s demo  # 新建
tmux ls  # 列表
exit  # 退出 或 Ctrl+b 再 d 
tmux attach -t demo  # 接回
# 关闭 Ctrl+b 再 x

Python环境

Conda

# --- 环境管理 ---
conda create -n myenv python=3.10        # 创建环境
conda env create -n myenv -f environment.yml  # 从配置创建环境
conda activate myenv                     # 激活环境
conda deactivate                         # 退出环境
conda env list                           # 查看环境
conda remove -n myenv --all              # 删除环境
conda env remove -n myenv -y             # 删除环境

# --- 包管理 ---
conda install numpy                      # 安装包
conda install -c conda-forge numpy       # 指定源安装
conda update numpy                       # 更新包
conda remove numpy                       # 卸载包
conda list                               # 查看已装包
conda search numpy                       # 搜索包

# --- 导入导出 ---
conda env export > environment.yml       # 导出环境
conda env export --from-history > environment.yml  # 只导出手动安装项
conda env update -f environment.yml      # 更新环境

# --- 配置与清理 ---
conda config --set auto_activate_base false  # 关闭自动进入 base
conda config --show-sources              # 查看配置来源
conda clean --all                        # 清理缓存
conda clean -i                           # 清理索引缓存

# --- 中国大陆镜像源:清华源 ---
conda config --set show_channel_urls yes # 显示源地址
conda config --set default_channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --append default_channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --append default_channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
conda config --set custom_channels.conda-forge https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
conda config --set custom_channels.pytorch https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
conda clean -i                           # 换源后刷新索引

# --- 恢复默认源 ---
conda config --remove-key default_channels
conda config --remove-key custom_channels
conda clean -i

venv

# --- 环境管理 ---
python -m venv .venv                     # 创建虚拟环境
source .venv/bin/activate                # Linux/macOS 激活
# .venv\Scripts\activate                 # Windows CMD 激活
# .venv\Scripts\Activate.ps1             # Windows PowerShell 激活
deactivate                               # 退出环境
rm -rf .venv                             # 删除环境

# --- 包管理 ---
python -m pip install numpy==1.26.4      # 指定版本
python -m pip install -U numpy           # 更新包
python -m pip uninstall numpy            # 卸载包
python -m pip list                       # 查看已装包
python -m pip show numpy                 # 查看包信息
python -m pip check                      # 检查依赖冲突

# --- requirements 管理 ---
python -m pip freeze > requirements.txt  # 导出依赖
python -m pip install -r requirements.txt  # 安装依赖
python -m pip install -r requirements.txt -U  # 更新安装

# --- pip 自身管理 ---
python -m pip install -U pip             # 更新 pip
python -m pip cache purge                # 清理缓存
python -m pip config list                # 查看 pip 配置
python -m pip config debug               # 查看配置文件位置

# --- 中国大陆镜像源:清华源 ---
python -m pip install -U pip             # 先更新 pip
python -m pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple  # 设为默认源
# --- 恢复默认源 ---
python -m pip config unset global.index-url

uv

# --- 安装与更新 ---
curl -LsSf https://astral.sh/uv/install.sh | sh  # 安装 uv
uv self update                            # 更新 uv
uv --version                              # 查看版本

# --- Python 版本管理 ---
uv python list                            # 查看可用 Python
uv python install 3.10                    # 安装 Python 3.10
uv python pin 3.10                        # 项目固定 Python 版本

# --- 项目管理 ---
uv init myproj                            # 创建项目
cd myproj                                 # 进入项目
uv init                                   # 初始化当前目录
uv sync                                   # 同步依赖到环境
uv lock                                   # 生成/更新锁文件
uv run python main.py                     # 在项目环境运行
uv run pytest                             # 运行命令

# --- 环境管理 ---
uv venv                                   # 创建 .venv
uv venv --python 3.10                     # 指定 Python 创建 .venv
source .venv/bin/activate                 # Linux/macOS 激活
# .venv\Scripts\activate                  # Windows CMD 激活
# .venv\Scripts\Activate.ps1              # Windows PowerShell 激活
deactivate                                # 退出环境
rm -rf .venv                              # 删除环境

# --- 项目依赖管理 ---
uv add numpy                              # 添加依赖
uv add "numpy==1.26.4"                    # 添加指定版本
uv add --dev pytest                       # 添加开发依赖
uv remove numpy                           # 移除依赖
uv tree                                   # 查看依赖树

# --- 兼容 pip 的用法 ---
uv pip install numpy                      # 安装包
uv pip install -r requirements.txt        # 从 requirements 安装
uv pip list                               # 查看已装包
uv pip freeze > requirements.txt          # 导出依赖
uv pip uninstall numpy                    # 卸载包

# --- 工具管理 ---
uvx ruff check .                          # 临时运行工具
uv tool install ruff                      # 全局安装工具
uv tool list                              # 查看工具
uv tool uninstall ruff                    # 卸载工具

# --- 缓存清理 ---
uv cache clean                            # 清理缓存
uv cache dir                              # 查看缓存目录

# --- 中国大陆镜像源:清华源,用户级配置 ---
mkdir -p ~/.config/uv                     # 创建配置目录

cat > ~/.config/uv/uv.toml <<'EOF'
[[index]]
url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/"
default = true
EOF

# --- 临时使用镜像源 ---
uv pip install numpy --default-index https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/  # 单次使用
uv add numpy --default-index https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/          # 单次使用

# --- 恢复默认源 ---
rm ~/.config/uv/uv.toml                   # 删除 uv 用户配置

Huggingface

export HF_ENDPOINT=https://hf-mirror.com

Autodl

apt update && apt install tmux
source /etc/network_turbo

# 下载并解压
wget https://github.com/ox01024/cmirror/releases/latest/download/cmirror-linux-x64.tar.gz
tar -xzf cmirror-linux-x64.tar.gz

# 添加执行权限
chmod +x cmirror

# 移动到系统路径(可选)
sudo mv cmirror /usr/local/bin/

# 验证安装
cmirror --help

unset http_proxy && unset https_proxy

cmirror use pip --fastest
cmirror use conda --fastest

Edit page

Previous Post
操作系统笔记整理
Next Post
日常工具推荐