Linux
DevOps Linux System 01
- Azure's resource group is equivalent to GCP's project: a container for project-related resources (based on business logic)
- RedHat vs CentOS vs Debian vs Ubuntu
- Ubuntu: comes with a GUI
- Debian: more stable than Ubuntu, smaller size, rock solid as a base OS
- RedHat: the gold enterprise edition, quite expensive
- CentOS: the open-source counterpart of RedHat
- apt acts as an index connecting to different version repositories;
apt updaterefreshes the package index ~/.jenkinscan archive user files separately; JENKINS_HOME defaults to/var/lib/jenkins- keep the same number of CPUs as executors (total number of concurrent jobs)
- SCM: source code management — pulls code from version control
DevOps Linux file struct 02
- /bin, /sbin, /usr/bin, /usr/sbin: The default directories for system executables — for example,
lslives at/bin/ls. - /bin and /usr/bin are for general users (non-root), while
/sbinand/usr/sbinare for root. - /var: Stores logs and other data that programs generate at runtime. Log files go under
/var/log; default mail storage is also here. /varcontains data that changes during normal system operation. Its subdirectories grow and shrink frequently. Content that used to live in/usrwas moved here to keep/usrrelatively stable. This directory is system-specific and not shared over the network./etccontains system configuration files, including many network configuration files.
DevOps Linux sudo vs su 03
- Difference between
suandsu -:suswitches the user identity to root but keeps the current shell environment (the original user's PATH);su -switches both the identity and the shell environment to root's. Onlysu -avoids PATH errors. Aftersu,pwdstill shows the original user's working directory; aftersu -, it switches to root's home directory. - sudo vs su:
sudo= super user do;su= switch usersuswitches to root by defaultsu - -ctemporarily borrows root privileges to run a single command, then returns to the current usersudo su -==sudo -i: switches to root as a login shell.sudo su -requires the current user's password;su -requires the root password.- Whether a user can use
sudodepends on whether root has granted authorization in/etc/sudoers. Only root can edit that file withvisudo.
[root@localhost ~]means:- Username: root
- Hostname: localhost
- Current path:
~(the current user's home directory) - Privilege indicator:
#for root,$for regular users
- Difference between
~and/in[root@localhost ~]:~is root's home directory (equivalent toC:\Users\Administratoron Windows);/is the Linux root partition (equivalent toC:\).- Under
/there are/root(root's home) and/home(home directories for other users, typically/home/username)
DevOps Linux Cron Job
-
set -xenables verbose logging (prints each command before executing);set +xdisables it. It's an alternative to scatteringechostatements throughout shell scripts. -
echo $?: checks whether the last command succeeded. Returns 0 on success, non-zero on failure. -
Searching files and content
grep "search_string" filename— find lines in a file matching a stringgrep -e "regex" filename— find lines matching a regular expressiongrep -i "search_string" filename— case-insensitive searchfind / -name "*.log" | xargs grep "ERROR"— find all.logfiles from the root directory and search them for "ERROR"
-
Pipes
|: combine multiple commands when one cannot complete the task alone| xargs: use when the piped output is a list of filenames
-
Scheduled tasks (cron)
crontab -e— edit scheduled taskscrontab -l— list scheduled tasks
-
Copy a file from local machine to a remote server (securely)
-
scp -i ~/.ssh/key.pem <local_file> ec2XX@<public_IP>:<remote_path>e.g.
scp -i ~/.ssh/my-key.pem newfile ec2@53.33.33.33:/home/ec2-user/newfileTo copy from remote to local, reverse the arguments. FileZilla can also be used for transfers.
-
-
Set file owner:
sudo chown <username>:<group> <fileName> -
Watch a live log file:
tail -f filename(-ffollows the file as it grows) -
PID: process identification number — a unique ID automatically assigned to each process when it is created by the OS
-
An executable file launched in the OS creates a running copy of the application called a process
-
lessis a handy command for reading file contents page by page
#AustraliaIT #DevOps #JobHunting #Programming
DevOps Regex Cheatsheet
Regular expressions are one of those things you learn and immediately forget — here's a quick reference:
^: matches the start of a string$: matches the end of a string*: 0 or more occurrences+: 1 or more occurrences?: 0 or 1 occurrencex{n}: exactly n occurrences of xx{n,}: n or more occurrences of xx{n,m}: between n and m occurrences of x(): treat as a single group[]: character set — matches any one character inside\d: a single digit\w: a single word character (letter, number, or underscore)\s: a single whitespace character (space, tab, or line break)\1: back-reference to the content captured by the first group, e.g./(x)(y)\1/matches(x)(y)(x)
\D, \W, \S are the negations of the above (non-digit, non-word, non-whitespace)
- Flags (placed after the closing
/of a pattern)g: global search — returns all matches, not just the firstm: multiline matchingi: case-insensitive matching
DevOps Mac && Linux Environment Variables
-
shgenerally refers to the Linux shell, but on Macshbehaves differently (generally considered more compatible) -
bashis an extension ofsh; on Mac bothshandbashrefer to bash -
Environment configuration load order:
/etc/profile— loaded first, applies to all users, lowest priority (not recommended to modify)/etc/paths— global environment variable configuration file~/.bash_profile→~/.bash_login→~/.profile→~/.bashrc— loaded after system files; ifbash_profileis found, the others are skipped. Order may differ on Linux.~/.zshrc— used when oh-my-zsh is installed; the system reads~/.zshrcon zsh startup and skips~/.bash_profile. Addsource ~/.bash_profileat the end of.zshrcto also load bash configuration.
-
source ~/.bash_profile— reload the configuration file to apply environment variable changes immediately -
export ZSH=$HOME/.oh-my-zsh— export an environment variable; view it withecho $ZSH -
PATH=$PATH:$HOME/bin— append to the existing PATH using:as a separator; the shell searches each directory in order when looking for executables -
Environment variables set directly in the terminal (without adding to a configuration file) only apply to the current session and are lost on restart
-
alias ll='ls -lt'— define a command shortcut; runaliasalone to list all defined aliases -
env— display all environment variables -
echo $PATH— display the value of a specific environment variable -
Linux command chaining operators
&— run a task in the background&&— run the next command only if the previous one succeeded|— pipe the output of one command as the input to the next||— run the next command only if the previous one failed
-
Linux IO streams:
stdin,stdout,stderr— equivalent to Python'sinput(),print(), and exceptions -
Trim whitespace from both ends:
sed -e 's/[[:space:]]*$//' | sed -e 's/^[[:space:]]*//'