Common Command Snippets in Linux

systemd

systemctl

# Start a service immediately
systemctl start nginx.service

# Stop a service immediately
systemctl stop nginx.service

# Restart a service
systemctl restart nginx.service

# Kill all child processes of a service
systemctl kill nginx.service

# Reload a service's configuration file
systemctl reload nginx.service

# Reload all modified configuration files
systemctl daemon-reload

# Show all underlying parameters of a specific Unit
systemctl show httpd.service

# Show the value of a specific property of a Unit
systemctl show -p CPUShares httpd.service

# View Unit file
systemctl cat crond.service

# Set a specific property of a Unit
systemctl set-property httpd.service CPUShares=500
# Reboot the system
systemctl reboot

# Power off the system
systemctl poweroff

# Halt the CPU
systemctl halt

# Suspend the system
systemctl suspend

# Hibernate the system
systemctl hibernate

# Enter hybrid sleep mode
systemctl hybrid-sleep

# Start in rescue mode (single-user mode)
systemctl rescue
# List running Units
systemctl list-units

# List all Units, including those without configuration files or that failed to start
systemctl list-units --all

# List all inactive Units
systemctl list-units --all --state=inactive

# List all failed Units
systemctl list-units --failed

# List all running Units of type service
systemctl list-units --type=service

journalctl

# View all logs (by default, only logs from the current boot are saved)
journalctl

# View kernel logs (does not show application logs)
journalctl -k

# View logs from the current boot
journalctl -b
journalctl -b -0

# View logs from the previous boot (requires configuration change)
journalctl -b -1

# View logs from a specific time
journalctl --since="2012-10-30 18:17:16"
journalctl --since "20 min ago"
journalctl --since yesterday
journalctl --since "2015-01-10" --until "2015-01-11 03:00"
journalctl --since 09:00 --until "1 hour ago"

# Show the last 10 lines of logs
journalctl -n

# Show the last specified number of lines of logs
journalctl -n 20

# Real-time display of the latest logs
journalctl -f

# View logs for a specific service
journalctl /usr/lib/systemd/systemd

# View logs for a specific process
journalctl _PID=1

# View logs for a script at a specific path
journalctl /usr/bin/bash

# View logs for a specific user
journalctl _UID=33 --since today

# View logs for a specific Unit
journalctl -u nginx.service
journalctl -u nginx.service --since today

# Real-time display of the latest logs for a specific Unit
journalctl -u nginx.service -f

# Merge and display logs for multiple Units
journalctl -u nginx.service -u php-fpm.service --since today

# View logs of a specific priority (and above), there are 8 levels
# 0: emerg
# 1: alert
# 2: crit
# 3: err
# 4: warning
# 5: notice
# 6: info
# 7: debug
journalctl -p err -b

# Logs are paginated by default, --no-pager changes to normal standard output
journalctl --no-pager

# Output in JSON format (single line)
journalctl -b -u nginx.service -o json

# Output in JSON format (multi-line), better readability
journalctl -b -u nginx.service -o json-pretty

# Show disk usage of logs
journalctl --disk-usage

# Specify the maximum space occupied by log files
journalctl --vacuum-size=1G

# Specify how long to keep log files
journalctl --vacuum-time=1years

ps

ps aux
ps -ef
# Show Threads
ps -eLf | wc -l
# -o: ps L
ps -eo uid,pid,ppid,comm,args

pgrep -a java | grep FineBI

ls /proc | grep -E '^[0-9]+$' | wc -l

awk

# Print JSON
awk '/http/{print}' logs/notify_access_huawei.log | head -n 100 | awk '{if(match($0, /(\\{.+\\})/, a)){print a[1]}}'

zip

# Compress the nginx conf directory
zip -r conf.zip.2023-07-12 conf

tar

# Compress the doris-meta directory
tar -czvf doris-meta.tar.gz doris-meta

# Decompress
tar -xzvf doris-meta.tar.gz

wsl

# Execute in PowerShell
wsl bash -c "echo $(Get-Process | select -Property Cpu, Id | Out-Host) | awk '{print}'"

netstat

