Table of Contents
3. Source code" >3. Source code
Home Operation and Maintenance CentOS CentOS 7 system optimization script

CentOS 7 system optimization script

Aug 02, 2023 pm 05:22 PM
centos centos7


CentOS 7 system optimization script


1. Introduction

As an operation and maintenance person, we often deploy operating systems for various purposes. However, in these tasks, we will find that many tasks are actually repetitive labor, and the content of the operations is similar. Based on this type of work, In this case, we can make the same operation into a script for unified execution, and different things can be entered manually as variables. Can’t the time saved be used to do more meaningful things?

Recently, I found a relatively easy-to-use shell source code based on the recommendation of fans. I adapted it based on this and shared it with everyone.

2. Menu

Main menu:

CentOS 7 system optimization script

Secondary menu:

CentOS 7 system optimization script

# Mainly realizes various optimizations of the system, such as commonly used functions such as modifying character sets, turning off selinux, turning off firewalls, installing commonly used tools, and accelerating SSH login. .

1

牛逼啊!接私活必备的 N 个开源项目!赶快收藏吧

Copy after login

3. Source code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

#!/bin/sh

 

. /etc/rc.d/init.d/functions

export LANG=zh_CN.UTF-8

 

#一级菜单

menu1()

{

        clear

        cat <<eof

----------------------------------------

|****   欢迎使用cetnos7.9优化脚本    ****|

|****      博客地址: aaa.al         ****|

----------------------------------------

1. 一键优化

2. 自定义优化

3. 退出

EOF

        read -p "please enter your choice[1-3]:" num1

}

 

#二级菜单

menu2()

{

 clear

 cat <<eof

----------------------------------------

|****Please Enter Your Choice:[0-13]****|

----------------------------------------

1. 修改字符集

2. 关闭selinux

3. 关闭firewalld

4. 精简开机启动

5. 修改文件描述符

6. 安装常用工具及修改yum源

7. 优化系统内核

8. 加快ssh登录速度

9. 禁用ctrl+alt+del重启

10.设置时间同步

11.history优化

12.返回上级菜单

13.退出

EOF

 read -p "please enter your choice[1-13]:" num2

 

}

 

#1.修改字符集

localeset()

{

 echo "========================修改字符集========================="

 cat > /etc/locale.conf <<eof

LANG="zh_CN.UTF-8"

#LANG="en_US.UTF-8"

SYSFONT="latarcyrheb-sun16"

EOF

 source /etc/locale.conf

 echo "#cat /etc/locale.conf"

 cat /etc/locale.conf

 action "完成修改字符集" /bin/true

 echo "==========================================================="

 sleep 2

}

 

#2.关闭selinux

selinuxset()

{

 selinux_status=`grep "SELINUX=disabled" /etc/sysconfig/selinux | wc -l`

 echo "========================禁用SELINUX========================"

 if [ $selinux_status -eq 0 ];then

  sed  -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/sysconfig/selinux

  setenforce 0

  echo &#39;#grep SELINUX=disabled /etc/sysconfig/selinux&#39;

  grep SELINUX=disabled /etc/sysconfig/selinux

  echo &#39;#getenforce&#39;

  getenforce

 else

  echo &#39;SELINUX已处于关闭状态&#39;

  echo &#39;#grep SELINUX=disabled /etc/sysconfig/selinux&#39;

                grep SELINUX=disabled /etc/sysconfig/selinux

                echo &#39;#getenforce&#39;

                getenforce

 fi

  action "完成禁用SELINUX" /bin/true

 echo "==========================================================="

 sleep 2

}

 

#3.关闭firewalld

firewalldset()

{

 echo "=======================禁用firewalld========================"

 systemctl stop firewalld.service &> /dev/null

 echo &#39;#firewall-cmd  --state&#39;

 firewall-cmd  --state

 systemctl disable firewalld.service &> /dev/null

 echo &#39;#systemctl list-unit-files | grep firewalld&#39;

 systemctl list-unit-files | grep firewalld

 action "完成禁用firewalld,生产环境下建议启用!" /bin/true

 echo "==========================================================="

 sleep 5

}

 

#4.精简开机启动

chkset()

{

 echo "=======================精简开机启动========================"

 systemctl disable auditd.service

 systemctl disable postfix.service

 systemctl disable dbus-org.freedesktop.NetworkManager.service

 echo &#39;#systemctl list-unit-files | grep -E "auditd|postfix|dbus-org\.freedesktop\.NetworkManager"&#39;

 systemctl list-unit-files | grep -E "auditd|postfix|dbus-org\.freedesktop\.NetworkManager"

 action "完成精简开机启动" /bin/true

 echo "==========================================================="

 sleep 2

}

 

#5.修改文件描述符

limitset()

{

 echo "======================修改文件描述符======================="

 echo &#39;* - nofile 65535&#39;>/etc/security/limits.conf

 ulimit -SHn 65535

 echo "#cat /etc/security/limits.conf"

 cat /etc/security/limits.conf

 echo "#ulimit -Sn ; ulimit -Hn"

 ulimit -Sn ; ulimit -Hn

 action "完成修改文件描述符" /bin/true

 echo "==========================================================="

 sleep 2

}

 

