John Siu Blog

Tech - Business Tool, Personal Toys

Linux Commands

☰ Table of Content

Misc. Linux commands.


Find

Find x-days before and action

In actuality, it should be (x-1).

1
/bin/find <path> -maxdepth 1 -mtime +<x> -type f -name "<pattern>" -exec rm -f {} \;

Find x-minutes before and action

1
/bin/find <path> -maxdepth 1 -mmin +<x> -type f -name "<pattern>" -exec rm -f {} \;

Password

Change password last modify

1
chage -d YYYY-MM-DD <user>

Change password never expire

1
chage -I -1 -m 0 -M 99999 -E -1 <user>

Lock/Unlock Account

1
2
3
4
5
6
7
passwd -l <user>  # Lock
passwd -u <user>  # Unlock

usermod -L <user> # Lock
usermod -U <user> # Unlock

pam_tally2 -r -u <user> # Reset fail login count

Ban IP

IPTables

1
2
iptable -A INPUT -s <IP> -j DROP
ip6table -A INPUT -s <IP> -j DROP

Fail2ban

1
2
fail2ban-client status # show jail list
fail2ban-client -vvv set <jail from list> banip <ip>

Rotate frame buffer

Number can be 1, 2, 3

1
2
echo 1 > /sys/class/graphics/fbcon/rotate_all
echo 1 > /sys/class/graphics/fbcon/rotate

File System

Resize Filesystem

Usually use after a partition / image resize

1
2
resize2fs <device>
resize2fs /dev/sda1

Disable journal on ext4

1
tune2fs -O ^has_journal /dev/<disk>

Create Sparse File

truncate -s <size> <filename>

1
2
3
4
5
6
7
$ truncate -s 10G 10G.txt

$ ls -lah 10G.txt
-rw-r--r--    1 user user   10.0G Apr  1 00:00 10G.txt

$ du -sh 10G.txt
0       10G.txt

Ref: Sparse file wikipedia

Rsync

1
rsync -vahpt --size-only --stats --del <source> <target>

<source> is put/sync INTO <target> directory, not replacing <target>.

Curl

Skip certificate checking

1
curl -k ... # Use -k to skip certificate check.

DNS Over HTTPS

1
2
curl -sH 'accept: application/dns-json' 'https://dns.google/resolve?name=google.com' | jq .
curl -sH 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=google.com' | jq .

Avahi/MDNS

Show All Entries

1
2
avahi-browse -a
avahi-browse -a -d <domain> # specify domain other than .local

IPv4 Lookup

1
avahi-resolve -n4 door.local

IPv6 Lookup

1
avahi-resolve -n6 door.local

Check NIC status

1
ethtool <nic>

Check WiFi SSID

1
2
iw dev <wifi nic> scan
iw dev wlan0 scan

Enable BBR

Create file /etc/sysctl.d/10-network-bbr.conf with following content and reboot:

1
2
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr

Detect Text File Charset/Encoding

1
uchardet <file.txt>

Convert Text File Charset/Encoding

1
2
iconv iconv -f <file charset> -t <output charset> <file.txt>
iconv iconv -f jis -t utf8 readme.txt

SSH

Remove Known Hosts

1
2
ssh-keygen -R <hostname/ip>
ssh-keygen -f "~/.ssh/known_hosts" -R <hostname/ip>

ProxyCommand

host test
ProxyCommand ssh -W %h:%p jumpserver

ProxyJump

host test
ProxyJump jumpserver

Journalctl

List Field

1
journalctl -N

List Identifier

Identifier value can be used in -t.

1
journalctl -F SYSLOG_IDENTIFIE

Check File

Content Type

1
file <filename>

Image Info

1
magick identify <filename>

John Siu

Update: 2022-07-27
comments powered by Disqus