function show_title() {
    echo -e "\n\033[41;42;25m $1 \033[0m"
}


function net_log() {
    # Networking
    ##
    show_title "Netstat"
    netstat -ant | awk '{++S[$NF]} END {for(i in S) {t+=S[i];print i, S[i];} print "Total", t}' | sort -nr -k2 | column -t
    show_title "Client 443 and 80"
    netstat -ant | grep -E '(:443|:80)' | wc -l
    show_title "Upstream Socket"
    netstat -ant | grep "192.168\\." | awk '{++S[$5]} END {for(i in S) {print i, S[i]}}' | sort -nr -k2 | column -t
}

net_log

nc

# Check if a port is open
nc -v 192.168.1.100 2222
telnet 192.168.1.100 2222

gpssh-exkeys

https://docs-cn.greenplum.org/v6/utility_guide/admin_utilities/gpssh-exkeys.html

https://github.com/greenplum-db/gpdb/blob/main/gpMgmt/bin/gpssh

https://github.com/greenplum-db/gpdb/blob/main/gpMgmt/bin/gpssh-exkeys

/var/log

/var/log/ Remarks
/var/log/messages Includes overall system information, including logs during system startup. Additionally, mail, cron, daemon, kern, and auth information are also recorded in the var/log/messages log.
/var/log/dmesg Contains kernel buffer information (kernel ring buffer). Many hardware-related messages are displayed on the screen during system startup. You can use dmesg to view them.
/var/log/auth.log Contains system authorization information, including user login and permission mechanisms used.
/var/log/boot.log Contains logs during system startup.
/var/log/daemon.log Contains log information from various system background daemon processes.
/var/log/dpkg.log Includes logs of software packages installed or removed by the dpkg command.
/var/log/kern.log Contains logs generated by the kernel, helpful for troubleshooting when customizing the kernel.
/var/log/lastlog Records the most recent information for all users. This is not an ASCII file, so you need to use the lastlog command to view its contents.
/var/log/maillog /var/log/mail.log Contains log information from the system’s email server. For example, sendmail log information is sent entirely to this file.
/var/log/user.log Records logs of all user information at all levels.
/var/log/Xorg.x.log Log information from X.
/var/log/alternatives.log Update alternative information is recorded in this file.
/var/log/btmp Records all failed login attempts. You can view the btmp file using the last command. For example, “last -f /var/log/btmp”
/var/log/cups Logs related to all printing information.
/var/log/anaconda.log All installation information is stored in this file during Linux installation.
/var/log/yum.log Contains information about software packages installed using yum.
/var/log/cron Whenever the cron process starts a job, relevant information is recorded in this file.
/var/log/secure Contains information related to authentication and authorization. For example, sshd logs all information (including failed logins) here.
/var/log/wtmp or /var/log/utmp Contains login information. Using wtmp can find out who is logging into the system, who is using commands to display this file or information, etc.
/var/log/faillog Contains information about failed user login attempts. Additionally, erroneous login commands are also recorded in this file.

release info

