MalcolmChalmers.com

Linux Tips and Tricks


Title: Bash Tips and Tricks

# bash script to wait for a log file to appear then start tailing it

while [ ! -f "/var/log/file.log" ]; do
  sleep 1
done
tail -f /var/log/file.log

# Using a for loop to repeat a command
for i in{1..5}; do COMMAND; done

# Useful aliases to add to .bash_profile - echo "alias bis='sudo -i -u bis'" >> ~/.bash_profile
alias dush='du -sh *'
alias bis='sudo -i -u bis'

# Change back to previous directory
cd -

# Search history
CRTL+R

# Profile files
When bash is invoked as an interactive login shell it first reads and executes the following files if they exist, in order:
    /etc/profile 
    ~/.bash_profile 
    ~/.bash_login 
    ~/.profile
         
When a login shell exits, bash reads and executes commands in the follow files, thif they exist:
    ~/.bash_logout
    /etc/bash.bash_logout



Title: DNF Tips and Tricks

# List all available versions of a package (i.e. jfrog)
dnf list all jfrog-artifactory-pro --showduplicates
jfrog-artifactory-pro.x86_64                                                                      7.90.7-79007900      
jfrog-artifactory-pro.x86_64                                                                      7.90.8-79008900                                                                       
jfrog-artifactory-pro.x86_64                                                                      7.90.9-79009900                                                                       
jfrog-artifactory-pro.x86_64                                                                      7.90.10-79010900                                                                      


# install specific version of a package
dnf install jfrog-artifactory-pro-7.90.7

# list package dependencies 
dnf deplist <package-name>

Title: Disk Tips and Tricks

# Resize a logical volume (adds 2gb and resizes filesystem to suit)
lvextend --resizefs --size +2G /dev/mapper/rootvg-configlv
lvextend --resizefs --size +2G /dev/mapper/rootvg-rootlv

# Adding a new disk
pvcreate /dev/sdb
vgcreate repovg /dev/sdb
lvcreate -n repolv -l 100%FREE repovg


Title: Extract a RPM file

# Extract rpm file
rpm2cpio <filename> | cpio -idmv


Title: Filtering in TOP

# Filtering in top command
press O
then
  type COMMAND=splunk
press ENTER

Title: Find Tips and Tricks

# Find files older than a specified time (mtime) and REMOVE them 
# suggest running command without -exec rm {} \; option first to check what files are found.

find . -type f -mtime +30 -name '*.aud' -exec rm {} \;
find . -type f -mtime +30 -exec rm {} \;
find . -type f -mtime +7 -exec rm {} \;

# Find files older than 7 days, type is file, mtime 7 days, name condition, rm to REMOVE
find /tmp -type f -mtime +7 -name "1*" -exec rm {} \;
find . -type f -mtime +7 -exec rm {} \;

# Find and gzip old log files
find . -type f -name "wms-cli-2024*.log"  -exec gzip {} \;

# REMOVE old log files
find /opt/bis/log -type f -mtime +7 -name "*2023*" -exec rm {} \;

# find gzipped logs older than three days and DELETE them
find /opt/bis/log -type f -mtime +3 -name "*.gz" -exec rm {} \;

# Clean up / santanise log files
find ./logs -type f -name "*" -print -exec sed -i 's/PBI..-...-PN./SERVER/g' {} \;  -exec sed -i 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/xxx.xxx.xxx.xxx/g' {} \; -exec sed -i 's/local.domain/domain.name.au/g' {} \; -exec sed -i 's/adm......\..../username/g' {} \;

# To change/fix permissions to for folder and files, so owner has full access and group has read access
sudo find . -type d -exec chmod 755 {} \;
sudo find . -type f -exec chmod 644 {} \;



Title: Fix remote logon issues

### Fix slow login (when using local credentials )

- vi /etc/ssh/sshd_config
        UseDNS no
        GSSAPIAuthentication no

- service sshd restart



### Fix mRemote / puTTY to close window when disconnected.

You need to change the putty options under tools->options->advanced.
Relevant options are setting Session->"Close window on exit" to Always,
enabling Connection->"Enable TCP keepalives" and setting
Connection->"Seconds between keepalives" to more than 0.

Remember to save the PuttyNG settings as "Default Settings" in Session->"Saved Sessions".


Title: GIT Tips and Tricks

# Discard local changes to tracked files (not yet staged or committed):
git restore .
# Or if you're using an older version of Git:
git checkout -- .

# Git branching commands
git branch // list branches
git branch -r // list remove branches
git branch -a // list all branches

# create a new branch
git branch branch-name

# create a new branch and switch to it
git checkout -b branch-name
# or
git switch -c branch-name

# switch to a different branch
git checkout branch-name
# or
git switch branch-name

# compare branches
git diff branch1..branch2

# merge 'branch-name' into the current branch
git merge branch-name

Title: GitHub and SSH Keys

# Using GitHub with SSH Keys
Generate key
$ ssh-keygen -t ed25519 -C “your_email@example.com”
View you new SSH Key
$ cat ~/.ssh/id_ed25519.pub
Open github, go to setting, SSH and GPG Keys, click add a new key, page contents of id_ed25519.pub into box.


Title: Grep Commands

