Linux Administration Tips and Solutions Collection

1. Implementing Time-Based Acces Control for SSH Users

To enable remote login time control for different SSH accounts, follow these steps:

# 1. Enable time-based access control for sshd
# Add the following configuration to /etc/pam.d/sshd and restart sshd service with systemctl restart sshd
account    required     pam_time.so
# Date representations
Mo(Monday), Tu(Tuesday), We(Wednesday), Th(Thursday), Fr(Friday), Sa(Saturday), Su(Sunday), Wk(Weekend), Wd(Weekday), Al(All days)

# 2. Example usage; default configuration file /etc/security/time.conf
# Allow user admin01 to access server via ssh on weekdays from 9am to 6pm
sshd;*;admin01;Wd0900-1800
# Allow user admin02 to access server via ssh on weekends from 9am to 6pm
sshd;*;admin02;Wk0900-1800
# Deny user admin03 ssh access from 10pm to midnight on any day
sshd;*;admin03;!Al2200-0000
# Only allow user admin04 ssh login from Monday to Friday between 6am and 10pm
sshd;*;admin04;MoTuWe0600-2200

2. Counting Source Code Lines in Major Open Source Projects

To count lines of code in projects like MySQL, Redis, Linux Kernel, or Kubernetes:

# Project repository addresses
[cloc](https://github.com/AlDanial/cloc)
[scc](https://github.com/boyter/scc)
# Installation
apt/snap install cloc/scc

# Execute in the root directory of the project to be counted. The 'time' command shows how long cloc took to execute. Recommended approach:
time cloc .
time scc .

3. Creating Virtual Hard Disks from System Memory

Implementation principle: Using software technology to transform a portion of physical memory into virtual storage to significantly improve disk data read speed.

Use case: As a temporary file directory to improve read/write speed while avoiding frequent physical disk writes, which reduces disk lifespan.

# On Windows, use the open-source tool ImDisk
[ImDiskTk-x64 Download Link](https://sourceforge.net/projects/imdisk-toolkit/files/20220826/ImDiskTk-x64.zip)

# On Linux, use the following commands:
mkdir /mnt/vmem
mount -t tmpfs -o size=2G tmpfs /mnt/vmem/

4. Troubleshooting Issues with /etc/rc.d/rc.local Not Executing at Boot

# 1. Add execute permissions to the file
chmod +x /etc/rc.d/rc.local

# 2. Ensure the rc-local service is enabled at boot
systemctl enable rc-local && systemctl start rc-local

# 3. Check script syntax and execution errors in rc.local
If the rc-local service fails to start normally at boot, you can check the specific reason with the command `journalctl -u rc-local`
Usually, this is due to incorrect syntax in scripts added to `/etc/rc.d/rc.local` or errors during script execution, such as when adding custom iptables rules that report "chain not found"!!

5. Fixing Chinese Character Display Issues in Vim

When Vim opens a file and displays Chinese characters as garbled text, create a file ~/.vimrc in your home directory and add the following content:

set termencoding=utf-8
set encoding=utf8
set fileencodings=utf8,ucs-bom,gbk,cp936,gb2312,gb18030

6. Verifying SSD TBW Parameter

The TBW (Total Bytes Written) parameter for SSDs indicates the total amount of data that can be written to the SSD during its lifespan. How can we verify this parameter when purchasing new or used SSDs?

# Install the TBW detection tool for hard drives
yum/apt install smartmontools

smartctl -A /dev/sda
> 241 Total_LBAs_Written      0x0032   099   099   000    Old_age   Always       -       18657690420
# Total_LBAs_Written represents the total amount of data written to the disk since manufacturing, in LBAs = 512Byte (0.5KB). The example shows 9TB of data has been written.

7. Managing CPU Resources Through Process Control

By pausing and resuming processes, you can effectively manage CPU resources:

stress-ng -c 3	# Start three processes, each using 100% CPU
kill -STOP 8728	# Pause process, releasing CPU resources
kill -CONT 8728	# Resume process, reclaiming CPU resources

8. Quickly Retrieving External IP Address (NAT Translated IP)

# Alternative query addresses: ifconfig.me

curl -4 ifconfig.co
#> 39.103.168.25

curl -6 ifconfig.co
#> 2408:4001:249:3500:deb6:cafa:75b3:11be

9. Understanding dpkg Output: Meaning of 'rc' Status

In Ubuntu systems, when executing the command dpkg -l |grep nginx, the output shows 'rc' in the first column. What does this mean?

rc  libnginx-mod-http-image-filter       1.18.0-0ubuntu1.3                 amd64        HTTP image filter module for Nginx
rc  libnginx-mod-http-xslt-filter        1.18.0-0ubuntu1.3                 amd64        XSLT Transformation module for Nginx
rc  libnginx-mod-mail                    1.18.0-0ubuntu1.3                 amd64        Mail module for Nginx
rc  libnginx-mod-stream                  1.18.0-0ubuntu1.3                 amd64        Stream module for Nginx
rc  nginx-common                         1.18.0-0ubuntu1.3                 all          small, powerful, scalable web/proxy server - common files 

R: Indicates the software package has been removed (Removed). C: Indicates the package's configuration files still exist. To completely remove these packages, run the following command:

sudo apt-get purge libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx-common

10. Manually Configuring Static IP Address in Rocky Linux 9/AlmaLinux 9

# Edit /etc/NetworkManager/system-connections/ens3.nmconnection and add the following configuration items
...
[ipv4]
method=manual
address1=192.168.31.93/24,192.168.31.1
dns=223.5.5.5
...

# Apply the configuration
nmcli connection reload ens3.nmconnection
# Check interface information
nmcli device show

11. Preventing NetworkManager from Overriding DNS Configuration

To prevent NetworkManager from using DNS servers obtained through dhclient to configure the system's DNS:

# 1. Add the following configuration to /etc/NetworkManager/NetworkManager.conf
[main]
dns = none

# 2. Remove automatically generated nameserver settings from /etc/resolv.conf

# 3. Manually configure DNS servers in /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1=223.5.5.5
DNS2=8.8.8.8

# 4. systemctl restart NetworkManager

12. Fixing Garbled Text When Using cat Command

When using the cat command to view files and encountering garbled text, the solution is as follows:

cat ~/.bashrc
export LANG=en_US.UTF-8
# After adding this configuration, exit the current shell and re-enter to take effect

Git Configurasion

1. Setting Up Git Proxy

# If you use HTTP or HTTPS protocols to connect to Git repositories
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy http://127.0.0.1:1080

# If you use SOCKS5 protocol to connect to Git repositories
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080

# To view current proxy settings
git config --global --get http.proxy
git config --global --get https.proxy

# To remove proxy settings
git config --global --unset http.proxy
git config --global --unset https.proxy

2. Git Configuration File Priority Order

--local
# Scope is the current Git repository. Configuration items are saved in the .git/config file of the current repository and are only effective for the current repository

--global
# Sets Git configuration with global scope. Configuration items are saved in your .gitconfig file in your home directory (~/.gitconfig), and are effective for any Git repository operated by the current system user

--system
# Sets Git configuration with system scope. Configuration items are saved in the system-wide Git configuration file, usually located at /etc/gitconfig, and are effective for any system user and any Git repository

3. Git Credential Helper Modes

You can specify which method to use for storing and retrieving credentials through the credential.helper configuration item.

1. Store mode: Stores credentials in plain text on disk (default location is user home directory ~/.git-credentials) and never expires.

2. Cache mode: Stores credentials in memory for a period of time (default is 15 minutes, after which they are cleared from memory) to automatically provide credentials.

3. Manager mode: Stores credentials in the system user's credential manager. This method encrypts credentials before storing them on disk and they never expire.

Configuration commands:

git config credential.helper {store | cache | manager}


For cache mode, you can configure the timeout:

git config --global credential.cacheOptions "--timeout 300"

4. GitHub Authentication Using SSH Public Keys

# 1. Generate a public key locally using ssh-keygen
cat ~/.ssh/id_rsa.pub

# 2. Log in to your GitHub account, go to Settings > SSH and GPG keys, click New SSH key, paste the copied public key into the Key field, add a title, and click Add SSH key to save the configuration

# 3. Change the remote (origin) repository URL to SSH format for the local GitHub repository !!
git remote -v
#> origin  https://github.com/demo/test.git (fetch)
#> origin  https://github.com/demo/test.git (push)
git remote set-url origin git@github.com:demo/test.git

Tags: Linux ssh Git System Administration Network Configuration

Posted on Mon, 26 Oct 2026 16:31:23 +0000 by saidbakr