MalcolmChalmers.com

Linux Tips and Tricks


Title: Bash 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


Title: Extend a logical volume

# 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

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 commands

# Find files older than a specified time (mtime) and REMOVE them - suggest running command without -exec rm {} \; opti                                                                        on 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 {} \;


Title: Firewalld 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

Title: Fix File and Folder permssions

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: 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: Redirecting output

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

Title: Santanise Log Files

# 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/pidm..                                                                        ....\..../username/g' {} \;

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: Setup a NIC using NMCLI

# 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

Title: Some useful bash aliases

# 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'

Title: Tar Commands

# '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: Using a for loop in bash

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


Title: gzip logs files and remove them

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