跳转至

安装 linux 后的配置

概述

不想每次配置 linux 都需要重新去搜索,因此在自己平时折腾时顺便记录一下(长期更新)

系统配置

时区

timedatectl set-timezone Asia/Shanghai

美化

Ubuntu

  • dock 栏 在设置->外观中设置 dock 栏为底部自动隐藏,并设置合适的大小。
  • 背景图 暂时还没有找到比较好的轮播图片的方法
  • gnome-terminal 设置背景颜色和透明度

常用软件

中文输入法

参考:

如果安装系统时选择了中文,则自带一个中文输入法。而如果选择了英文,后面想改成中文。可以:

  1. 将设置中的区域和语言改为中文。
  2. 打开language support,第一次打开会显示语言支持没有完全安装,会安装中文字体等。

下载谷歌拼音输入法

sudo apt-get install fcitx-googlepinyin

然后在 language support 中将 Keyboard input method 从默认的 iBus 改成 fcitx,重启电脑。

"桌面"改为"Desktop"

ubuntu

export LANG=en_US
xdg-user-dirs-gtk-update

选择更新名称

export LANG=zh_CN
xdg-user-dirs-gtk-update

选择保留名称,并不再提醒

更改 xdg-user-dirs

  • xdg 全局配置文件:/etc/xdg/user-dirs.conf/etc/xdg/user-dirs.defaults
  • 修改用户配置文件$HOME/.config/user-dirs.dirs

    XDG_DOWNLOAD_DIR="$HOME/downloads/Downloads"
    XDG_DESKTOP_DIR="$HOME/downloads/Desktop"
    XDG_TEMPLATES_DIR="$HOME/downloads/Templates"
    XDG_PUBLICSHARE_DIR="$HOME/downloads/Public"
    XDG_DOCUMENTS_DIR="$HOME/downloads/Documents"
    XDG_MUSIC_DIR="$HOME/downloads/Music"
    XDG_PICTURES_DIR="$HOME/downloads/Pictures"
    XDG_VIDEOS_DIR="$HOME/downloads/Videos"
    
  • (重要)创建对应目录,否则重启后上述配置会指向$HOME/
#!env bash
PREFIX=$HOME/downloads

DIRs="Downloads Desktop Templates Public Documents Music Pictures Videos"
for dir in $DIRs; do
    rm -d $HOME/$dir
    mkdir $PREFIX/$dir
    sed -i $HOME/.config/user-dirs.dirs 's/$HOME\/'$dir'/'$PREFIX'\/'$dir
done

代理

  • 使用 qv2ray

    1. 在 github 上下载 qv2ray, v2ray
    2. 配置 qv2ray core
    3. 导入链接

    具体参考https://qv2ray.net/getting-started/

    注:目前版本 (2.6.3),在设置开机自启动后,右上角的 qv2ray 图标会消失,导致无法断开连接非常麻烦。解决办法为取消开机自启动并重新安装(数据仍然会保留,故不必担心。卸载后需要重启或注销一下账户,然后才能重装,否则仍然不显示)

  • 使用 clash for windows

    • 使用 flatpak 安装即可

      flatpak install io.github.Fndroid.clash_for_windows
      
    • 旧方法
    1. 下载Releases · Fndroid/clash_for_windows_pkg · GitHub

    2. 解压后命令行运行 cfw

      (不知道有什么办法把该程序固定到任务栏)

谷歌浏览器

非 snap 安装 chromium: Chromium - web browser (Xenial & newer) : Rob Savoury (launchpad.net)

  • 使用 chromium(chromium 也可以使用 google 帐号进行同步)
    sudo apt install chromium-browser
    
  • 使用 google chrome
    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    cd /path
    sudo apt install ./google-chrome-stable_current_amd64.deb
    

wine

qq

wget -O- https://deepin-wine.i-m.dev/setup.sh | sh
sudo apt-get install deepin.com.qq.im

VS Code

