Linux 新机初始化 Centos8.x

分类:Linux |

1. 配置系统时区

#!/bin/bash
echo 'ZONE="Asia/Shanghai"'>/etc/sysconfig/clock
\cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'TZ="Asia/Shanghai"; export TZ' >> /etc/profile
source /etc/profile


如果出现报错: 

-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): 没有那个文件或目录


请添加修改: 

vim /etc/sysconfig/i18n 

LANG="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"

参考: https://www.updateweb.cn/zwfec/item-236.html


2. 系统默认配置 高效使用

yum install git -y 
yum install nss -y
ldconfig
yum install curl -y
yum install tmux -y
yum install zsh -y
yum install lrzsz -y
yum install dstat -y
yum update -y nss curl libcurl
yum install -y htop
yum install -y iotop

sh -c "$(curl -fsSL https://gitee.com/shmhlsy/oh-my-zsh-install.sh/raw/master/install.sh)"

or
REMOTE=https://gitee.com/mirrors/oh-my-zsh.git sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)"
git clone https://github.com/zsh-users/zsh-autosuggestions.git ~/.zsh/zsh-autosuggestions
sed -i "s/fg=8/fg=cyan/g" ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
cat <<EOF >> ~/.zshrc
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
export EDITOR=/usr/bin/vim
HISTSIZE=9000000
SAVEHIST=9000000
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias grep='grep --color=auto'
alias tmuxls='tmux ls'
alias tmuxa='tmux attach-session -t ' 
alias dstat='dstat -cdlmnpsy'
alias vime='vim -c "colorscheme evening" '
EOF
source ~/.zshrc 
cat <<EOF >> ~/.vimrc
colorscheme desert
set ts=4
set expandtab
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936
set fileencoding=gb2312
set termencoding=utf-8
set number
set paste
syntax on
set showmatch
filetype plugin indent on
EOF
cat <<EOF >> ~/.tmux.conf
set-window-option -g mode-mouse on
EOF


对于阿里云系统(Alibaba Cloud Linux 3.2104 LTS 64位), 

image.png


无法修改默认shell: 

只好以以下方法代替了  

 vim .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
     exec /usr/bin/zsh 
     . ~/.zshrc
     #. ~/.bashrc
fi                                                                                                                                                                         if [ -f ~/.bashrc ]; then                                                                                                                                                                                  exec /usr/bin/zsh . ~/.zshrc#. ~/.bashrc                                                                                                                                                                                           fi                                                                                                                                                                                                                                                                                                                                                                                   if [ -f ~/.bashrc ]; then                                                                                                                                                                                  exec /usr/bin/zsh . ~/.zshrc#. ~/.bashrc                                                                                                                                                                                           fi                                                                                                                                                                                                                                                                                                                                                                                    if [ -f ~/.bashrc ]; then                                                                                                                                                                                  exec /usr/bin/zsh . ~/.zshrc#. ~/.bashrc                                                                                                                                                                                           fi


如果报如下错误: 说明你的网络无法正常访问 github 

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

curl: (7) Failed to connect to ::: Cannot assign requested address


如果出现如下错误:

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

curl: (7) Failed to connect to raw.github.com port 443: Connection refused


使用: 

sh -c "$(curl -fsSL https://gitee.com/shmhlsy/oh-my-zsh-install.sh/raw/master/install.sh)"


如果出现: name lookup timeout 的情况

image.png


请执行: 

yum update ca-certificates


根据自己的实际情况修改: 

vim ~/.oh-my-zsh/themes/robbyrussell.zsh-theme


写入:

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
local pwd="%{$fg[yellow]%}%~%{$reset_color%}"
local user="%(!.%{$fg[cyan]%}.%{$fg[cyan]%})%n%{$reset_color%}"
ZSH_THEME_IP=`ifconfig -a|grep -o -e 'inet [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}'|grep -v '127.0.0\|192.168.124\|192.168.159'|awk '{print $2}'`

nl=$'\n%{\r%}╰─ ';
PROMPT_PREFIX="╭─ %{$fg[yellow]%}[$ZSH_THEME_IP]${user} ${pwd} %{$fg_bold[cyan]%}20%D %*%{$reset_color%}"
PROMPT='${PROMPT_PREFIX} ${nl}${ret_status}%{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'


显示主机名: 

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

typeset -A host_repr
host_repr=('dieter-ws-a7n8x-arch' "%{$fg_bold[green]%}ws" 'dieter-p4sci-arch' "%{$fg_bold[blue]%}p4")
local host="%{$fg[cyan]%}@${host_repr[$HOST]:-$HOST}%{$fg[cyan]%}%{$reset_color%}"


