M1 个人配置

前言

  记录一下 Shaun 个人的 Mac 装机配置。

必备

AlDente:Mac 电池健康保护神器,默认 80% 就行,想充满就设置为 100%,需要禁掉自带的优化电池充电

LuLu:防火墙,控制应用联网权限。


iTerm2

  下载安装 iTerm2,默认 shell 就是 zsh,所以不需要安装。

  安装 Oh My Zsh,github 上的命令在国内可能无法顺利执行,先 clone 下来,手动执行 sh tools/install.sh

  安装 Powerlevel10k 之前,先安装 nerd font 字体,Shaun 个人还是比价喜欢 Fira Code 字体,所以就选择下载 Fira Code Nerd Font 字体,只需要安装 Fira Code Retina Nerd Font Complete.ttf 即可。设置 iTerm2 字体为 FiraCode Nerd Font。

  随后开始安装 Powerlevel10k,安装完之后重启 iTerm2,会有 Powerlevel10k 的配置提问,依次回答(有推荐按推荐)完成即可配置好 Powerlevel10k,若后续想修改配置,可直接编辑 ~/.p10k.zsh 文件或使用 p10k configure 命令重新回答配置提问。最后在 zsh 的配置文件 ~/.zshrc 中设置 ZSH_THEME=powerlevel10k/powerlevel10k

  推荐安装 zsh 插件 zsh-syntax-highlightingzsh-autosuggestions,在执行完

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

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

后修改 ~/.zshrc 的 plugins 值,

1
2
3
4
5
6
plugins=( 
git
zsh-syntax-highlighting
zsh-autosuggestions
# other plugins...
)

VSCode

  VSCode 同样需要设置终端字体为 FiraCode Nerd Font,在终端中进入 Downloads 目录执行 mv Visual\ Studio\ Code.app /Applications 命令,将 VSCode 放进 应用程序 中,再执行 sudo ln -s "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" /usr/local/bin/code,之后可在终端使用命令(code .)直接打开 VSCode。若无法自动更新,需执行:

1
2
sudo chown -R $USER ~/Library/Caches/com.microsoft.VSCode.ShipIt
xattr -dr com.apple.quarantine /Applications/Visual\ Studio\ Code.app

Homebrew

  直接执行:

1
2
3
4
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

安装完成后先检查目录 /opt/homebrew/Library/Taps/homebrew/homebrew-cask 是否存在,若不存在,则执行:

1
2
cd /opt/homebrew/Library/Taps/homebrew/
git clone https://mirrors.ustc.edu.cn/homebrew-cask.git

  最后设置中科大源:

1
2
3
4
5
6
7
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
brew update

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile

Aria2

  直接使用命令 brew install aria2 安装,生成配置文件:

1
2
3
4
cd ~
mkdir .aria2
cd .aria2
touch aria2.conf

  打开 Finder,通过 Shift+Cmd+G 进入路径:~/.aria2/,编辑文件 aria2.conf,添加以下内容:

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
#用户名
#rpc-user=user
#密码
#rpc-passwd=passwd
#上面的认证方式不建议使用,建议使用下面的token方式
#设置加密的密钥
#rpc-secret=token
#允许rpc
enable-rpc=true
#允许所有来源, web界面跨域权限需要
rpc-allow-origin-all=true
#允许外部访问,false的话只监听本地端口
rpc-listen-all=true
#RPC端口, 仅当默认端口被占用时修改
#rpc-listen-port=6800
#最大同时下载数(任务数), 路由建议值: 3
max-concurrent-downloads=5
#断点续传
continue=true
#同服务器连接数
max-connection-per-server=5
#最小文件分片大小, 下载线程数上限取决于能分出多少片, 对于小文件重要
min-split-size=10M
#单文件最大线程数, 路由建议值: 5
split=10
#下载速度限制
max-overall-download-limit=0
#单文件速度限制
max-download-limit=0
#上传速度限制
max-overall-upload-limit=0
#单文件速度限制
max-upload-limit=0
#断开速度过慢的连接
#lowest-speed-limit=0
#验证用,需要1.16.1之后的release版本
#referer=*
#文件保存路径, 默认为当前启动位置
dir=/Users/yuanxu/Downloads
#文件缓存, 使用内置的文件缓存, 如果你不相信Linux内核文件缓存和磁盘内置缓存时使用, 需要1.16及以上版本
#disk-cache=0
#另一种Linux文件缓存方式, 使用前确保您使用的内核支持此选项, 需要1.15及以上版本(?)
#enable-mmap=true
#文件预分配, 能有效降低文件碎片, 提高磁盘性能. 缺点是预分配时间较长
#所需时间 none < falloc ? trunc << prealloc, falloc和trunc需要文件系统和内核支持
file-allocation=prealloc
bt-tracker=udp://tracker.opentrackr.org:1337/announce,udp://open.tracker.cl:1337/announce,udp://9.rarbg.com:2810/announce,udp://tracker.openbittorrent.com:6969/announce,udp://exodus.desync.com:6969/announce,udp://www.torrent.eu.org:451/announce,udp://vibe.sleepyinternetfun.xyz:1738/announce,udp://tracker1.bt.moack.co.kr:80/announce,udp://tracker.zerobytes.xyz:1337/announce,udp://tracker.torrent.eu.org:451/announce,udp://tracker.theoks.net:6969/announce,udp://tracker.srv00.com:6969/announce,udp://tracker.pomf.se:80/announce,udp://tracker.ololosh.space:6969/announce,udp://tracker.monitorit4.me:6969/announce,udp://tracker.moeking.me:6969/announce,udp://tracker.lelux.fi:6969/announce,udp://tracker.leech.ie:1337/announce,udp://tracker.jordan.im:6969/announce,udp://tracker.blacksparrowmedia.net:6969/announce