https://code.visualstudio.com/docs/setup/linux

  1. download deb

    Download Visual Studio Code - Mac, Linux, Windows

    sudo apt install ./<file>.deb
    
  2. add apt repo

    sudo apt install software-properties-common apt-transport-https wget
    wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
    sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
    
    sudo apt update
    sudo apt-get -o Acquire::http::proxy="socks5h://127.0.0.1:1089/" install code #约 70MB
    
  3. snap or Ubuntu software

    软件占用空间更大

其它工具

gparted

sudo apt install gparted

使用该软件进行分区

zerotier

curl -s https://install.zerotier.com | sudo bash

join network

% zerotier-cli info
200 info 8af72edda7 1.10.2 ONLINE

% zerotier-cli join d5e04297a16fa690
200 join OK

命令行

软件

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list
sudo apt update

sudo apt install vim git wget zsh htop net-tools

自动配置

apt install wget curl git zsh tmux mtr htop bind9-dnsutils
wget https://github.com/TheRainstorm/my-vim-config/raw/master/setup.sh 
# 或 https://raw.githubusercontent.com/TheRainstorm/my-vim-config/master/setup.sh

apt install zsh
zsh

source setup.sh

.bashrc

可以让 bash 和 zsh 共用同一个 alias 配置文件。在 zshrc 中添加

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

主要内容

alias t="tmux "
alias ra="ranger"
alias ca="conda activate "

export proxy_addr=localhost
export proxy_http_port=11223
export proxy_socks_port=11223
function set_proxy() {
    export http_proxy=http://$proxy_addr:$proxy_http_port #如果使用git 不行,这两个http和https改成socks5就行
    export https_proxy=http://$proxy_addr:$proxy_http_port
    export all_proxy=socks5://$proxy_addr:$proxy_socks_port
}
function unset_proxy() {
    unset http_proxy
    unset https_proxy
    unset all_proxy
}
function test_proxy() {
    curl -v https://www.google.com 2>&1 | egrep 'HTTP/(2|1.1) 200'
}

# Prompt
export PROMPT="%B%n@%m $PROMPT"

vim

vim 粘贴时关闭自动空格

:set paste    #paste模式,临时关闭autoindent
:set nopaste

zsh

安装 oh-my-zsh 时会自动将 zsh 设置为默认 shell

oh-my-zsh 手动安装

通常来说 curl 获得 install 脚本时会卡住,git clone 仓库时也可能卡住,此时不如手动安装,查看install脚本,会发现其实只做了几件事情:

  • 将 oh-my-zsh 仓库克隆到$HOME 下.oh-my-zsh
    • 可以从其他计算机 scp 到路由器里
  • 复制 zshrc 文件:cp .oh-my-zsh/templates/zshrc.zsh-template .zshrc
    • 直接复制即可,不用修改内容
  • chsh
    • openwrt 没有 chsh 命令,需要修改/etc/passwd
    • 注意,如果修改错误(如直接输入 zsh 而不是 zsh 从根开始的完整路径)可能导致无法登录 openwrt

安装 oh-my-zsh 并安装插件

install:Oh My Zsh - a delightful & open source framework for Zsh

 sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

tips:

  1. 安装 oh-my-zsh 会使用 git 克隆仓库,请提前安装 git 并设置代理
  2. 如果连接不上 raw.github.com 可以尝试更换 DNS。更保险的还是给 bash 设置代理
  3. 推荐直接从另一台机器上 scp
zsh-autosuggestions

zsh-autosuggestions 作用:自动提示命令

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

在.zshrc 中 plugin 行添加 zsh-autosuggestions,即

plugins=(git zsh-autosuggestions)
zsh-syntax-highlighting

zsh-syntax-highlighting 作用:命令高亮,正确的命令显示绿色,错误的显示为红色

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
echo "source $ZSH_CUSTOM/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
autojump

autojump 作用:j 直接跳转到任意一个目录

git clone git://github.com/wting/autojump.git $ZSH_CUSTOM/plugins/autojump

cd $ZSH_CUSTOM/plugins/autojump
./install.py or ./uninstall.py #如果系统只识别python3,可以设置软链接python指向python3。或者python3 install.py

