Command toolbox

Disable RH subscription manager

vi /etc/yum/pluginconf.d/subscription-manager.conf
enabled=0

Export defined amount of lines of log to tar.gz

cat <log_file_name> | wc -l
sed -n '777850,805947p' <log_file_name> | gzip > <output_file_name>

Pack bunch of log files in their own tar.gz

find . -type f \( -name "server.log.2021-02*" ! -name "*tar.gz" \) -exec tar czvf {}.tar.gz {} --remove-files \;

VirtualAnywhere trial with legacy usb printer

If you have old usb printer connected to USB in your linx server you know that cups will not provide high quality printing.
So, my HP DeskJet 920c neded encapsulation of usb. Unfortunately freely available usbip is not a solution since on client side I’m using Windows and usbip drivers for windows are not signed. Hard way you can use it but it involves mappiing device and starting windows in disabled drivers signing check.
It is a pain.
Looking for solutions for it there are only comerciallly available solutions.
VirtuAnywhere trial allows you use only one device on server side in trial mode with no time restrictions.
Taking under consideration my unfrequent usage of printer - it is just fine.
Bellow instalation covering linux server and windows client.

  1. Get server for your operating system from https://virtualhere.com/usb_server_software https://www.virtualhere.com/sites/default/files/usbserver/vhusbdx86_64

  2. Publish package as you want on your server

    1. Run first only to generate config.ini file

  3. Move config.ini file to you rfavorite destination, mine is /etc/usbanywhere/config.ini

    1. Because trial gives you oportunity to work only with one USB device chose one:

  lsusb
  Bus 002 Device 002: ID 0bc2:231a Seagate RSS LLC Expansion Portable
  Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
  Bus 001 Device 003: ID 03f0:1504 HP, Inc DeskJet 920c
  Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Mine ID is 03f0, so update config with:

  AllowedDevices=03f0
  ServerName=my_server_name
  Hostname=my_server_name
  1. Create service [source,sh]

vi /etc/systemd/system/usbanywheresrv.service

[Unit]
Description=VirtualHere Client
After=network.target
[Service]
Type=forking
ExecStart=/usr/sbin/vhusbdx86_64 -b -c /etc/usbanywhere/config.ini
[Install]
WantedBy=multi-user.target

systemctl enable usbanywheresrv.service

systemctl start usbanywheresrv.service

Scale columns for ps dump

ps -eo user:20,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,group:50,args > /tmp/ps_202107010942.txt

Docker volume containers assigment

docker ps -aq | xargs docker inspect -f '{{ .Name }}{{ printf "\n" }}{{ range .Mounts }}{{ printf "\n\t" }}{{ .Type }} {{ if eq .Type "bind" }}{{ .Source }}{{ end }}{{ .Name }} => {{ .Destination }}{{ end }}{{ printf "\n" }}'

Docker overlay to container assigment

docker inspect $(docker ps -qa) |  jq -r 'map([.Name, .GraphDriver.Data.MergedDir]) | .[] | "\(.[0])\t\(.[1])"'

Micro server serving your favorite http status (python 3.9)

#!/bin/python3.9
import http.server

PORT = 8000

class MyHandler(http.server.BaseHTTPRequestHandler):
    def send_response(self, code, message=None):
        """Send the response header and log the response code.

        In contrast to base class, do not send two standard headers
        with the server software version and the current date.
        """
        self.log_request(code)
        if message is None:
            if code in self.responses:
                message = self.responses[code][0]
            else:
                message = ''
        if self.request_version != 'HTTP/0.9':
            self.wfile.write("%s %d %s\r\n" %
                             (self.protocol_version, code, message))

    def do_GET(self):
        self.send_response(500)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("Internal Server Error\n")


httpd = http.server.HTTPServer(("", PORT), MyHandler)
print(("serving at port"), PORT)
httpd.serve_forever()

Keep podman rules after nftables reload/restart

This solution is for situation when nftables are used as main firewall tool, without firewalld.
If you have own rules for nftables in named tables call to systemctl restart nftables or systemctl reload nftables, will kill your podman network.
Override of systemd nftables.nftables is needed as follows:

systemctl edit nftables

[Service]
ExecReload=
ExecReload=/sbin/nft 'flush table inet firewall; include "/etc/sysconfig/nftables.conf";'
ExecStartPost=/usr/bin/podman network reload --all

ExecStartPost simple calls command after restart. ExecReload is modification of original command, instead flush ruleset we are flushing only our table keeping podman rules intact.

Remove CRLF (^M) with vim

Go to binary mode withi your vi/vim

vi -b <your.file.encoded.in.windows>

Replace using
^M sign is created by combination of Ctrl+V+M

%s/^M$//

Find CVE number for package specified

rpm -q --changelog {package-name} | grep CVE-NUMBER

Password generator

