CentOS系统优化

优化ssh

[ops@kvm01 ~]$ sudo egrep -v "^#|^$" /etc/ssh/sshd_config
## 修改ssh端口
Port 58022
## 禁止root远程登录
PermitRootLogin no
## 优化ssh连接速度
GSSAPIAuthentication no
UseDNS no
  • 修改配置文件需要重启 sshd 的服务,重启后连接并不会断
  • 重启后不要马上关闭连接,先打开新的页面连接是否可以连上

设置普通用户sudo免密

echo 'ops    ALL=(ALL)       NOPASSWD: ALL' > /etc/sudoers.d/ops
  • /etc/sudoers.d 目录下的文件,必须与用户名一致。

修改yum源

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
curl http://mirrors.aliyun.com/repo/epel-7.repo -o /etc/yum.repos.d/epel.repo
yum clean all
yum makecache

下载常用的命令

sudo yum install -y net-tools vim wget lsof telnet bash-completion-extras rsync sysstat

设置ps

cat <<EOF | sudo tee /etc/profile.d/env.sh > /dev/null
export PS1="\[\e[37;1m\][\[\e[35;1m\]\u\[\e[37;1m\]@\[\e[32;1m\]\h \[\e[36;1m\]\w\[\e[37;1m\]]\[\e[37;1m\]\\\\$ \[\e[0m\]"
EOF

history持久化

cat <<EOF | sudo tee /etc/profile.d/history.sh > /dev/null
## 获取当前登录用户ip
USER_IP=\$(who -u am i 2>/dev/null| awk '{print \$NF}'|sed -e 's/[()]//g')
if [ -z \$USER_IP ];then
  USER_IP="NO_client_IP"
fi

HISTORY_DIR=/opt/hislog
## 判断文件夹是否存在 不存在则创建
if [ ! -d \$HISTORY_DIR ];then
  sudo mkdir \$HISTORY_DIR
  sudo chmod 777 \$HISTORY_DIR
fi

## 为用户创建目录
if [ ! -d \$HISTORY_DIR/\${LOGNAME} ];then
  mkdir \$HISTORY_DIR/\${LOGNAME}
  chmod 300 \$HISTORY_DIR/\${LOGNAME}
fi

## 写入的文件名,使用同一个名字否则history无法展示历史记录
export HISTFILE="\$HISTORY_DIR/\${LOGNAME}/history_\$USER_IP"

## 使用追加模式写入
shopt -s histappend

## 每执行一次写入文件
PROMPT_COMMAND="history -a"

## 设置history命令格式
HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] "

## history文件保存的行数
HISTFILESIZE=50000

## history命令输出的行数
HISTSIZE=4096
EOF

注意:HISTORY_DIR 目录如果不存在 且 下次登录用户没有 sudo 权限,则会报错。
原来的 history 记录不会在 history 命令显示,但是可以通过 cat ~/.bash_history 来查看。

优化vim

sudo cp /etc/vimrc{,.bak}
cat <<-EOF | sudo tee -a /etc/vimrc > /dev/null
" 以下是自定配置
set completeopt=preview,menu "代码补全
set smartindent   "开启新行时使用智能自动缩进 set autoindent自动缩进,等价于set ai
set number        "显示行号,vim临时添号行号可以写成set nu,去除行号可以写成set nonu
set ignorecase    "搜索忽略大小写
set ruler         "显示标尺
set showcmd       "输入的命令显示出来,看的清楚些
set tabstop=4     "Tab键的宽度
set shiftwidth=4  "这个是用于程序中自动缩进所使用的空白长度指示的
set softtabstop=4 "按下tab键,插入的是空格和tab制表符的混合
set cursorline    "设置光标行线
function AddshTitle()
call setline(1,"#!/bin/bash")
call setline(2,"# FileName: " . expand("%"))
call setline(3,"# Author: jiaxzeng")
call setline(4,"# Date: " . strftime("%F %T"))
call setline(5,"")
call setline(6,"############## Begin Write Script ##############")
call setline(7,"")
endf
function AddpyTitle()
call setline(1,"#!/usr/bin/env python")
call setline(2,"# encoding: utf-8")
call setline(3,"# FileName: " . expand("%"))
call setline(4,"# Author: jiaxzeng")
call setline(5,"# Date: " . strftime("%F %T"))
call setline(6,"")
call setline(7,"############## Begin Write Script ##############")
call setline(8,"")
endf
autocmd BufNewFile *.sh exec ":call AddshTitle()"
autocmd BufNewFile *.py exec ":call AddpyTitle()"
autocmd BufNewFile * normal G 
EOF
posted @ 2021-12-11 11:40  jiaxzeng  阅读(121)  评论(0)    收藏  举报