Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write a Shell script for Linux system security settings

Shulou Source: shulou.com Published: 2022-06-03 08:18:47 09月29日 Update

This article introduces the knowledge of "how to write a Shell script for Linux system security settings". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

We make the commonly used system security configuration as a shell script, and we only need to run this shell script on the server to complete the security settings.

Linux's system security setting Shell script is the second update, which has been widely used in a large media website system, adding some security settings that were not thought of before. Use the method to copy it and save it as a shell file, such as security.sh. Upload it to the Linux server, execute sh security.sh, and you can use the script!

The code is as follows:

#! / bin/sh

# desc: setup linux system security

# account setup

Passwd-l xfs

Passwd-l news

Passwd-l nscd

Passwd-l dbus

Passwd-l vcsa

Passwd-l games

Passwd-l nobody

Passwd-l avahi

Passwd-l haldaemon

Passwd-l gopher

Passwd-l ftp

Passwd-l mailnull

Passwd-l pcap

Passwd-l mail

Passwd-l shutdown

Passwd-l halt

Passwd-l uucp

Passwd-l operator

Passwd-l sync

Passwd-l adm

Passwd-l lp

# chattr / etc/passwd / etc/shadow

Chattr + I / etc/passwd

Chattr + I / etc/shadow

Chattr + I / etc/group

Chattr + I / etc/gshadow

# add continue input failure 3, passwd unlock time 5 minite

Sed-I 's#auth required pam_env.so#auth required pam_env.sonauth required pam_tally.so onerr=fail deny=3 unlock_time=300nauth required / lib/security/$ISA/pam_tally.so onerr=fail deny=3 unlock_time=300#' / etc/pam.d/system-auth

# system timeout 5 minite auto logout

Echo "TMOUT=300" > > / etc/profile

# will system save history command list to 10

Sed-I "s/HISTSIZE=1000/HISTSIZE=10/" / etc/profile

# enable / etc/profile go!

Source / etc/profile

# add syncookie enable / etc/sysctl.conf

Echo "net.ipv4.tcp_syncookies=1" > > / etc/sysctl.conf

Sysctl-p # exec sysctl.conf enable

# optimizer sshd_config

Sed-I "s/#MaxAuthTries 6/MaxAuthTries 6" / etc/ssh/sshd_config

Sed-I "s/#UseDNS yes/UseDNS no/" / etc/ssh/sshd_config

# limit chmod important commands

Chmod 700 / bin/ping

Chmod 700 / usr/bin/finger

Chmod 700 / usr/bin/who

Chmod 700 / usr/bin/w

Chmod 700 / usr/bin/locate

Chmod 700 / usr/bin/whereis

Chmod 700 / sbin/ifconfig

Chmod 700 / usr/bin/pico

Chmod 700 / bin/vi

Chmod 700 / usr/bin/which

Chmod 700 / usr/bin/gcc

Chmod 700 / usr/bin/make

Chmod 700 / bin/rpm

# history security

Chattr + a / root/.bash_history

Chattr + I / root/.bash_history

# write important command md5

Cat > list > / var/log/ `hostname`.log

Fi

Done

Rm-f list

This is the end of "how to write a Shell script for Linux system security settings". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Security scripts systems content more servers knowledge websites services practicality learning next code system usage dilemma media actual commonly used situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB MySQL Huawei vpn