#将install.py结尾输出复制到.zshrc中

按照 install.py 输出提示,复制对应代码到 zshrc 中

fzf

fzf is a general-purpose command-line fuzzy finder.

使用 fzf 配合 ctrl-r,可以极大方便查找输入过去指令。

推荐使用 git 方式,使用软件包安装的方式没有设置快捷键(在 bash, zsh 上均是如此),如 ctrl-r 搜索时调用 fzf

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install

当然,fzf 的功能远不止搜索历史记录,更多用法可以查看视频:Vim universe. fzf - command line fuzzy finder - YouTube

apt 安装方式配置快捷键

dpkg -L 可以看到安装的包的文件

root@acsa-nfs:~# dpkg -L fzf
/.
/usr
/usr/bin
/usr/bin/fzf
/usr/bin/fzf-tmux
/usr/share
/usr/share/bash-completion
/usr/share/bash-completion/completions
/usr/share/bash-completion/completions/fzf
/usr/share/doc
/usr/share/doc/fzf
/usr/share/doc/fzf/README.Debian
/usr/share/doc/fzf/changelog.Debian.amd64.gz
/usr/share/doc/fzf/changelog.Debian.gz
/usr/share/doc/fzf/changelog.gz
/usr/share/doc/fzf/copyright
/usr/share/doc/fzf/examples
/usr/share/doc/fzf/examples/completion.zsh
/usr/share/doc/fzf/examples/fzf.vim
/usr/share/doc/fzf/examples/key-bindings.bash
/usr/share/doc/fzf/examples/key-bindings.fish
/usr/share/doc/fzf/examples/key-bindings.zsh
/usr/share/doc/fzf/examples/plugin
/usr/share/fish
/usr/share/fish/vendor_functions.d
/usr/share/fish/vendor_functions.d/fzf_key_bindings.fish
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/fzf-tmux.1.gz
/usr/share/man/man1/fzf.1.gz
/usr/share/doc/fzf/examples/plugin/fzf.vim

复制快捷键绑定脚本

cp /usr/share/doc/fzf/examples/key-bindings.bash ~/.fzf.bash

bashrc 中添加一下内容

# Enable fuzzy search for command history using fzf
[ -f ~/.fzf.bash ] && source ~/.fzf.bash

问题

如果 ctrl-r 显示unknown option: --scheme=history,是因为系统 apt 安装的 fzf 版本过旧导致的。

使用 git 安装时,~/.fzf/install会下载 fzf bin 文件,之后将 git 仓库下.fzf/bin添加到 PATH 变量尾部。但是由于是在 PATH 尾部,因此优先级不如系统的/usr/bin 目录。解决办法为手动修改.fzf.bash文件

# Setup fzf
# ---------
#if [[ ! "$PATH" == */staff/fyyuan/.fzf/bin* ]]; then
#  #PATH="${PATH:+${PATH}:}/staff/fyyuan/.fzf/bin"
#  PATH="/staff/fyyuan/.fzf/bin:$PATH"
#fi
PATH="/staff/fyyuan/.fzf/bin:$PATH"

搜索不到时已经输入的命令白费

Requesting special history search feature: If matching zero, upon entry don't nuke my command, emit it. · Issue #3528 · junegunn/fzf (github.com)

我没有成功。不够我的历史记录够长了,基本不存在搜索为空的情况(更多是模糊搜索找到太多结果),因此对我也没用了。

tmux(.tmux(oh-my-tmux))

git clone https://github.com/gpakosz/.tmux
ln -s -f .tmux/.tmux.conf
cp .tmux/.tmux.conf.local .

tmux source-file .tmux.conf

比较好用的几个功能

session

  • <prefix> f enter显示所有的会话、窗口,并提供预览。方便管理多个会话
  • <prefix> shift-Tab(BTab):切换到上一个 session
  • <prefix> ctrl-c:创建新 session
  • <prefix> hjkl:在不同 panel 中切换,更符合 vim 习惯
  • <prefix> b, P:b 显示剪切板(buffer),P 选择粘贴

修改