#6.安装常用工具及修改yum源

yumset()

{

 echo "=================安装常用工具及修改yum源==================="

 yum install wget -y &> /dev/null

 if [ $? -eq 0 ];then

  cd /etc/yum.repos.d/

  \cp CentOS-Base.repo CentOS-Base.repo.$(date +%F)

  ping -c 1 mirrors.aliyun.com &> /dev/null

  if [ $? -eq 0 ];then

   wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null

   yum clean all &> /dev/null

   yum makecache &> /dev/null

  else

   echo "无法连接网络"

       exit $?

    fi

 else

  echo "wget安装失败"

  exit $?

 fi

 yum -y install ntpdate lsof net-tools telnet vim lrzsz tree nmap nc sysstat &> /dev/null

 action "完成安装常用工具及修改yum源" /bin/true

 echo "==========================================================="

 sleep 2

}

 

#7. 优化系统内核 #另外,搜索公众号技术社区后台回复“壁纸”,获取一份惊喜礼包。kernelset()

{

 echo "======================优化系统内核========================="

 chk_nf=`cat /etc/sysctl.conf | grep conntrack |wc -l`

 if [ $chk_nf -eq 0 ];then

  cat >>/etc/sysctl.conf<<eof

net.ipv4.tcp_fin_timeout = 2

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_keepalive_time = 600

net.ipv4.ip_local_port_range = 4000 65000

net.ipv4.tcp_max_syn_backlog = 16384

net.ipv4.tcp_max_tw_buckets = 36000

net.ipv4.route.gc_timeout = 100

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_synack_retries = 0

net.core.somaxconn = 16384

net.core.netdev_max_backlog = 16384

net.ipv4.tcp_max_orphans = 16384

net.netfilter.nf_conntrack_max = 25000000

net.netfilter.nf_conntrack_tcp_timeout_established = 180

net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120

net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60

net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120

EOF

 sysctl -p

 else

  echo "优化项已存在。"

 fi

 action "内核调优完成" /bin/true

 echo "==========================================================="

 sleep 2

}

 

#8.加快ssh登录速度

sshset()

{

 echo "======================加快ssh登录速度======================"

 sed -i &#39;s#^GSSAPIAuthentication yes$#GSSAPIAuthentication no#g&#39; /etc/ssh/sshd_config

 sed -i &#39;s/#UseDNS yes/UseDNS no/g&#39; /etc/ssh/sshd_config

 systemctl restart sshd.service

 echo "#grep GSSAPIAuthentication /etc/ssh/sshd_config"

 grep GSSAPIAuthentication /etc/ssh/sshd_config

 echo "#grep UseDNS /etc/ssh/sshd_config"

 grep UseDNS /etc/ssh/sshd_config

 action "完成加快ssh登录速度" /bin/true

 echo "==========================================================="

 sleep 2

}

 

#9. 禁用ctrl+alt+del重启

restartset()

{

 echo "===================禁用ctrl+alt+del重启===================="

 rm -rf /usr/lib/systemd/system/ctrl-alt-del.target

 action "完成禁用ctrl+alt+del重启" /bin/true

 echo "==========================================================="

 sleep 2

}

 

#10. 设置时间同步

ntpdateset()

{

 echo "=======================设置时间同步========================"

 yum -y install ntpdate &> /dev/null

 if [ $? -eq 0 ];then

  /usr/sbin/ntpdate time.windows.com

  echo "*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null" >> /var/spool/cron/root

 else

  echo "ntpdate安装失败"

  exit $?

 fi

 action "完成设置时间同步" /bin/true

 echo "==========================================================="

 sleep 2

}

 

#11. history优化

historyset()

{

 echo "========================history优化========================"

 chk_his=`cat /etc/profile | grep HISTTIMEFORMAT |wc -l`

 if [ $chk_his -eq 0 ];then

  cat >> /etc/profile <<&#39;EOF&#39;

#设置history格式

export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`whoami`] [`who am i|awk &#39;{print $NF}&#39;|sed -r &#39;s#[()]##g&#39;`]: "

#记录shell执行的每一条命令

export PROMPT_COMMAND=&#39;\

if [ -z "$OLD_PWD" ];then

    export OLD_PWD=$PWD;

fi;

if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then

    logger -t `whoami`_shell_dir "[$OLD_PWD]$(history 1)";

fi;

export LAST_CMD="$(history 1)";

export OLD_PWD=$PWD;&#39;

EOF

  source /etc/profile

 else

  echo "优化项已存在。"

 fi

 action "完成history优化" /bin/true

 echo "==========================================================="

 sleep 2

}

 

#控制函数

main()

