linux系统最小化安装之基础优化

话题2:linux之centos系统常用基础优化

1. 设置命令提示符颜色

1 echo 'PS1="\[\e[1;31m\][\u@\h \W]\\$\[\e[0m\]"' >> /etc/profile.d/env.sh
2 #使其立即生效
3 . /etc/profile.d/env.sh

2. 设置系统默认编辑器为vim

1 echo export EDITOR=vim >> /etc/profile.d/env.sh
2 . /etc/profile.d/env.sh

3. 禁用SELinux

1 sed -ri 's/^(SELINUX=)enforcing/\1disabled/'  /etc/selinux/config
2 setenforce 0

4. 禁用防火墙

1 systemctl stop firewalld.service
2 systemctl disable firewalld.service

5. 优化ssh登录

1 sed -ri 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
2 sed -ri 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
#ubuntu修改允许root登录
sed -ri 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
systemctl restart ssh

6. 修改网卡名称并最小化网卡配置

 1 #1. 修改网卡名称为传统的网卡名称eth0,eth...
 2 sed -ri '/CMDLINE/s#(.*)"#\1 net.ifnames=0"#' /etc/default/grub
 3 grub2-mkconfig -o /etc/grub2.cfg
 4 #2. 需要修改/etc/sysconfig/network-scripts/ifcfg-???文件名
 5 #3. 需要重启系统
 6 echo -e "\033[1;32m System initialization is complete and will be reboot in 10s...\033[0m"
 7 sleep 10
 8 reboot
 9 
10 
11 #网卡最小配置范例
12 [root@centos7 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 
13 TYPE="Ethernet" 
14 NAME="eth0" 
15 DEVICE="eth0" 
16 ONBOOT="yes" 
17 BOOTPROTO="static" 
18 IPADDR=192.168.2.196 
19 GATEWAY=192.168.2.1 
20 PREFIX=24 
21 DNS1=192.168.2.1 
22 
23 
24 #加入alias 
25 [root@centos7 ~]# alias vieth0='vim /etc/sysconfig/network-scripts/ifcfg-eth0'

 

7. yum源配置

1 #安装wget工具
2 yum -y install wget
3 
4 #配置系统使用阿里云yum源和EPEL源
5 mkdir /etc/yum.repos.d/bak
6 mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
7 wget -P /etc/yum.repos.d http://mirrors.aliyun.com/repo/Centos-7.repo
8 wget -P /etc/yum.repos.d http://mirrors.aliyun.com/repo/epel-7.repo
9 yum makecache

9. 时间同步

1 #安装chrony时间同步服务
2 yum -y install chrony && systemctl enable --now chronyd.service
3 #配置chrony时间同步阿里云ntp服务器
4 sed -i -e '/^server/s/^/#/'  -e '1a server ntp.aliyun.com iburst' /etc/chrony.conf
5 systemctl restart chronyd.service

10. 编写脚本的自动开头

 1 vim ~/.vimrc
#复制如下代码修改称自己的即可
2 set ignorecase 3 set cursorline 4 set autoindent 5 autocmd BufNewFile *.sh exec ":call SetTitle()" 6 func SetTitle() 7 if expand("%:e") == 'sh' 8 call setline(1,"#!/bin/bash") 9 call setline(2,"#") 10 call setline(3,"#********************************************************************") 11 call setline(4,"#Author: zouyongbing") 12 call setline(5,"#QQ: 273838882") 13 call setline(6,"#Date: ".strftime("%Y-%m-%d")) 14 call setline(7,"#FileName: ".expand("%")) 15 call setline(8,"#URL: https://www.cnblogs.com/zouyongbing/") 16 call setline(9,"#Description: The test script") 17 call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved") 18 call setline(11,"#********************************************************************") 19 call setline(12,"") 20 endif 21 endfunc 22 autocmd BufNewFile * normal G

写一个脚本完成上述过程

 1 [root@centos7 scripts]# cat baseset.sh 
 2 #!/bin/bash
 3 #
 4 #********************************************************************
 5 #Author:zouyongbing
 6 #QQ:                 273838882
 7 #Date: 2021-11-03
 8 #FileName:baseset.sh
 9 #URL: https://home.cnblogs.com/zouyongbing
10 #Description:The test script
11 #Copyright (C): 2021 All rights reserved
12 #********************************************************************
13 #1.set yum base and epel
14 sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
15          -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
16          -i.bak \
17          /etc/yum.repos.d/CentOS-*.repo
18  
19 yum install epel-release -y && 
20 sed -e 's!^metalink=!#metalink=!g' \
21     -e 's!^#baseurl=!baseurl=!g' \
22     -e 's!//download\.fedoraproject.org/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
23     -e 's!//download\.example/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
24     -e 's!http://mirrors!https://mirrors!g' \
25     -i.bak  \
26     /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo
27 
28 #2. set alias vinet0
29 echo "alias vinet0='vim /etc/sysconfig/network-scripts/ifcfg-eth0'" >> /etc/profile  && source /etc/profile
30 #3. stop firewalld
31 systemctl stop firewalld && systemctl disable firewalld
32 
33 #4. stop selinux
34 sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config 
35 #sed -ri 's/^(SELINUX=)enforcing/\1disabled/'  /etc/selinux/config
36 
37 #5. ssh ora
38 sed -ri 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
39 sed -ri 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
40 
41 #6. setup tools
42 yum install  vim iotop bc gcc gcc-c++ glibc glibc-devel pcre \
43 pcre-devel openssl  openssl-devel zip unzip zlib-devel  net-tools \
44 lrzsz tree  telnet lsof tcpdump wget libevent libevent-devel \
45 bc  systemd-devel bash-completion traceroute -y
46 
47 #7. set scripts format
48 cat > ~/.vimrc <<-EOF
49 set ignorecase
50 set cursorline
51 set autoindent
52 autocmd BufNewFile *.sh exec ":call SetTitle()"
53 func SetTitle()
54     if expand("%:e") == 'sh'
55     call setline(1,"#!/bin/bash") 
56     call setline(2,"#") 
57     call setline(3,"#********************************************************************") 
58     call setline(4,"#Author:        zouyongbing") 
59     call setline(5,"#QQ:                         273838882") 
60     call setline(6,"#Date:             ".strftime("%Y-%m-%d"))
61     call setline(7,"#FileName:        ".expand("%"))
62     call setline(8,"#URL:             https://home.cnblogs.com/zouyongbing")
63     call setline(9,"#Description:        The test script") 
64     call setline(10,"#Copyright (C):     ".strftime("%Y")." All rights reserved")
65     call setline(11,"#********************************************************************") 
66     call setline(12,"") 
67     endif
68 endfunc
69 autocmd BufNewFile * normal G
70 EOF
71 
72 #8. echo network
73 echo "TYPE=Ethernet
74 BOOTPROTO=none
75 NAME=eth0
76 DEVICE=eth0
77 ONBOOT=yes
78 #modify this ip
79 IPADDR=10.1.100.7
80 PREFIX=24
81 GATEWAY=10.1.100.254
82 DNS1=10.1.100.254
83 " > /etc/sysconfig/network-scripts/ifcfg-eth0
84 
85 echo "base optimize is complete!!!"

 

posted @ 2021-12-03 16:06  运维的终点  阅读(443)  评论(0)    收藏  举报