#!/bin/bash # ubuntu 14.04/16.04 系统初始化配置脚本. set -ex sudo_home= if test -n "${SUDO_USER}" -a -n "${SUDO_UID}" -a -n "${SUDO_GID}" ; then eval sudo_home=~"${SUDO_USER}" fi rsync_to_home() { if test -n "${sudo_home}" ; then rsync -ai "${@}" "${sudo_home}/" --chown "${SUDO_UID}:${SUDO_GID}" fi rsync -ai "${@}" ~/ } # apt #sed -re '/^#*deb/ s,\S+(/ubuntu(-ports)?/?),http://mirrors.aliyun.com\1,' /etc/apt/sources.list -i apt-get update #apt-get install aptitude -y DEBIAN_FRONTEND=noninteractive apt-get install debconf-utils apt-utils whiptail -y apt-get install locales util-linux less man-db lsb-release -y apt-get install iputils-ping iputils-tracepath traceroute iproute2 dnsutils net-tools rsync curl -y apt-get install jq moreutils tree -y apt-get install bash-completion -y # locales locale-gen en_US.UTF-8 zh_CN.UTF-8 # WSL 不支持使用双引号, 如 `LANG="en_US.UTF-8"` (?) update-locale --reset LANG=en_US.UTF-8 #container= #eval "$(grep -z "^container=" /proc/1/environ)" : ${update_localtime:=false} if ${update_localtime} && ! mountpoint -q /etc/localtime ; then # tzdata 软件包较特殊,其希望尽量保持当前配置不变. # 可隐藏当前配置以使其使用 debconf-set-selections 配置. # 查看 /var/lib/dpkg/info/tzdata.config 脚本可知, # 其优先检查软连接 /etc/localtime 或配置文件 /etc/timezone 获取当前配置. if test -L /etc/localtime ; then rsync -ai "$(readlink -f /etc/localtime)" /etc/localtime ; fi rm -f /etc/timezone echo $'tzdata tzdata/Areas select Asia\ntzdata tzdata/Zones/Asia select Shanghai' | debconf-set-selections DEBIAN_FRONTEND=noninteractive apt-get install tzdata -y DEBIAN_FRONTEND=noninteractive dpkg-reconfigure tzdata fi # .bashrc # fix ll, do not use "-a", use "-Al" sed -re 's/^(alias ll='\''ls -).*?a.*?('\'')/#\0\n\1Al\2/' /etc/skel/.bashrc ~/.bashrc ${sudo_home:+"${sudo_home}"/.bashrc} -i # fix PS1='\n...\n\$ ' , 参考 bashrc-fix-ps1.sh 脚本 ( fixPs1='PS1="$(sed -re '\''s#(\\n)*(\\\$\s*)$#\\n\2# ; /\\n.*\\u/ b ; s#^#\\n#'\'' <<< "$PS1")" # 确保 PS1 以 r'\''\n\$'\'' 结尾' sed -re "\$ a $(perl -E "say quotemeta" -s -- -_="${fixPs1}")" -e '/^PS1="\$\(sed\s/ d' /etc/skel/.bashrc ~/.bashrc ${sudo_home:+"${sudo_home}"/.bashrc} -i ) # inputrc cat <<'EOF' | tee /etc/skel/.inputrc $include /etc/inputrc # mappings for Ctrl-up-arrow and Ctrl-down-arrow for history search "\e[1;5A": history-search-backward "\e[1;5B": history-search-forward "\ep": history-search-backward "\en": history-search-forward EOF rsync_to_home /etc/skel/.inputrc #if test -f /lib/systemd/system/getty@.service ; then # sed -r -e '/^Environment=/ s#$# TERM=xterm#' /lib/systemd/system/getty@.service > /etc/systemd/system/getty@.service #fi # fix grub config #if test -f /etc/default/grub ; then # sed -r -e 's/^(GRUB_DEFAULT=).*/\1saved/g' -e '/^GRUB_HIDDEN/ s/^/#/g' -e 's/^(GRUB_TIMEOUT=).*/\13/g' -e 's/^(GRUB_CMDLINE_LINUX_DEFAULT=).*/\1"quiet"/g' -e $'/^GRUB_CMDLINE_LINUX=/ a\\\nGRUB_DISABLE_SUBMENU=y' -e '/^GRUB_DISABLE_SUBMENU=/ d' /etc/default/grub -i # update-grub #fi # adduser 用户名允许使用 "." sed $'/^NAME_REGEX=/ d; /^#NAME_REGEX=/ a \\\nNAME_REGEX="^[a-z][-a-z0-9_.]*$"' /etc/adduser.conf -i # vim apt-get install vim -y update-alternatives --set editor /usr/bin/vim.basic tee /etc/vim/vimrc.local <<'EOF' set noet ts=4 sw=0 set fencs=ucs-bom,utf-8,default,gbk,latin1 set nobackup set backupdir=~/tmp/vim,/tmp set noundofile set undodir=~/tmp/vim,/tmp EOF #> sed -r -e '/^\s*autocmd\s+.*setlocal textwidth=/ s#^#"#' -e '$ a'$'\n' -e '$ a set nobackup' -e '$ a set noundofile' -e '$ a set modeline' /usr/share/vim/vim*/vimrc_example.vim > /etc/skel/.vimrc rsync_to_home /etc/skel/.vimrc # tmux apt-get install tmux -y tee /etc/tmux.conf <<'EOF' set -g history-limit 5000 set -g default-terminal 'xterm' set -g xterm-keys on #set -g alternate-screen off set -ga terminal-overrides ',xterm*:smcup=:rmcup=:Tc' # avoid tmux execute default-shell as login shell by default set -g default-command /bin/bash set -g prefix C-a unbind-key C-b bind-key C-a send-prefix set -g mode-keys vi set -g status-keys vi EOF #> # language-pack #apt-get install language-pack-zh-hans -y # git #apt-get install git -y