0x324 Linux Admin
- 1. Bash
- 1.1. Arithmetic
- 1.2. Conditions
- 1.3. For
- 1.4. Find
- 1.5. Awk
- 1.6. Others
- 2. Service Management
- 3. Network Management
- 4. Package Management
- 4.1. Debian
- 4.2. Redhat
- 5. File System Management
- 6. Credentials
- 6.1. Users / Groups
- 7. Terminal
- 8. Reference
1. Bash
1.1. Arithmetic
a=5
b=3
# Addition
sum=$((a + b))
echo "Sum: $sum"
1.2. Conditions
if [[ -z "$var" ]]; then
echo "var is empty"
fi
if [[ -n "$var" ]]; then
echo "var is not empty"
fi
1.3. 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
1.4. 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
1.5. Awk
awk '!seen[$0]++' text
: remove duplicates from text
1.6. Others
shuf input.txt > output.txt
: shuffle lines of a given file
rename 's/.html/.json/' *.html
: rename all files' suffix (probably require installing)
2. 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.
3. Network Management
(sudo) netstat -tupln
see all listening tcp/udp pid programs
4. Package Management
4.1. Debian
4.2. Redhat
yum list available | grep postgres
: search available packages
5. 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