CentOS System Administration: Common Configuration Tasks

Setting Up YUM Repository

sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
sudo 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
sudo yum makecache

Initial Configuration via Shell Script

A shell script can be used to automate initial setup steps.

Checking Port Usage

# Install netstat tool
yum install net-tools -y
# List listening ports
netstat -lnpt
# Check specific port occupation
netstat -lnpt | grep 5672
# Test port connectivity
telnet 192.168.0.11 50890

Firewalld Commands

# Check firewalld service status
systemctl status firewalld
# Check firewalld runtime state
firewall-cmd --state
# Start firewalld
service firewalld start
# Restart firewalld
service firewalld restart
# Stop firewalld
service firewalld stop
# List firewall rules
firewall-cmd --list-all
# Query if a port is open
firewall-cmd --query-port=8080/tcp
# Open a port (common: 80,443,22,21,3306,8080)
firewall-cmd --permanent --add-port=80/tcp
# Remove a port
firewall-cmd --permanent --remove-port=8080/tcp
# Reload firewall configuration
firewall-cmd --reload
# Parameter explanation:
# --permanent: make setting persistent
# --add-port: add the specified port

Changing Hostname / DNS

# View host information
hostnamectl
# Set hostname
hostnamectl set-hostname newhostname

Viewing DNS

cat /etc/resolv.conf

VIM Usage

# Search for "xxx"
/xxx
# Clear search highlight
:noh

SED Patterns

Insert lines:

# Insert before line 1
sed -i '1i<text>' filename
# Insert after line 1
sed -i '1a<text>' filename
# Insert before/after a matching line
sed -i '/pattern/i<text>' filename
sed -i '/pattern/a<text>' filename
# Append at end of file
sed -i '$a<text>' filename
</text></text></text></text></text>

Delete lines:

# Delete line 1
sed -i '1d' filename
# Delete every 2nd line starting from line 1 (odd lines)
sed -i '1~2d' filename
# Delete lines 1 through 2
sed -i '1,2d' filename
# Delete all except lines 1 and 2
sed -i '1,2!d' filename
# Delete last line
sed -i '$d' filename
# Delete empty lines
sed -i '/^$/d' filename
# Delete lines matching pattern
sed -i '/pattern/d' filename

Replace lines:

# Replace line 1
sed -i '1c<newtext>' filename
# Replace line matching pattern
sed -i '/oldpattern/c<newtext>' filename
# Replace last line
sed -i '$c<newtext>' filename
# It is recommended to use '\' to enclose content.
</newtext></newtext></newtext>

SCP File Tranfser

# Upload file to server
scp /local/path/file user@host:/remote/path/
# Download file from server
scp user@host:/remote/path/file /local/path/
# Download entire directory (-r)
scp -r user@host:/remote/dir /local/path/
# Upload entire directory (-r)
scp -r /local/dir user@host:/remote/path/
# Run scp in background
nohup scp /local/file user@host:/remote/path/
# After entering password, press Ctrl+Z, then run 'bg' command.
# Exit terminal with 'exit' to keep background process running.
# To kill the background process:
ps -aux | grep "scp"
kill -9 <PID>

SSH Password-less Login

# Generate RSA key pair
ssh-keygen -t rsa
# Copy public key to remote server
ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote_ip
# Check remote authorized_keys
cat ~/.ssh/authorized_keys
# Ensure proper permissions on remote:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Preventing SSH Timeout

# Edit /etc/ssh/sshd_config and add:
ClientAliveInterval 60
ClientAliveCountMax 86400
# Restart SSH service
service sshd restart
# These settings send keepalive every 60 seconds and disconnect after 86400 missed responses.

Cron Jobs Initialization

If crontab -l shows no crontab for root, run crontab -e once, save an empty file, then crontab -l will work.

NTP Time Synchronization

# Check if ntp is installed
rpm -qa ntp
# Install if not present
yum -y install ntp
# Edit /etc/ntp.conf:
# Comment out the line starting with 'restrict default kod...' and add:
restrict default nomodify
# Comment out default server lines, add custom:
server time1.aliyun.com
# Remove any cron job that syncs time (to avoid conflict)
crontab -l | grep -v ntpdate > /tmp/cron.tmp; crontab /tmp/cron.tmp
# Enable and start NTP service
systemctl enable ntpd.service && systemctl start ntpd.service
# Check NTP status
ntpq -p
# After a few minutes, verify synchronization
ntpstat
# For clients, wait several minutes before running ntpdate
ntpdate 192.168.66.11

Upgrading Git on CentOS

# For CentOS 6:
yum install http://opensource.wandisco.com/centos/6/git/x86_64/wandisco-git-release-6-1.noarch.rpm
# For CentOS 7:
yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-1.noarch.rpm
# or
yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
# Then install/update git
yum install git

Full Upgrade Procedure for CentOS 7

yum remove git
rpm -ivh http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-1.noarch.rpm
yum install git -y

Common Environment Variables

export JAVA_HOME=/usr/local/java/jdk1.8.0_361
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

export MAVEN_HOME=/usr/local/apache-maven-3.8.8
export PATH=${PATH}:${MAVEN_HOME}/bin

export NODE_HOME=/usr/local/node/node-v14.21.3-linux-x64
export PATH=${NODE_HOME}/bin:$PATH

Tags: centos Yum firewalld ssh cron

Posted on Sun, 10 May 2026 04:38:23 +0000 by JukEboX