lsb_release -a
cat /etc/*-release
uname -a
cat /proc/version
dmesg
yum
dnf
rpm
apt
apt-get

Scripts

Program Name

PROG_NAME="$(basename $0)"
PROG_DIR="$(cd $(dirname $0) && pwd)"

Comments batch

echo start
:<<!
This is a batch comment
Haha.
!
echo end

Loading

sleep 5 &
pid=$!
frames="/ | \ -"
while kill -0 $pid 2>1 > /dev/null;
do
    for frame in $frames;
do
        printf "\r$frame Loading..."
        sleep 0.5
    done
done
printf "\n"

LogUtil

# https://github.com/Vonng/pigsty/configure

__CN='\033[0m';__CB='\033[0;30m';__CR='\033[0;31m';__CG='\033[0;32m';
__CY='\033[0;33m';__CB='\033[0;34m';__CM='\033[0;35m';__CC='\033[0;36m';__CW='\033[0;37m';
function log_info() {  printf "[${__CG} OK ${__CN}] ${__CG}$*${__CN}\n";   }
function log_warn() {  printf "[${__CY}WARN${__CN}] ${__CY}$*${__CN}\n";   }
function log_error() { printf "[${__CR}FAIL${__CN}] ${__CR}$*${__CN}\n";   }
function log_debug() { printf "[${__CB}HINT${__CN}] ${__CB}$*${__CN}\n"; }
function log_input() { printf "[${__CM} IN ${__CN}] ${__CM}$*\n=> ${__CN}"; }
function log_hint()  { printf "${__CB}$*${__CN}"; }

Check Command Exists

function check_package_manager(){
    # get package / manager: rpm|deb and dnf|yum|apt|apt-get|zypper
    if command -v dpkg >/dev/null 2>&1; then
        OS_PACKAGE="deb"
        if command -v apt >/dev/null 2>&1; then
            OS_MANAGER="apt"
        elif command -v apt-get >/dev/null 2>&1; then
            OS_MANAGER="apt-get"
        else
            log_error "fail to determine os package manager for deb"
            exit 4
        fi
    elif command -v rpm >/dev/null 2>&1; then
        OS_PACKAGE="rpm"
        if command -v dnf >/dev/null 2>&1; then
            OS_MANAGER="dnf"
        elif command -v yum >/dev/null 2>&1; then
            OS_MANAGER="yum"
        elif command -v zypper >/dev/null 2>&1; then
            OS_MANAGER="zypper"
        else
            log_error "fail to determine os package manager for rpm"
            exit 4
        fi
    else
        log_error "fail to determine os package type"
        exit 3
    fi
    log_info "package = ${OS_PACKAGE},${OS_MANAGER}"
}

python requests

# application/x-www-form-urlencoded
requests.post(url='',data={'key1':'value1','key2':'value2'},headers={'Content-Type':'application/x-www-form-urlencoded'})
# multipart/form-data
requests.post(url='',data={'key1':'value1','key2':'value2'},headers={'Content-Type':'multipart/form-data'})
# xml
requests.post(url='',data='<?xml  ?>',headers={'Content-Type':'text/xml'})
# json
requests.post(url='',data=json.dumps({'key1':'value1','key2':'value2'}),headers={'Content-Type':'application/json'})
requests.post(url='',json={{'key1':'value1','key2':'value2'}},headers={'Content-Type':'application/json'})
# binary
requests.post(url='',files={'file':open('test.xls','rb')},headers={'Content-Type':'binary'})
with open(r'C:\Users\idido\Downloads\notify_access_x9cloud.log.bak') as f:
  lines = f.readlines()
  for item in lines:
      ret = requests.post(url="https://api.fooww.com/VirtualPhone/X9cloudNotifyDC", data=item, headers={'Content-Type':'text/xml'})
      print(ret.text)
# pip install requests_toolbelt
from requests_toolbelt import MultipartEncoder
import requests

m = MultipartEncoder(
    fields={'field0': 'value', 'field1': 'value',
            'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
)

r = requests.post('http://httpbin.org/post', data=m,
                  headers={'Content-Type': m.content_type})
# pip install requests_toolbelt
from requests_toolbelt import MultipartEncoder
import requests

m = MultipartEncoder(fields={'field0': 'value', 'field1': 'value'})

r = requests.post('http://httpbin.org/post', data=m,
                  headers={'Content-Type': m.content_type})

docker-clean-log

for container in $(docker ps -aq); do
    log=$(docker inspect --format='{{.LogPath}}' $container)
    echo $log
    cat /dev/null > $log
done

Ubuntu

libssl.so.1.1

  • error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory
# @see https://blog.csdn.net/Asgard_Hu/article/details/127532328
wget http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
sudo dpkg -i libssl1.1_1.1.1-1ubuntu2.1~18.04.23_amd64.deb

Resources

  • Linux – View port usage and find and kill occupying processes[1]
    • lsof
    • netstat
    • fuser -v -n tcp 22
  • shellcheck[2] a static analysis tool for shell scripts.

References

[1]

Linux – View port usage and find and kill occupying processes: https://www.cnblogs.com/shoufeng/p/11308614.html

[2]

shellcheck: https://github.com/koalaman/shellcheck

Leave a Comment