由于 tmux 默认的 prefix 是Ctrl-b不太好按,因此我会把它换成ctrl-x

窗口切换改为了tab, C-h, C-l,感觉有点麻烦,因此将p粘贴上一个 buffer 功能去掉,换回原本的n, p

set -g prefix2 C-x
bind C-x send-prefix -2

bind -r n previous-window # select previous window
bind -r p next-window     # select next window
bind Tab last-window      # move to last active window

#bind p paste-buffer  # paste from the top paste buffer

zplug

zplug/zplug: A next-generation plugin manager for zsh (github.com) zsh 的一个插件管理工具,类似的还有Antigen,优点是启动更加快速。 相比于 oh-my-zsh 的插件管理,安装配置都更简单(如不需要手动 git clone 安装插件,有的插件还需要修改.zshrc 等)

使用

安装

curl -sL --proto-redir -all,https https://raw.githubusercontent.com/zplug/installer/master/installer.zsh | zsh

配置:在.zshrc中添加以下节,其中zplug 'endaaman/lxd-completion-zsh'指定了需要使用的插件。还可以使用其它插件

  • 安装插件需要设置代理
##########
#####  zplug
##########
source ~/.zplug/init.zsh

# Zsh completion for lxc and lxd commands of LXD.
zplug 'endaaman/lxd-completion-zsh'

# Fish-like fast/unobtrusive autosuggestions for zsh.
zplug 'zsh-users/zsh-autosuggestions'

# Fish shell-like syntax highlighting for Zsh.
zplug 'zsh-users/zsh-syntax-highlighting'

# Install plugins if there are plugins that have not been installed
if ! zplug check --verbose; then
    printf "Install? [y/N]: "
    if read -q; then
        echo; zplug install
    fi
fi

# Then, source plugins and add commands to $PATH
zplug load

寻找插件

自定义 prompt

bash

Bash/Prompt customization - ArchWiki (archlinux.org) bash(1) — Arch manual pages (archlinux.org)

zsh

Zsh - ArchWiki (archlinux.org)

初始化自带的 prompt theme system

~/.zshrc
autoload -Uz promptinit
promptinit
prompt -l   # 列出所有
prompt -p   # 预览所有
prompt walters  # 选择一个theme

自定义 theme

# Load promptinit
autoload -Uz promptinit && promptinit

# Define the theme
prompt_mytheme_setup() {
  PS1="%~%# "
}

# Add the theme to promptsys
prompt_themes+=( mytheme )

# Load the theme
prompt mytheme
Customized
  • 兼容 bash 的 left-sided prompt: PS1, PS2, ...,自己有别名:PROMPT, PROMPT2...
  • 另外支持 right-sided prompt:RPS1 (RPROMPT)
print -P '%B%F{red}co%F{green}lo%F{blue}rs%f%b'   # 测试效果

PROMPT='%n@%m %~ %# '

print -P "%l %M %m %n %y %d"
%~

zshmisc(1) — Arch manual pages (archlinux.org)

服务

sshd

参考:Service - OpenSSH | Ubuntu

安装 openssh-server(有可能默认安装了)

sudo apt install openssh-server

配置 sshd,如端口

sudo vim /etc/ssh/sshd_config

运行 sshd

  • ubuntu

    sudo systemctl restart sshd.service
    
  • deepin 没有 systemd sshd.service,使用

    sudo service ssh start
    

vnc

常用的有 tigervnc,但是实际安装过程中经常遇到问题,比如黑屏、任务栏消失、xstartup 设置、无法使用 systemd 等问题。遇到问题不要慌,可能就是 vnc 本身太烂了,能不配就不配

问题

  • 新版的 Tigervnc(>=1.10.0)不再支持 vncserver 命令启动远程桌面服务。取而代之的是通过 systemed 作为系统服务的方式来启动。

参考:

tigervnc<1.10

tigervnc github 仓库TigerVNC/tigervnc readme 说的不是很详细,可以直接安装

sudo apt install tigervnc-standalone-server

直接 vncserver 启动,第一次运行会设置密码

