Operations 39 min read

Master Linux Bash: From User ID Summation to Systemd Services and Encryption

This comprehensive guide walks through essential Linux administration techniques, including Bash scripts for summing user IDs, array and string manipulation, advanced variable usage, random number analysis, factorial recursion, process and thread concepts, job control, kernel design models, boot and GRUB workflows, service management with chkconfig and systemd, as well as CA creation, certificate handling, encryption fundamentals, and OpenSSH key‑based authentication.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Bash: From User ID Summation to Systemd Services and Encryption

1. Calculate User ID Sum

Uses a while loop reading /etc/passwd to extract each user ID and accumulate the total, requiring root privileges.

#!/bin/bash
uid=0
sum=0
while read line; do
    uid=$(echo "$line" | cut -d':' -f3)
    sum=$((sum + uid))
done < /etc/passwd
echo "用户ID总和为:$sum"

2. Bash Arrays, String Processing, and Advanced Variables

Demonstrates indexed and associative arrays, common string operations (${#string}, ${string:start:length}, ${string#substring}, ${string%substring}, ${string/find/replace}, ${string//find/replace}), and advanced parameter expansions (${parameter:=word}, ${parameter:+word}, ${parameter:?msg}).

# Indexed array
my_array=("apple" "banana" "orange")
echo ${my_array[0]}   # apple

# Associative array (Bash 4+)
declare -A colors
colors["red"]="apple"
colors["yellow"]="banana"
echo ${colors["red"]}   # apple

3. Random Number Max/Min

Generates ten random numbers, tracks the maximum and minimum values.

#!/bin/bash
declare -i min max
declare -a nums
for ((i=0;i<10;i++)); do
    nums[i]=$RANDOM
    [ $i -eq 0 ] && min=${nums[0]} && max=${nums[0]} && continue
    [ ${nums[i]} -gt $max ] && max=${nums[i]} && continue
    [ ${nums[i]} -lt $min ] && min=${nums[i]}
done
echo "All numbers are ${nums[*]}"
echo "Max is $max"
echo "Min is $min"

4. Factorial Algorithm

Implements factorial using a recursive function.

#!/bin/bash
function factorial {
    if [ $1 -eq 0 -o $1 -eq 1 ]; then
        echo 1
    else
        echo $[ $1 * $(factorial $[ $1 - 1 ]) ]
    fi
}
factorial $1

5. Processes and Threads

Explains differences: processes have separate memory spaces and are heavyweight; threads share the same address space within a process and are lightweight.

6. Process Structure

Describes code segment, data segment, stack, heap, and PCB.

7. Process States

Running, Ready, Blocked, New, Terminated, with corresponding ps status codes (R, S, D, Z, T).

8. IPC and RPC Communication

IPC methods include pipes, message queues, semaphores, shared memory. RPC methods include client‑server, remote object invocation, and web services.

9. Foreground and Background Jobs

Shows how to start a job in background with '&' and bring it to foreground with fg, or send a foreground job to background with bg.

$ ./my-program &
$ fg %1
$ bg %1

10. Kernel Design Paradigms

Summarizes monolithic, microkernel, and hybrid kernels, highlighting trade‑offs between performance, reliability, and complexity.

11. Linux Boot Process (Rocky)

Outlines BIOS/POST, boot device selection, loading GRUB, menu selection, kernel loading, init, and runlevel configuration.

12. chkconfig Service Script

Provides a sample init script supporting start, stop, restart, and status actions.

#!/bin/bash
# chkconfig: - 96 3
# description: This is test service script

. /etc/init.d/functions

start(){
    [ -e /var/lock/subsys/testsrv ] && exit || touch /var/lock/subsys/testsrv
    action "Starting testsrv"
    sleep 3
}

stop(){
    [ -e /var/lock/subsys/testsrv ] && rm /var/lock/subsys/testsrv || exit
    action "Stopping testsrv"
}

status(){
    [ -e /var/lock/subsys/testsrv ] && echo "testsrv is running..." || echo "testsrv is stopped"
}

case $1 in
    start) start ;;
    stop) stop ;;
    restart) stop; start ;;
    status) status ;;
    *) echo "Usage: $0 {start|stop|status|restart}"; exit 2 ;;
esac

13. systemd Service Unit

Shows a minimal unit file with [Unit], [Service], and [Install] sections and explains common directives.

[Unit]
Description=My Service
After=network.target

[Service]
ExecStart=/usr/local/bin/my-service
Restart=always

[Install]
WantedBy=multi-user.target

14. systemd Startup Flow

Describes BIOS/UEFI → GRUB → kernel → PID 1 (systemd) → unit loading → target → services.

15. AWK Fundamentals

Explains line‑by‑line processing, field splitting, pattern/action syntax, and common options.

16. AWK Arrays and Functions

Shows associative array usage and function definition/call.

# Example: collect fruits
awk -F, '{ if ($2=="fruit") fruits[$1]=$3 } END { for (f in fruits) print f, fruits[f] }' test.txt

17. CA Management Tools

Outlines steps to create a private CA with OpenSSL, generate keys, CSR, sign certificates, revoke them, and update CRL.

18. Encryption Overview

Compares symmetric (DES, 3DES, AES, etc.) and asymmetric (RSA, DSA, ECC) algorithms and presents OpenSSL commands for key and certificate generation.

19. OpenSSH Key‑Based Authentication

Describes generating a key pair, copying the public key to the server, and the challenge‑response flow.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

process managementencryptionOpenSSLBashsystemdawk
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.