Skip to content

0x324 Linux Admin

Bash

Arithmetic

a=5
b=3

# Addition
sum=$((a + b))
echo "Sum: $sum"

For

examples to iterate over a given string args

for file in file1.txt file2.txt file3.txt; do
  do_something_with "$file"
done

FILES="file1.txt file2.txt file3.txt"
for file in $FILES; do
  do_something_with "$file"
done

for file in /path/to/directory/*.txt; do
  do_something_with "$file"
done

# if you need to handle filenames with spaces, you would typically use an array in Bash:
files=("file1.txt" "file 2.txt" "file3.txt") # Note: "file 2.txt" has a space
for file in "${files[@]}"; do
  echo "$file"
done

examples to iterate over numbers

for i in {0..10}; do
  echo "$i"
done

for i in $(seq 0 10); do
  echo "$i"
done

5.1. Find

Usage examples

# concat all files matching duration into a single file
# last + instead \; means args are flatten in one line
find . -type f -name 'duration*' -exec cat {} + > combined_file.txt

# delete files less than 10k
find . -type f -size -10k -exec rm {} \;

# find files with html suffix
find . -maxdepth 1 -name "*.html" -print0

5.2. Awk

awk '!seen[$0]++' text: remove duplicates from text

5.3. Others

shuf input.txt > output.txt : shuffle lines of a given file

rename 's/.html/.json/' *.html: rename all files' suffix (probably require installing)

1. Service Management

The initialization daemon is the first process to be started by the kernel on the Linux server (with ppid 0 and pid 1). The original implementation was BSD init and SysVinit. Currently many linux distributions are adopting systemd (e.g: Ubuntu, RHEL, Fedora distributions)

runlevel is a categorization number that determines what services are started and what services are stopped.

2. Network Management

(sudo) netstat -tupln see all listening tcp/udp pid programs

3. Package Management

3.1. Debian

3.2. Redhat

yum list available | grep postgres: search available packages

4. File System Management

/proc/sys/fs/file-max: file limit

/proc/sys/fs/file-nr: currently opened files

6. Credentials

6.1. Users / Groups

File (/etc/passwd) nonsensitive system password file

  • login name: unique user name encrypted password: DES hash of password, x if shadow password enabled
  • User ID (UID): superuser (root) if value 0
  • Group ID (GID): group id of the first group
  • Comment: text about the user
  • home directory: HOME variable
  • login shell: shell

File (/etc/shadow) sensitive password file. password hash is saved here

File (/etc/group) group info (note that part of the info is saved in /etc/passwd) . current login user's group can be checked with group (1)

  • group name: unique group name
  • encrypted password: group password, x if shadow password enabled
  • group ID (GID): group id
  • user list: users

7. Terminal

stty -a: show terminal line settings

8. Reference

[1] Linux Bible