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


安装源:  https://developer.aliyun.com/mirror/centos

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum -y install epel-release
yum makecache

OR 

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum -y install epel-release
yum makecache


关闭selinux:

vim /etc/selinux/config # 修改disable

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

yum install -y atop git nss curl tmux zsh lrzsz dstat libcurl htop iotop atop nload nethogs iperf
ldconfig

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=2
set shiftwidth=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.121.3github.com

#140.82.121.3 www.github.com

#140.82.121.4 github.com

#140.82.121.4 www.github.com

#140.82.113.4    github.com

#140.82.113.4    www.github.com

#20.205.243.166 github.com

#20.205.243.166www.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


Centos8默认安装的是podman, 但podman与docker-compose 存在各种各样的不兼容: 

各种升级python版本后, 还是(TypeError: kwargs_from_env() got an unexpected keyword argument 'ssl_version')错误, 

下载docker-compose 直接安装, 又提示没有 (Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?)



建议方法:


yum erase podman buildah

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce

如果安装gitlab: 
yum install docker-ce-3:24.0.9-1.el8

systemctl enable docker
systemctl start docker


把docker从24版本升级至26版本之后从客户端 又报错: 

╰─ ➜ gitlab docker pull gitlab/gitlab-ce:8.6.3-ce.0        
8.6.3-ce.0: Pulling from gitlab/gitlab-ce
8.6.3-ce.0: Pulling from gitlab/gitlab-ce
[DEPRECATION NOTICE] Docker Image Format v1 and Docker Image manifest version 2, schema 1 support is disabled by default and will be removed in an upcoming release. Suggest the author of docker.io/gitlab/gitlab-ce:8.6.3-ce.0 to upgrade the image to the OCI Format or Docker Image manifest v2, schema 2. More information at https://docs.docker.com/go/deprecated-image-specs/


查docker-ce 版本 

yum list docker-ce --showduplicates | sort -r

image.png

再次安装:
yum install docker-ce-3:24.0.9-1.el8


docker-compose 下载地址:
https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-linux-x86_64