local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
local pwd="%{$fg[yellow]%}%~%{$reset_color%}"
local user="%(!.%{$fg[cyan]%}.%{$fg[cyan]%})%n%{$reset_color%}"

#ZSH_THEME_CURRENT_DATE=`date +%Y-%m-%d`
#ZSH_THEME_IP=`ifconfig eth0|grep 'inet addr'|cut -d ":" -f2|cut -d " " -f1`
ZSH_THEME_IP=`ifconfig -a|grep -o -e 'inet [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}'|grep -v '127.0.0\|192.168.124\|192.168.159'|awk '{print $2}'`


nl=$'\n%{\r%}╰─ ';
PROMPT_PREFIX="╭─ %{$fg[yellow]%}[$ZSH_THEME_IP]${user}${host} ${pwd} %{$fg_bold[cyan]%}20%D %*%{$reset_color%}"
PROMPT='${PROMPT_PREFIX} ${nl}${ret_status}%{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'


function git_prompt_info() {
  local ref
  if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
    ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
    ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
    echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$ZSH_THEME_GIT_PROMPT_DIRTY "
  fi
}


My MacOSX

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
local pwd="%{$fg[yellow]%}%~%{$reset_color%}"
local user="%(!.%{$fg[cyan]%}.%{$fg[cyan]%})%n%{$reset_color%}"
ZSH_THEME_IP=`ifconfig -a|grep -o -e 'inet [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}'|grep -v '127.0.0\|192.168.124\|192.168.159'|awk '{print $2}'`

nl=$'\n%{\r%}╰─ ';
PROMPT_PREFIX="╭─ %{$fg[yellow]%}[$ZSH_THEME_IP]${user} ${pwd} %{$fg_bold[cyan]%}20%D %*%{$reset_color%}"
PROMPT='${PROMPT_PREFIX} ${nl}${ret_status}%{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'

效果图:

image.png


对于 git 提交次数比较的git库 zsh切换时会很慢:

解决方法是在 git 的目录下 执行以下: 

git config --add oh-my-zsh.hide-dirty 1 
git config --add oh-my-zsh.hide-status 1

image.png



3.  格式化并挂载硬盘 

fdisk /dev/xvdb
mkfs.ext4 /dev/xvdb1
mount /dev/xvdb1  /srv/
vi /etc/fstab
添加类似: 根据实际情况而定
/dev/xvdb1  /srv/   ext4   defaults    0 0


4. 系统打开的文件数优化:

cat <<EOF >> /etc/security/limits.conf
*               soft    nofile          65532
*               hard    nofile          65532 
EOF


5. 内网间设置ssh 免密码登录

生成私钥和公钥:  ssh-keygen

将公钥复制到指定服务器: 

ssh-copy-id -i  ~/.ssh/id_rsa.pub  root@xxx.xxx.xxx.xxx 

也可以直接复制 id_rsa.pub 的数据到指定服务器的 authorized_keys 文件里即可


6. 磁盘速度简单测试: 写20G的文件

 dd bs=1M count=20000 if=/dev/zero of=test.dd

image.png



如果打不开github,可以添加以下hosts试下:

#140.82.112.3   github.com

#140.82.112.3   www.github.com

20.205.243.166  github.com

20.205.243.166  www.github.com


7.  安装 autojump 提升效率: 

方法1: yum install autojump autojump-zsh -y


方法2: 

更多版本:  https://github.com/wting/autojump/tags


wget https://github.com/wting/autojump/archive/refs/tags/release-v22.5.3.tar.gz

tar -xvf release-v22.5.3.tar.gz

cd autojump-release-v22.5.3

python install.py


将 Please manually add the following line(s) to ~/.zshrc: 下的文件

复制到 对应的 .zshrc 或 .bashrc中 

image.png


命令执行: 

source ~/.zshrc

j -v

image.png


autojump 的可选参数:
-h:显示帮助信息并退出
-a DIRECTORY:添加路径
-i [WEIGHT]:增加当前目录权重
-d [WEIGHT]:降低当前目录权重
--complete:用于 Tab 补全
--purge:删除数据库中不存在的路径
-s:显示数据库条目及其权重
-v:显示版本信息
j 是 autojump 命令的缩写,任何可以用 autojump 的地方都可以以j命令替代。


添加trzsz 方便上传: 

echo '[trzsz]
name=Trzsz Repo
baseurl=https://yum.fury.io/trzsz/
enabled=1
gpgcheck=0' | sudo tee /etc/yum.repos.d/trzsz.repo

sudo yum install trzsz