vncserver -localhost no #设置为no localhost,不然只能在本计算机上访问(LAN内的都不行)。

查看 vncserver,默认为:

  vncserver -list   #查看当前运行的server
  vncserver -kill :number #停止指定的server

虽然有的教程说要配置 xstartup,但是可能不配置已经可以了

客户端连接

  • 使用 realVNC 输入 ip_address:port

使用隧道建立加密连接

ssh -L 59000:localhost:5901 -C -N -l server_user_name server_ip_address
  • -L 59000:localhost:5901 The -L argument specifies the post on your local computer (ours is 59000) to the given host (localhost) and port (5901) on the destination server (server_ip_address).
  • -C: This argument speeds up processes and minimizes resource usage.
  • -N: This argument prevents execution of remote commands and tells SSH to only forward ports.
  • -l server_user_name server_ip_address: This line specifies the user you will use to login to the server.

常用操作参考

apt 相关

apt 换源

cp /etc/apt/sources.list /etc/apt/sources.list.back #备份
sed -i "s/archive.ubuntu.com/mirrors.ustc.edu.cn/g" /etc/apt/sources.list
sed -i "s/security.ubuntu.com/mirrors.ustc.edu.cn/g" /etc/apt/sources.list

在 vim 中手动替换则是%s

:%s/security.ubuntu.com/mirrors.ustc.edu.cn/g

aliyun mirror ubuntu

apt-get 临时使用 sock 代理

sudo apt-get -o Acquire::http::proxy="socks5://127.0.0.1:1089/" install typora

bash 设置代理

#端口对应qv2ray软件默认设置
export HTTP_PROXY=http://localhost:8889
export HTTPS_PROXY=http://localhost:8889
unset HTTP_PROXY
unset HTTPS_PROXY

git 设置代理

git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080

git doc: http.proxy

命令行临时代理

GIT_TRACE=1 git -c "http.proxy=address:port" clone https://...

恢复默认.bashrc

  • 在 ubuntu 中,默认的 bashrc 文件存储在 /etc/skel/.bashrc

vim 复制粘贴(系统剪贴板)

  1. 首先确认 vim 是否开启了 clipboard 功能

    vim --version |grep clipboard #如果显示出-clipboard 则需要重装 vim
    

    如果没有则重装

    sudo apt remove vim
    sudo apt install vim-gtk
    
  2. 使用鼠标选中内容,输入"+y命令便可将内容复制到剪贴板。

    使用"+p将剪贴板内容到当前位置。

    或者

    然后在.vim/vimrc中设置快捷键ctrl-c, ctrl-v。

    "clipboard
    map <C-c> "+y<CR>
    map <C-v> "+p<CR>
    

    此时便可以在选中内容时按ctrl-c复制到系统剪切板。处于normal模式时ctrl-v粘贴了。

解释

vim 中包含许多不同功能的寄存器,用于存储在 vim 中复制的内容。可以通过:reg 命令查看 vim 中各寄存器的值

:reg #查看vim中的寄存器

    ""      // 默认unnamed寄存器,最近一次"d","c","s","x","y"复制,删除,修改内容
    "0      // 最近一次"y"复制内容
    "1      // 最近一次"d","c","s","x"删除,修改内容
    "2      // 上一次"d","c","s","x"删除,修改内容,[3-9]类推
    "a      // 字母寄存器,供用户指定使用,比如"ay就是复制到"a寄存器,[b-z]类推
    "-      // 少于一行的"d","c","x"删除内容
    ".      // 只读寄存器
    ":      // 只读寄存器
    "%      // 只读寄存器
    "#      // 只读寄存器

    "+      // 映射系统剪贴板 (有的默认设置不支持)
    "*      // 映射系统剪贴板 (有的默认设置不支持)

vim 默认复制到的寄存器是""寄存器,要想复制到系统剪切板,可以通过"+寄存器(需要 vim 开启 clipboard 功能)

在选中内容的情况下,按y默认将内容复制到""寄存器。按p默认将""寄存器内容复制到当前位置。要想复制到指定寄存器,输入寄存器名+y 即可,粘贴时同理。