最后的 bt-tracker 可以从 trackerslist 获取,只用最好的 20 个即可(trackers_best (20 trackers) => link / mirror / mirror 2)。

  接着启动 aria2:aria2c --conf-path="/Users/xxx/.aria2/aria2.conf" -D (xxx 为电脑用户名),在 ~/.zshrc 中加入

1
2
alias start-aria2='aria2c --conf-path="/Users/xxx/.aria2/aria2.conf" -D'
start-aria2

将 start-aria2c 作为启动 aria2 的命令别名,顺便开机自启。

  最后从 Aria2中文网 安装 Chrome 插件,打开 aria2 的 WebUI 界面。

expect

  经常需要使用 ssh 远程登陆堡垒机再到远程服务器,输密码选机器都很麻烦,可以用 expect 写些脚本,自动填充密码和机器,一键直接进到远程服务器。首先安装 expect:brew install expect。在 /usr/local/bin 目录中新建脚本:sudo vi mysl.sh,填充相应内容:

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
#!/usr/bin/expect -f

set USER [用户名]
set PWD [密码]
set TERMSERVIP [堡垒机服务器ip]

# 全部的远程服务器([remote_server_name] 需要修改为对应的服务器名
set RS1 [remote_server_name]
set RS2 [remote_server_name]

# help 命令,查看所有需要登录的远程服务器
if {[lindex $argv 0] == "help"} {
puts "1: $RS1 [说明]"
puts "2: $RS2 [说明]"
send "exit\r"
exit
}

# ===== 脚本正文 =====
# 默认登陆远程服务器1
set RS $RS1
set timeout 10

# 输入命令 1,则登陆第一台服务器
if {[lindex $argv 0] == "1"} {
set RS $RS1
}
if {[lindex $argv 0] == "2"} {
set RS $RS2
}

spawn ssh ${USER}@${TERMSERVIP} -p 22
expect {
"yes/no" { send "yes\r"; exp_continue; }
"*assword*" { send "$PWD\n"}
}

# 选择几号跳板机
expect "*num*" { send "0\n" }

# 登陆远程服务器
expect "${USER}@" { send "ssh $RS\n" }

# 退出 expect(保持在远程服务器终端
interact

# 退出 expect(回到本地终端
# expect eof

为新建的脚本增加可执行权限:sudo chmod 777 mysl.sh,之后可直接使用 mysl.sh 1 登录到对应的远程服务器。

lrzsz

  与 FTP 和 NFS 相比,使用 lrzsz 与远程 linux 服务器做文件上传和下载是最简单的,在 iTerm2 中使用 rzsz 命令进行上传和下载文件需要一定的配置。※注使用 expect 自动登录的远程环境可能无法使用 sz rz 命令

  首先安装 lrzsz:brew install lrzsz。再跳转目录:cd /usr/local/bin,新建文件:sudo vi iterm2-recv-zmodem.sh,添加内容:

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
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domain

osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
FILE=`osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
else
FILE=`osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
fi

if [[ $FILE = "" ]]; then
echo Cancelled.
# Send ZModem cancel
echo -e \\x18\\x18\\x18\\x18\\x18
sleep 1
echo
echo \# Cancelled transfer
else
cd "$FILE"
/usr/local/bin/rz -E -e -b
sleep 1
echo
echo
echo \# Sent \-\> $FILE
fi

再新建文件:sudo vi iterm2-send-zmodem.sh,添加内容:

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
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domain

osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
FILE=`osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
else
FILE=`osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
fi
if [[ $FILE = "" ]]; then
echo Cancelled.
# Send ZModem cancel
echo -e \\x18\\x18\\x18\\x18\\x18
sleep 1
echo
echo \# Cancelled transfer
else
/usr/local/bin/sz "$FILE" -e -b
sleep 1
echo
echo \# Received $FILE
fi

  为新建的两文件添加可执行权限:sudo chmod 777 iterm2-*。之后添加 rz sz 命令的软连接:

1
2
sudo ln -s /opt/homebrew/bin/rz /usr/local/bin/rz
sudo ln -s /opt/homebrew/bin/sz /usr/local/bin/sz

  最后配置 iTerm2,选择 Preference... -> Profiles -> Default -> Advanced -> Edit (in Triggers),添加下载触发器:

1
2
3
4
5
6
7
8
9
10
11
# 1. Regular expression 中填写
rz waiting to receive.\*\*B0100

# 2. Action 选择
Run Silent Coprocess...

# 3. Parameters 中填写
/usr/local/bin/iterm2-send-zmodem.sh

# 4. Instant 不勾选
# 5. Enabled 勾选

再添加上传触发器:

1
2
3
4
5
6
7
8
9
10
11
# 1. Regular expression 中填写
\*\*B00000000000000

# 2. Action 选择
Run Silent Coprocess...

# 3. Parameters 中填写
/usr/local/bin/iterm2-recv-zmodem.sh

# 4. Instant 不勾选
# 5. Enabled 勾选

  至此 M1 中 iTerm2 rz sz 命令配置完成。

参考资料

iTerm2 + zsh + Oh My Zsh + Powerlevel10k 打造 Mac 下最强终端

M1芯片Mac上Homebrew安装教程

Mac M1 iTerm2 配置rz sz 上传下载文件