公司最近新搭建了测试服务器,因为要经常登录操作,我习惯于在上面搭建自己顺手的使用环境,
使用 zsh + oh-my-zsh,zsh-autosuggestion 等工具,配合自己配置的一些 alias, shell 脚本,还有诸如vim配置,tmux配置,git的配置都弄上去。
用了这一套顺手的工具,操作效率会高很多。
之前是手动配置的,把这些搭建好,大概也花个半个小时。今天同事把服务器重装了,我的配置也没有了。
也促使我下决心写一个自动化的脚本来做这个事情。
花了一点时间来写了两个脚本。
其一是以 root 运行的,创建用户,授予root权限,安装zsh/tmux/git/vim等必备软件,大概如下:
script1 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
| # 安装 git / tmux / zsh / vim read -p "发行版[ubuntu or centos]>" version if [ $version == "ubuntu" ]; then apt update; apt install git tmux zsh vim -y; elif [ $version == "centos" ]; then yum -y install git tmux zsh vim python3 fi
# 创建用户 read -p "要创建的用户名> " name read -p "密码> " password useradd -d /home/$name -s /bin/bash -m $name passwd $name $password $password
echo "-----创建用户成功---\n"
read -p "是否需要 root 权限[y/yes/n/no]> " need_root
# 如果需要root,创建 admin group,并且加入到里面去 if [ $need_root == 'y' ] || [ $need_root == 'yes' ]; then addgroup admin usermod -a -G admin $name # 给与权限 echo "$name ALL=(ALL) NOPASSWD: ALL" | sudo EDITOR='tee -a' visudo fi
echo "修改用户shell 为 zsh >" chsh -s `which zsh` $name
|
另一个脚本是以我创建的用户来运行,涉及到生成ssh key , 等待将 key 复制到Github/Gitee,安装 oh-my-zsh, 从 GitHub 克隆各类配置文件(dotfile),再将其软连接到对应的位置。
代码如下:
script1 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
| # 如果没有 ssh key, 创建 if [ ! -f ~/.ssh/id_rsa.pub ]; then ssh-keygen fi cat ~/.ssh/id_rsa.pub
read -p "是否已将id_rsa复制到github中[y/yes/n/no]> " copied
# oh-my-zsh sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)"
# clone dotfile git clone git@github.com:WuHuaJi0/dotfile.git ~/dotfile
# install autojump git clone git://github.com/wting/autojump.git cd autojump python install.py
# link ln -sf $HOME/dotfile/zshrc $HOME/.zshrc; ln -sf $HOME/dotfile/vimrc $HOME/.vimrc; ln -sf $HOME/dotfile/gitconfig $HOME/.gitconfig; ln -sf $HOME/dotfile/gitignore $HOME/.gitignore; ln -sf $HOME/dotfile/tmux.conf $HOME/.tmux.conf; ln -sf $HOME/dotfile/git/gitconfig $HOME/.gitconfig ln -sf $HOME/dotfile/git/global_ignore $HOME/.global_ignore ln -sf $HOME/dotfile/shell/ $HOME/shell ln -sf $HOME/dotfile/zsh-syntax-highlighting $HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting ln -sf $HOME/dotfile/zsh-autosuggestions $HOME/.oh-my-zsh/custom/plugins/zsh-autosuggestions
|
这一套组合下来,我很满意,我喜欢能自动化的东西都尽量自动化了!