# Searching for a string in multiple log files in specific folders
grep -r "string to search for " FOLDER1/*.log FOLDER2/*.log FOLDER3/*.log FOLDER4/*.log

# Grep a file and remove comments and blank lines
cat dovecot.conf | grep -v ^# | grep -v ^$

Title: Journalctl Commands

# search for a pattern
journalctl --grep=PATTERN


# Show all messages from this boot:
journalctl -b


# Show all messages from date (and optional time):
journalctl --since="2012-10-30 18:17:16"

# Show all messages since 20 minutes ago:
journalctl --since "20 min ago"


# Follow new messages:
journalctl -f

# Show all messages by a specific executable:
journalctl /usr/lib/systemd/systemd

# Other examples
journalctl --since "2015-01-10 17:15:00"
journalctl --since yesterday
journalctl --since 09:00 --until "1 hour ago"


# filters
# by unit
journalctl -u nginx.service
journalctl -u nginx.service --since today
# by PID
journalctl _PID=8088
# where 33 is the UID of the user/process owner
journalctl _UID=33 --since today
# by error level
journalctl -p err -b
# by identifier ???
journalctl -t setroubleshoot --since=today

Title: Networking Tips and Tricks

# Adding a rich firewall rule(s) - if firewall is not running, use firewall-offline-cmd
firewall-cmd --add-rich-rule='rule family="ipv4" source address="XXX.XXX.XXX.XXX" accept' --perm

# Add/allow a port/service to the firewall
firewall-cmd --add-port 25/tcp --perm
firewall-cmd --add-service ssh --perm

# After adding a rich rule, port or service the firewall needs to be reloaded for the changes to take effect
firewall-cmd --reload

# To see if Firewall Loggin (of denied messages) is enabled
sudo firewall-cmd --get-log-denied

# too add a source IP address to the rule add this option when creating the rule
--add-source=<ip-address>

# to list all zones use
firewall-cmd --list-all-zones

# to list all rules for a specific zone use
firewall-cmd --zone=to-database --list-all

# Set IP address using NMCLI
nmcli con mod ens3f1 ipv4.method manual ipv4.addres XXX.XXX.XXX.XXX/24
nmcli con mod ens3f1 ipv4.gateway XXX.XXX.XXX.1
nmcli con mod ens3f1 dns-search domain.name
nmcli con mod ens3f1 dns "XXX.XXX.XXX.XXX,XXX.XXX.XXX.XXX"
nmcli con mod ens3f1 connection.autoconnect yes
nmcli con up ens3f1

# To use a text based graphical interface
nmtui

# Add a route 
sudo ip route add 172.168.1.0/24 via 172.168.1.1 dev ens01

# remove a route 
sudo ip route del 172.168.1.0/24 via 172.168.1.1 dev ens01

# show link stats (e.g. amount of traffic in and out)
ip -s link show ens01
# or
watch "ip -s link show ens01"



Title: Other Topics

# Enable RDP on Linux
yum install xrdp (needs epel repo)
systemctl enable xrdp --now
systemctl status xrdp
firewall-cmd --permanent --add-port=3389/tcp
firewall-cmd --reload
sudo chcon --type=bin_t /usr/sbin/xrdp
sudo chcon --type=bin_t /usr/sbin/xrdp-sesman
sudo setsebool -P xrdp_can_connect_all_ports on

# Enable epel in EL8 ?
subscription-manager repos --enable codeready-builder-for-rhel-8-$(arch)-rpms
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Title: PostgreSQL Tips

# install postgres
dnf module enable postgresql:15
dnf install postgresql-server

# init database (in /data instead of default location)
initdb -D /data

# enable it to auto start
systemctl enable postgresql
systemctl start postgresql
firewall-cmd –permanent –add-service=postgresql
firewall-cmd –reload

# set SELinux permissions on folder
semanage fcontext -a -t postgresql_db_t “/data(/.*)?”
restorecon -Rv /data

# backup a database
pg_dump dbname > dumpfile

# restore a database
psql -X dbname < dumpfile

Title: Redirecting output

# Redirecting output
command 2>&1 /dev/null
or
command 1> /dev/null 2> /dev/null
or
command > /dev/null 2>&1

Title: Sed Commands

# Set a logback file to from INFO to DEBUG
sed -i 's/INFO/DEBUG/g' /opt/bis/fes/etc/fes-logback.xml


# Set a logback file to from DEBUG to INFO
sed -i 's/DEBUG/INFO/g' /opt/bis/fes/etc/fes-logback.xml


Title: Tar Tips and Tricks

# 'zip' a bunch of files into the one tar file, and remove the files as they are being archieved.
tar cvf wfmcl-2023-11.tar wfmcl-2023-11-*.log --remove-files


Title: Useful App and Tools

# text graphical system monitoring
btop

# text based file manager
mc or 'midnight commander'

Title: Useful Links

# A good XML validator
https://jsonformatter.org/xml-validator

# File Diff Checker
https://www.diffchecker.com/text-compare/

Title: Useful Tools

# ss - show socket activity
ss -lt // show listening ports
ss -at '( dport = :22 or sport = :22 )'  // show anything using port 22

# Run any one of the following command on Linux to see open ports:
$ sudo lsof -i -P -n | grep LISTEN
$ sudo netstat -tulpn | grep LISTEN
$ sudo ss -tulpn | grep LISTEN
$ sudo lsof -i:22 ## see a specific port such as 22 ##
$ sudo nmap -sTU -O IP-address-Here

# how to use tee
dmesg | tee dmesg.txt     // outputs to screen and dmesg.txt

Title: VI Tips and Tricks

# NOTE
some systems will replace vi with an alias to vim when it is installed
some do not

# to disable the mouse
ESC
set mouse =

# to switch color scheme (this may be overiden by system settings or using vi instead of vim)
colorscheme elflord

# to turn on/off line number
ESC
set number

ESC
set nonumber

# turn turn off highlighting
ESC
:nohl



More tips