{

 menu1

 case $num1 in

  1)

   localeset

   selinuxset

   firewalldset

   chkset

   limitset

   yumset

   kernelset

   sshset

   restartset

   ntpdateset

   historyset

   ;;

  2)

   menu2

   case $num2 in

                  1)

                    localeset

                    ;;

                  2)

                    selinuxset

                    ;;

                  3)

                    firewalldset

                    ;;

                  4)

                    chkset

                    ;;

                  5)

                    limitset

                    ;;

                  6)    

            yumset

                    ;;

                  7)

                    kernelset

                    ;;

                  8)

                    sshset

                    ;;

                  9)

                    restartset

                    ;;

                  10)

                    ntpdateset

                    ;;

    11)

      historyset

      ;;

    12)

      main

      ;;

    13)

      exit

      ;;

    *)

      echo &#39;Please select a number from [1-13].&#39;

      ;;

   esac

   ;;

  3)

   exit

   ;;

  *)

   echo &#39;Err:Please select a number from [1-3].&#39;

   sleep 3

   main

   ;;

 esac

}

main $*

Copy after login

Save it as init.sh, then grant execution permission and execute it.

1

chmod +x init.sh && ./init.sh

Copy after login

If it is troublesome to copy and paste back and forth like this, you can also execute it through my one-click command, which can also achieve the above effect:

1

bash -c "$(curl -L s.aaa.al/init.sh)"

Copy after login

Finally, if you have any functions you want to implement, It can also be modified and implemented based on the original script.

The above is the detailed content of CentOS 7 system optimization script. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Centos configuration IP address Centos configuration IP address Apr 14, 2025 pm 09:06 PM

Steps to configure IP address in CentOS: View the current network configuration: ip addr Edit the network configuration file: sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0 Change IP address: Edit IPADDR= Line changes the subnet mask and gateway (optional): Edit NETMASK= and GATEWAY= Lines Restart the network service: sudo systemctl restart network verification IP address: ip addr

How to optimize CentOS HDFS configuration How to optimize CentOS HDFS configuration Apr 14, 2025 pm 07:15 PM

Improve HDFS performance on CentOS: A comprehensive optimization guide to optimize HDFS (Hadoop distributed file system) on CentOS requires comprehensive consideration of hardware, system configuration and network settings. This article provides a series of optimization strategies to help you improve HDFS performance. 1. Hardware upgrade and selection resource expansion: Increase the CPU, memory and storage capacity of the server as much as possible. High-performance hardware: adopts high-performance network cards and switches to improve network throughput. 2. System configuration fine-tuning kernel parameter adjustment: Modify /etc/sysctl.conf file to optimize kernel parameters such as TCP connection number, file handle number and memory management. For example, adjust TCP connection status and buffer size

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

Centos shutdown command line Centos shutdown command line Apr 14, 2025 pm 09:12 PM

The CentOS shutdown command is shutdown, and the syntax is shutdown [Options] Time [Information]. Options include: -h Stop the system immediately; -P Turn off the power after shutdown; -r restart; -t Waiting time. Times can be specified as immediate (now), minutes ( minutes), or a specific time (hh:mm). Added information can be displayed in system messages.

CentOS HDFS performance tuning tips CentOS HDFS performance tuning tips Apr 14, 2025 pm 06:00 PM

CentOS Platform Hadoop Distributed File System (HDFS) Performance Optimization Guide Optimizing HDFS Performance is a multi-faceted issue, and multiple parameters need to be adjusted for specific situations. The following are some key optimization strategies: 1. Memory management adjusts the NameNode and DataNode memory configuration: reasonably configure the HADOOP_NAMENODE_OPTS and HADOOP_DATANODE_OPTS environment variables according to the actual memory size of the server to optimize memory utilization. Enable large page memory: For high memory consumption applications (such as HDFS), enabling large page memory can reduce memory page allocation and management overhead and improve efficiency. 2. Disk I/O optimization uses high-speed storage

Centos stops maintenance 2024 Centos stops maintenance 2024 Apr 14, 2025 pm 08:39 PM

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

How is the GPU support for PyTorch on CentOS How is the GPU support for PyTorch on CentOS Apr 14, 2025 pm 06:48 PM

Enable PyTorch GPU acceleration on CentOS system requires the installation of CUDA, cuDNN and GPU versions of PyTorch. The following steps will guide you through the process: CUDA and cuDNN installation determine CUDA version compatibility: Use the nvidia-smi command to view the CUDA version supported by your NVIDIA graphics card. For example, your MX450 graphics card may support CUDA11.1 or higher. Download and install CUDAToolkit: Visit the official website of NVIDIACUDAToolkit and download and install the corresponding version according to the highest CUDA version supported by your graphics card. Install cuDNN library:

What files do you need to modify in HDFS configuration CentOS? What files do you need to modify in HDFS configuration CentOS? Apr 14, 2025 pm 07:27 PM

When configuring Hadoop Distributed File System (HDFS) on CentOS, the following key configuration files need to be modified: core-site.xml: fs.defaultFS: Specifies the default file system address of HDFS, such as hdfs://localhost:9000. hadoop.tmp.dir: Specifies the storage directory for Hadoop temporary files. hadoop.proxyuser.root.hosts and hadoop.proxyuser.ro

See all articles