Linux 常用命令

266次阅读
没有评论

ubuntu 系统换中科大源

说明 https://mirrors.ustc.edu.cn/help/ubuntu.html
需要注意的是,自 Ubuntu 24.04 起,ubuntu 使用 DEB822 配置源,和以前不一样了。

# ubuntu24.04
sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list.d/ubuntu.sources
sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/ubuntu.sources

# 之前的版本
sed -i "s@http://.*archive.ubuntu.com@http://mirrors.ustc.edu.cn@g" /etc/apt/sources.list
sed -i "s@http://.*security.ubuntu.com@http://mirrors.ustc.edu.cn@g" /etc/apt/sources.list

生成 ssh 秘钥

rsa 比较通用,现在推荐 4096 位的才能保证安全,未来可能会被量子计算机轰成渣。
ed25519 更快更安全,抗量子计算机攻击,github 推荐使用。

ssh-keygen -t ed25519 -C "your_email@example.com"
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

linux 使用秘钥登录

把公钥写入下列文件即可

 vi  ~/.ssh/authorized_keys

安装 docker

对于 ubuntu,可安装 ubuntu 官方维护的 docker,但它不是最新版本,并且不带 buildx 等插件。

apt install docker.io

对于所有支持的 linux 发行版,可以用官方安装脚本。

export DOWNLOAD_URL="https://mirrors.tuna.tsinghua.edu.cn/docker-ce"
# 如您使用 curl
curl -fsSL https://get.docker.com/ | sh
# 如您使用 wget
wget -O- https://get.docker.com/ | sh

docker 免 sudo

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

docker 镜像加速器

已团灭,直接科学上网
可能是因为 docker 镜像数据量太大,并且 docker 官方还会对 ip 限速,很多加速器都在服务一段时间后停服,或者停止同步,这里找到了一个项目在收集可用加速器。
https://gist.github.com/y0ngb1n/7e8f16af3242c7815e7ca2f0833d3ea6
个人感觉 nju 的能用,但对于冷门资源速度远不如直接科学上网

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://docker.nju.edu.cn"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

github action self-host runner

 允许以 root 配置
RUNNER_ALLOW_RUNASROOT=1 ./config.sh --token xxxxxxx
以 root 用户注册服务
sudo ./svc.sh install root

文件搜索

2>/dev/null 这是错误输出的重定向, 用于屏蔽错误信息

 find / -name xxxx 2>/dev/null

添加 swap

dd if=/dev/zero of=/swap bs=1024 count=8192000  # 8G
mkswap /swap
chmod 0600 /swap
swapon /swap
# 修改自动挂载
vi /etc/fstab
/swap swap swap default 0 0

修改 swap 大小

假设已经按上边添加了 swap,想换成更大的。

sudo swapoff /swap
sudo dd if=/dev/zero of=/swap bs=1M count=1024
sudo chmod 0600 /swap
sudo mkswap /swapfile
swapon /swap

# fstab 无需修改 

swap 策略

vm.swappiness 是 Linux 内核中的一个参数,用于控制系统将内存页交换到交换空间(swap)的倾向。它的取值范围是 0 到 100,0 是不用 swap,100 是最积极使用。

# 获取当前的值,一般默认 60
sysctl vm.swappiness
# 临时修改
sysctl vm.swappiness=60
# 永久修改需要改配置文件
vi /etc/sysctl.conf
vm.swappiness=60
# 重启后生效,或者用这个立即生效
sudo sysctl -p

修改时区

timedatectl set-timezone Asia/Shanghai

git 克隆

祖传代码,仓库只克隆表层, 克隆子模块, 子模块只克隆表层

git clone --recurse-submodules --shallow-submodules --depth=1 <repository-url>

创建 tar.gz 时候不记录修改时间

大量小文件压缩时显著减小体积

tar --mtime="1970-01-01" -czvf files.tar.gz  files

python 切换国内源

遇到过清华源对 ip 限速,下载超大 whl 时报 403,这里用阿里源

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple

anaconda 基本命令

conda create --name myenv python=3.8
conda activate myenv
conda deactivate
conda env list
conda remove --name myenv --all
 0