从零开始构建 Linux 开发环境
我的 Linux 配置
一些常用文件的默认位置
zsh 配置文件:~/.zshrc
vscode server:~/.vscode-server
客户端配置 SSH 免密登录
在客户端 .ssh
目录中,打开 bash 配置私钥
将客户端的公钥放到服务端上
1
| cat ~/.ssh/id_rsa.pub | ssh username@ip 'cat >> ~/.ssh/authorized_keys'
|
⭐ 用 ssh-copy-id
命令更方便
1
| ssh-copy-id username@ip
|
此时,客户端的公钥会被添加到远程服务器上的 ~/.ssh/authorized_keys
文件中
然后就可以使用私钥登录啦~
1
| ssh username@ip [-p port] -i id_rsa
|
Windows Terminal 配置
字体选择
https://www.nerdfonts.com/font-downloads 随便挑
推荐:
- CaskaydiaCove Nerd Font Mono(或者叫 CaskaydiaCove NFM)
- Hack Nerd Font,选 HackNerdFontMono-Regular.ttf
网络配置
镜像源
可选,未经验证!
备份原有软件源文件
1
| sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak_yyyymmdd
|
修改 sources.list 文件
1
2
| # 修改为如下地址:
sudo vi /etc/apt/sources.list
|
1
2
3
4
5
6
7
8
9
10
11
| # 163源
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
|
更新系统软件源
1
2
3
| # 执行命令,更新系统软件源地址:
sudo apt-get update
sudo apt-get upgrade
|
1
2
3
4
| # 安装lrzsz tree
sudo apt-get -y install lrzsz tree
# 安装 build-essential 软件包集合 (其中就包括 gcc、G ++ 和 make 等)
sudo apt install build-essential
|
代理
在 Windows 中的 C:\Users<your_username> 目录下创建一个 .wslconfig 文件,然后在文件中写入如下内容
1
2
3
4
5
6
| [experimental]
autoMemoryReclaim=gradual
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true
|
然后用 wsl --shutdown
关闭 WSL,之后再重启
zsh 配置
更改默认 shell 为 zsh
1
2
3
4
5
6
| # 查看系统当前使用的shell
echo $SHELL
# 如果没有zsh,那就下一个!
sudo apt-get install zsh
# 设置默认zsh
chsh -s /bin/zsh
|
oh-my-zsh 配置
安装 oh-my-zsh
https://github.com/ohmyzsh/ohmyzsh/wiki
1
| sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
|
安装 Powerlevel10k 主题
https://github.com/romkatv/powerlevel10k#installation
1
2
| git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/.opt/powerlevel10k
echo 'source ~/.opt/powerlevel10k/powerlevel10k.zsh-theme' >>~/.zshrc
|
source ~/.zshrc
,就会进入 P10k 的配置界面
若要重置 P10k 配置,运行 p10k configure
或者直接修改 ~/.p10k.zsh
这是手动安装方案,所以不需要在 ~/.zshrc
里配置 ZSH_THEME="powerlevel9k/powerlevel9k"
这句话
更改配置文件 .zshrc
内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| # Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
export PATH=$PATH:~/.local/bin/:/snap/bin
# ZSH_THEME="random"
plugins=(
git
extract
rand-quote
themes
z
per-directory-history
history-substring-search
command-not-found
safe-paste
colored-man-pages
sudo
zsh-autosuggestions
# history
zsh-syntax-highlighting
copypath
copyfile
you-should-use
)
source $ZSH/oh-my-zsh.sh
alias :vrc="vi ~/.zshrc"
alias :src="source ~/.zshrc"
alias -s ttt='tar zxvf'
alias cp="cp -i" # 防止 copy 的时候覆盖已存在的文件, 带上 i 选项,文件已存在的时候,会提示,需要确认才能 copy
alias gcm='git checkout master'
# 以自己输入的所有内容为前缀,进行历史查找
bindkey '^P' history-substring-search-up
bindkey '^N' history-substring-search-down
# 让 history 命令显示时间
HIST_STAMPS="yyyy-mm-dd"
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
source ~/.opt/powerlevel10k/powerlevel10k.zsh-theme
|
此配置文件会根据文档的进展而逐渐完善
一些插件的下载
zsh-autosuggestions
1
| git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
|
zsh-syntax-highlighting
1
2
3
4
| sudo apt install zsh-syntax-highlighting
cd ~/.opt
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
|
zsh-history-substring-search
1
| git clone https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-search
|
zsh-you-should-use
1
| git clone https://github.com/MichaelAquilina/zsh-you-should-use.git $ZSH_CUSTOM/plugins/you-should-use
|
git 配置
ssh-keygen 一下叭
1
2
| cd ~/.ssh
ssh-keygen -t rsa
|
将 id_rsa.pub
公钥内容,添加到 github 之类的托管平台上
设置名字,邮箱
1
2
| git config --global user.name "Your Name"
git config --global user.email "email@example.com"
|
Vim 配置
安装 NeoVim
1
| sudo snap install --beta nvim --classic
|
或者这样
1
2
3
4
| # 不要直接用 apt install neovim,版本很老!
sudo add-apt-repository ppa:neovim-ppa/unstable
sudo apt-get update
sudo apt-get install neovim
|
在 ~/.zshrc
中添加别名,以替换默认的 vim 命令
1
2
| alias vim='nvim'
alias vi='nvim'
|
使用个人配置(基于 LazyVim)
1
2
| git clone git@github.com:Trouvaille0198/neovim-config.git ~/.config/nvim
rm -rf ~/.config/nvim/.git
|
运行 nvim
,等待插件下载完成
添加 snippets
工具安装
snap
包管理器,你总会用到的
1
| sudo apt install snapd -y
|
deb-get
包管理器,你总会用到的
1
| curl -sL https://raw.githubusercontent.com/wimpysworld/deb-get/main/deb-get | sudo -E bash -s install deb-get
|
bat
cat 的美化替代品
使用软链接
1
2
| mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/bat
|
==一切的软链接都装在 ~/.local==
并在配置中添加
exa
ls 的替代品
在配置中添加
项目地址:https://github.com/sharkdp/fd
fd
一个命令行搜索工具
1
| sudo apt install fd-find
|
使用软链接
1
| ln -s $(which fdfind) ~/.local/bin/fd
|
bashtop
https://github.com/aristocratos/bashtop
top 的替代品
1
2
3
| sudo add-apt-repository ppa:bashtop-monitor/bashtop
sudo apt update
sudo apt install bashtop
|
ifconfig
1
| sudo apt install net-tools
|
cheat
一本常用命令说明书:https://github.com/cheat/cheat
1
2
3
4
5
6
7
8
9
10
11
| # cd /tmp \
# && wget https://github.com/cheat/cheat/releases/download/4.4.0/cheat-linux-amd64.gz \
# && gunzip cheat-linux-amd64.gz \
# && chmod +x cheat-linux-amd64 \
# && sudo mv cheat-linux-amd64 /usr/local/bin/cheat
cd ~/.opt \
&& wget https://github.com/cheat/cheat/releases/download/4.4.0/cheat-linux-amd64.gz \
&& gunzip cheat-linux-amd64.gz \
&& chmod +x cheat-linux-amd64 \
&& ln -s ~/.opt/cheat-linux-amd64 ~/.local/bin/cheat
|
👆 最新版本号随时从官方仓库看
dust
GitHub - bootandy/dust: A more intuitive version of du in rust
du 替代
1
| deb-get install du-dust
|
duf
GitHub - muesli/duf: Disk Usage/Free Utility - a better ‘df’ alternative
df 替代
gping
GitHub - orf/gping: Ping, but with a graph
gui ping
procs
GitHub - dalance/procs: A modern replacement for ps written in Rust
ps 替代
httpie
Snapcraft (Linux) - HTTPie 3.2.2 (latest) docs
curl 替代
curlie
GitHub - rs/curlie: The power of curl, the ease of use of httpie.
又一个 curl 的替代,保留了 curl 的语法
1
| curl -sS https://webinstall.dev/curlie | bash
|
其他
1
2
3
4
| sudo apt install unzip
sudo apt install build-essential # gcc etc
sudo apt install libedit-dev liblzma-dev libreadline-dev libffi-dev
sudo apt install zlib1g zlib1g-dev libssl-dev libbz2-dev libsqlite3-dev
|
编程环境配置
Python
https://github.com/pyenv/pyenv#installation
1
2
| curl https://pyenv.run | bash
# 或者去文档选用其他方法
|
添加环境变量
1
2
3
4
5
| echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
sudo ln -s ~/.pyenv/bin/pyenv ~/.local/bin/pyenv
|
安装 / 卸载任意版本
1
2
3
| pyenv install --list # 列出所有版本
pyenv install <version-name>
pyenv uninstall <version-name>
|
如果下得太慢,就用镜像把源码下到缓存目录:
1
| export v=3.12.1; wget https://npm.taobao.org/mirrors/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/; pyenv install $v
|
安装 pyenv-virtualenvwrapper
GitHub - pyenv/pyenv-virtualenvwrapper: an alternative approach to manage virtualenvs from pyenv.
这样就能在 pyenv
中使用 [[virtualenv]] 啦,依赖分离
1
| git clone https://github.com/pyenv/pyenv-virtualenvwrapper.git $(pyenv root)/plugins/pyenv-virtualenvwrapper
|
在 ~/.zshrc
中补充指令缩写
1
| alias pv='pyenv virtualenvwrapper'
|
之后就能在 pyenv 当前版本的 Python 中使用诸如 mkvirtualenv
或 workon
的指令啦
Go
安装
官网下载:https://golang.org/dl/
下载 go 源码包
版本随时从官网取,确认当前 linux 系统版本是 32 位还是 64 位,再选择 go 源码包
1
2
3
| # 查看linux多少位
uname -m
# x86_64
|
之后所有的软件都安装在 ~/.opt/
(而非 /opt/
,主要考虑到用户隔离)
1
2
3
| cd ~/.opt
sudo wget https://golang.google.cn/dl/go1.21.6.linux-amd64.tar.gz
sudo tar -zxvf go1.21.6.linux-amd64.tar.gz
|
给予权限
1
2
3
| mkdir gocode
sudo chmod -R 777 go/
sudo chmod -R 777 gocode/
|
配置环境变量
配置 go 的工作空间(配置 GOPATH),以及 go 的环境变量
创建 ~/.opt/gocode/{src,bin,pkg},用于设置 GOPATH 为 ~/.opt/godocer
1
2
3
4
5
6
| mkdir -p ~/.opt/gocode/{src,bin,pkg}
~/.opt/gocode/
├── bin
├── pkg
└── src
|
设置 GOPATH 环境变量
在 ~/zshrc
中写入 GOPATH 信息以及 go sdk 路径
1
2
3
4
5
| export GOROOT=~/.opt/go # Golang 源代码目录,安装目录
export GOPATH=~/.opt/gocode # Golang 项目代码目录
export GOBIN=$GOPATH/bin # go install 后生成的可执行命令存放路径
export PATH=$GOROOT/bin:$GOBIN:$PATH # Linux 环境变量
|
go install 配置代理(可选)
1
| go env -w GOPROXY=https://goproxy.cn
|
Node.js
官网下载:https://nodejs.org/en/download/
都给我装在 ~/.opt/
!
下载、解压 Node.js Linux 64 位二进制安装包
1
2
3
| cd ~/.opt
wget https://nodejs.org/dist/v20.12.1/node-v20.12.1-linux-x64.tar.xz
tar xvf node-v20.12.1-linux-x64.tar.xz
|
👆 版本随时从官网取
创建软链接
1
2
| sudo ln -s ~/.opt/node-v20.12.1-linux-x64/bin/node ~/.local/bin/node
sudo ln -s ~/.opt/node-v20.12.1-linux-x64/bin/npm ~/.local/bin/npm
|
Docker
由于 apt
源使用 HTTPS 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书。
1
2
3
4
5
6
| sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
|
为了确认所下载软件包的合法性,需要添加软件源的 GPG
密钥。
1
2
3
4
5
| curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 官方源
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
|
然后,我们需要向 sources.list
中添加 Docker 软件源
1
2
3
4
5
6
7
8
9
| echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 官方源
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
以上命令会添加稳定版本的 Docker APT 镜像源,如果需要测试版本的 Docker 请将 stable 改为 test。
更新 apt 软件包缓存,并安装 docker-ce
:
1
2
3
| sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
安装 docker-compose(最新版本去 仓库 看)
1
2
| sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.1/docker-compose-$(uname -s)-$(uname -m)" -o ~/.local/bin/docker-compose
sudo chmod +x ~/.local/bin/docker-compose
|
修改设置(限制日志容量上限、启用 IPv6 等)
1
2
3
4
5
6
7
8
9
10
11
12
13
| cat > /etc/docker/daemon.json <<EOF
{
"log-driver": "json-file",
"log-opts": {
"max-size": "20m",
"max-file": "3"
},
"ipv6": true,
"fixed-cidr-v6": "fd00:dead:beef:c0::/80",
"experimental":true,
"ip6tables":true
}
EOF
|
(可选)设置 swap 分区
查看 Linux 当前分区情况:
如果是增加 swap 分区,则先把当前所有分区都关闭了:
创建要作为 Swap 分区文件(其中 /var/swapfile
是文件位置,bs*count
是文件大小,例如以下命令就会创建一个 4G 的文件):
1
| dd if=/dev/zero of=/var/swapfile bs=1M count=4096
|
建立 Swap 的文件系统(格式化为 Swap 分区文件):
启用 Swap 分区:
设置开启启动。在 /etc/fstab 文件中加入一行代码:
1
| /var/swapfile swap swap defaults 0 0
|
Code-Server
可以在浏览器中编辑代码
安装
1
| curl -fsSL https://code-server.dev/install.sh | sh
|
To have systemd start code-server now and restart on boot:
1
| sudo systemctl enable --now code-server@$USER
|
Or, if you don’t want/need a background service you can run:
修改配置
1
| vi ~/.config/code-server/config.yaml
|
开启
1
| sudo systemctl restart code-server@$USER
|
查看进程状态
1
| sudo systemctl status --now code-server@$USER
|
最终的 .zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| # Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
export PATH=$PATH:~/.local/bin/:/snap/bin
export GOROOT=~/.opt/go # Golang 源代码目录,安装目录
export GOPATH=~/.opt/gocode # Golang 项目代码目录
export GOBIN=$GOPATH/bin # go install 后生成的可执行命令存放路径
export PATH=$GOROOT/bin:$GOBIN:$PATH # Linux 环境变量
# ZSH_THEME="random"
plugins=(
git
extract
rand-quote
themes
z
per-directory-history
history-substring-search
command-not-found
safe-paste
colored-man-pages
sudo
zsh-autosuggestions
# history
zsh-syntax-highlighting
copypath
copyfile
you-should-use
)
source $ZSH/oh-my-zsh.sh
alias :vrc="vi ~/.zshrc"
alias :src="source ~/.zshrc"
alias -s ttt='tar zxvf'
alias cp="cp -i" # 防止 copy 的时候覆盖已存在的文件, 带上 i 选项,文件已存在的时候,会提示,需要确认才能 copy
alias gcm='git checkout master'
alias cat='batcat'
alias top='bashtop'
alias ls='exa'
alias du='dust'
alias df='duf'
alias find='fd'
alias ping='gping'
alias ps='procs'
alias curl='curlie'
alias vim='nvim'
alias vi='nvim'
alias pv='pyenv virtualenvwrapper'
# 以自己输入的所有内容为前缀,进行历史查找
bindkey '^P' history-substring-search-up
bindkey '^N' history-substring-search-down
# 让 history 命令显示时间
HIST_STAMPS="yyyy-mm-dd"
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
source ~/.opt/powerlevel10k/powerlevel10k.zsh-theme
source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
|