vim redo

undo: u
redo: ctrl + r

退出终端后命令不退出

关闭终端后,shell 给程序发送 SIGHUP 信号,导致程序退出。

会话(session),进程组,控制终端的概念。

  • nohup command
  • setsid command
    • 将 command 放在新的 session 中执行
  • (cmd &)
    • 原理:括号产生子 shell,子 shell 执行 cmd &。cmd & 导致

Q: (while true; do sleep 1; echo xxx; done &)如何杀掉

进程树结构:

  • zsh: 当前ssh zsh
    • zsh: 括号产生一个子shell
      • zsh: 后台执行一个shell脚本,又产生一个shell
        • sleep, echo: shell隔1s执行一次echo

ssh 的有用功能

jump

跳跃连接,使用中间节点进行代理,访问另一台 ssh 服务器。

ssh -J user@middle:port user@target -p port

ssh -J root@192.168.36.1:2202 root@192.168.36.254 -p 2202

正向转发

使用 ssh 可以在两台机器间建立一条加密的隧道,并且还可以将流量从一台机器转发到另一台机器。使用场景有:

  • A 能访问 B,B 能访问 C,但是 A 无法直接访问 C,此时就可以使用 ssh 将 A 的流量转发到 B,然后访问 C
  • vnc 连接时通过 ssh 建立加密隧道,更加安全
ssh -L 59000:localhost:5901 -C -N -l server_user_name server_ip_address
  • -L 59000:localhost:5901 The -L argument specifies the post on your local computer (ours is 59000) to the given host (localhost) and port (5901) on the destination server (server_ip_address).
  • -C: This argument speeds up processes and minimizes resource usage.
  • -N: This argument prevents execution of remote commands and tells SSH to only forward ports.
  • -l server_user_name server_ip_address: This line specifies the user you will use to login to the server.

远程机通过本地机代理上网(反向代理)

远程机比如服务器可能处于内网环境,无法访问网络。可以通过 ssh 解决这个问题。具体过程为:

  • 通过 ssh,在本地机和远程机上建立一条隧道。
  • 本地机上开启一个代理服务,比如 clash。
  • 远程机上配置网络访问使用代理(地址使用 localhost)
  • 远程机访问代理的端口时,会被 ssh 转发给本地机,作为本地流量。因此该访问会发送给本地的代理服务,实现上网。

整个过程中的核心是,服务器能访问本地机的代理服务。而这是通过 ssh 的反向转发实现的。对应的选项为:

ssh -fNgR 11223:127.0.0.1:11223 server
  • f:表示后台执行
  • N:表示 ssh 不用执行远程命令
  • g:表示 Allows remote hosts to connect to local forwarded ports. If used on a multiplexed connection, then this option must be specified on the master process.
  • 其中关于 R 的解释,作用类似于-L,只不过反了过来。

    -R [bind_address:]port
                 Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local side.
    
                 This works by allocating a socket to listen to either a TCP port or to a Unix socket on the remote side.  Whenever a connection is made to
                 this port or Unix socket, the connection is forwarded over the secure channel, and a connection is made from the local machine to either an
                 explicit destination specified by host port hostport, or local_socket, or, if no explicit destination was specified, ssh will act as a
                 SOCKS 4/5 proxy and forward connections to the destinations requested by the remote SOCKS client.
    
                 Port forwardings can also be specified in the configuration file.  Privileged ports can be forwarded only when logging in as root on the
                 remote machine.  IPv6 addresses can be specified by enclosing the address in square brackets.
    
                 By default, TCP listening sockets on the server will be bound to the loopback interface only.  This may be overridden by specifying a
                 bind_address.  An empty bind_address, or the address ‘*’, indicates that the remote socket should listen on all interfaces.  Specifying a
                 remote bind_address will only succeed if the server's GatewayPorts option is enabled (see sshd_config(5)).
    
                 If the port argument is ‘0’, the listen port will be dynamically allocated on the server and reported to the client at run time.  When used
                 together with -O forward the allocated port will be printed to the standard output.