tr -dc [[:alnum:]^%#@!=+] </dev/urandom | head -c 12

Get Hostname & IP linux

$(hostname -s)
# OL8
$(ip route get 1 | awk '{print $(NF-2);exit}')
# other
ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p'
# maybe
ip route get 1 | awk '{print $NF;exit}'

Docker - safelly remove dangling overlays

docker volume ls -qf dangling=true | xargs -r docker volume rm

Grep recursively for text including file name and its modification date

grep -rIH <something> * | awk -F: '{"stat -c %y " $1 | getline r; print r": "$0 }'

Get directory sizes in tree without tree to easy browse in spreadsheet

du -mx | tac | sed 's_[^/[:cntrl:]]*/_--_g;s/-/|/' > /tmp/pseudotree.txt

Podman complaining about overlays

Error: kernel does not support overlay fs: 'overlay' is not supported over extfs at "/var/home/rugk/.local/share/containers/storage/overlay": backing file system is unsupported for this graph driver

/etc/containers/storage.conf

[storage]
driver = "overlay"
[storage.options]
mount_program = "/usr/bin/fuse-overlayfs"

Reset USB devices

for i in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*; do
  [ -e "$i" ] || continue
  echo "${i##*/}" > "${i%/*}/unbind"
  echo "${i##*/}" > "${i%/*}/bind"
done

Resize oracle linux volume

This porcedure make your oracle linux 9 partition bigger - simply remove crash report parttion that eats up 15 GB.
Of course - do your own backup :-)

pvresize /dev/sda3
lvextend -l +100%FREE -r /dev/mapper/ocivolume-root
or
lvextend -l +100%FREE /dev/mapper/ocivolume-root
xfs_growfs /
# disable now performance metrics collector
systemctl disable --now pmcd pmlogger pmie
# Size before
df -h

# Backup and unmount
tar -czpf oled.tar.gz /var/oled
umount /var/oled

# Remove
lvremove /dev/mapper/ocivolume-oled

lvcreate -L 1G -n oled ocivolume
mkfs.xfs  /dev/mapper/ocivolume-oled

# Remount and restore data
mount /dev/mapper/ocivolume-oled  /var/oled/
tar -xzf oled.tar.gz -C /

# Size after
df -h

# extend root
lvextend -l +100%FREE -r /dev/mapper/ocivolume-root

# sometimes -r does not work , remove it and then resize fs manually with:
xfs_growfs ...

Find where your alias come from

PS4='+$BASH_SOURCE> ' BASH_XTRACEFD=7 bash -xl 7> /tmp/mylog
grep "alias " /tmp/mylog | grep -e /home -e /etc

Bash functions

There are some additional functions to help in bash.

SAR - magic sed replacer

First one is magic tool that will help to replace block of text from one to another. Remember that it will ignore preceding spaces and tabs in search but block replacemen will be exact.

sar() {
# sar will replace any text block to any other
# text block
# for separation of from - to , use ----
# search will ignore line tabs
# sar <from> <to>
sed -i -f <(sed -nf <(echo '
:a;/^----$/!{;s,[/\.*[],\\&,g;s/$/\\/;s/\s*/\\s*/;
H;x;1s,^.,s/,;1! s/^/N\;/;x;n;ba;};x;s,\\$,/,;x;
:b;n;s,[/\],\\&,g;$!s/$/\\/;H;$!bb;g;s,/\n,/\\\n,;
s,$,/\;P\;D,p') <(cat $1) ) $2
}

This is example usage:

sar <(echo'
</Context>
----
            <CookieProcessor sameSiteCookies="none" />
        </Context>

') /opt/server/conf/context.xml

Above will replace any line contaning </Context> to block

           <CookieProcessor sameSiteCookies="none" />
        </Context>

In file /opt/server/conf/context.xml

rsync - nicer look

rsyncc() {
# this is rsync function that will print tree of folders an readable
# file status
# rsyncc <zrodlo> <cel>
rsync -avvr --human-readable --out-format="%t %i %-8''b %f  > $_%n" $1 $2
}

LookBusy

Compile

curl -L http://www.devin.com/lookbusy/download/lookbusy-1.4.tar.gz -o lookbusy-1.4.tar.gz
tar -xzvf lookbusy-1.4.tar.gz
cd lookbusy-1.4/
./configure
make
make install

Add service

systemctl edit --full --force lookbusy.service

Adapt service settings

[Unit]
Description=lookbusy service

[Service]
Type=simple
ExecStart=/usr/local/bin/lookbusy -c 5 -m 2048MB
Restart=always
RestartSec=10
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

Start service

systemctl enable --now lookbusy.service

Change user ID

Original user GID:2000, ID: 1005, username foo.

usermod -u 2005 foo
groupmod -g 3000 foo

find / -group 2000 -exec chgrp -h foo {} \;
find / -user 1005 -exec chown -h foo {} \;

Check:

ls -l /home/foo/
id -u foo
id -g foo
# search for 'foo' in the passswd file #
grep foo /etc/passwd # search for 'foo' in the group file #
grep foo /etc/group # use the find command to locate files owned by ' foo'#
find / -user foo -ls
find / -group sales -ls

EPEL enable ol9

sudo su -c "dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm"
sudo /usr/bin/crb enable