Linux commands

Find latest authenticated IP addresses using authorized_keys:

while read l; do if [[ -n $l && ${l###} = $l ]]; then echo $l; grep `ssh-keygen -l -f /dev/stdin <<<$l | awk '{print $2}'` /var/log/auth.log; echo '---'; fi; done < /home/ubuntu/.ssh/authorized_keys

Append line in file if not exist:

LINE='test'
FILE=/path/to/file
grep -q -F -E "(^$LINE)" $FILE || echo "$LINE" >> $FILE

uniq:

uniq            # if lines are repeated - show only one of several these lines
    -c          # in start of all lines show repeats number and space
    -d          # show only repeatable lines
    -f <num>    # ignore first num words (word include only not spaces)
    -s <num>    # ignore first num chars
    -u          # show only uniq lines
    -i          # case-insensitive matching

case (all of syntax from example are true):

case "test1" in
   "apple" | "test" ) echo "Apple pie is quite tasty." ;;
   "kiwi") echo "New Zealand is famous for kiwi."
        ;;
   "test"*) echo "test* match"
        ;;
   *) echo "matches not found"
        ;;
esac

# Output:
# test* match

expr:

expr ( 2 + 3 )      # error
expr 2+3            # 2+3
expr 2 + 3          # 5
expr '2 + 3'        # 2 + 3
expr "2 + 3"        # 2 + 3
expr 6 % 3          # 0
expr 6 / 3          # 2
expr 6 / 3.5        # error
expr 6 \* 3         # 18
expr 3 \> 3         # 0
expr 3 \>= 3        # 1
expr tt = t         # 0
expr length test    # 4
expr index test e   # 2

Get first line:

head -n 1

Sleep for 1 second between each xargs command:

ps aux | awk '{print $1}' | xargs -I % sh -c '{ echo %; sleep 1; }'

Empty pagecache, dentries and inodes:

# run as root

# pagecache
free -h && echo && sync && echo 1 > /proc/sys/vm/drop_caches && free -h

# (not recomended) pagecache, dentries and inodes
free -h && echo && sync && echo 3 > /proc/sys/vm/drop_caches && free -h

Flush swap:

# run as root
swapoff -a && swapon -a

Command ‘ps’ options:

# show resource usage of process
ps -p <pid> -o %cpu,%mem,cmd,args   # % - means without column header

Show count of processes of some user:

ps aux  | awk '{print $1}' | grep <user> | wc -l

# show processes count of each user
ps -eo user=|sort|uniq -c

Show user group id’s (GID):

id <username>

Show free/usage space on partitions:

df -h # readable
   -m
   -k

Show count of chars, lines, bytes, etc.:

wc

Show size:

du <dir/file>
    -h # readable
    -s # size of dir

Show size and sort with hidden files/directories and total size:

du -sch /.[!.]* /* |sort -h

Show last lines of file in realtime:

tail -f <file>

Show listening ports:

netstat -antup

Change group:

chgrp [-R] <GID/gname>

Compress image size:

sudo apt-get install jpegoptim

# recursive compress with 50% quality
find . -type f -name "*.jpg" -exec jpegoptim -m50 {} \;

ARP:

arp     # ARP table contains device addresses and mac's which located in same network
    -n  # only IP (without domains)
    -a  # IP and domains

# show in the file
cat /proc/net/arp

APT

apt-cache policy <package>  # search versions of packages which enabled to install
fdisk -l    # show info about disks and partitions
blkid       # show small list of disks, UUID, TYPE
lsblk       # show disk type, size, name, mountpoint

# show UUID of all disks
ls -al /dev/disk/by-uuid/

# show disk info
sudo hdparm -I /dev/sda
                        | grep -i trim  # TRIM for SSD

# mount all from fstab (run it after fstab was changed)
sudo mount -a

# create partition table
sudo mkfs -t ext4 <device_name>
# create 100 files: file1, file2, ..., file100
touch file{1..100}

NMAP:

nmap <ip>       # scan IP
            -sT # TCP
            -sU # UDP
            -O  # show software version
            -v  # show process of scannig
# show status of all services
service --status-all

Linux users and groups:

# add existing user to existing group
sudo usermod -a -G groupName userName

adduser <name>  # create new user
        -r      # create system user
        -g      # group

useradd
        -u          # UID
        -r          # create system user
        -g <group>  # name or ID of group
        -m          # create home dir
        -d <path>   # path to home dir
        -s <shell>  # register shell for user
        -c          # comments

userdel <name>  # delete user
        -r      # also delete home dir

# create group
groupadd <name>
        -r      # create system group
        -g      # group ID

Shutdown commands:

shutdown [options] now|<time> # power management
        -r  # reboot
        -h  # power off after shutdown
        -c  # cancelling scheduled shutdown

# poweroff now
shutdown -h now
halt -p
# send message to another user
write <user> [tty]

# show entered users
who

last       # login history
    reboot # reboot history
    <user> # user login history

lastlog    # show all user with login time

Show system information:

inxi -F             # show information about system
lshw | more | less  # show hardware information, also sector size
lscpu               # show CPU information

# show hardware information, also type (virtual or bare metal)
sudo dmidecode -t system

CPU benchmark:

apt install sysbench
sysbench --test=cpu --cpu-max-prime=20000 run

Decrease *.vdi size:

# 1. On guest - zero fill free space
sudo dd if=/dev/zero of=zero bs=512k    # bs=<sector size>
sudo rm zero

# 2. On host
vboxmanage modifyhd /path/to/thedisk.vdi --compact
# run command as another user
sudo -u lee <command>

# for adding PATH env
sudo nano /etc/environment

# restart networking
sudo ifconfig wlan0 down && sudo ifconfig wlan0 up

# get and show page
wget -qO- 127.0.0.1

grep:

grep -iR -E '(error_report|ini_set)' ./*.php    # find in files
        -R                                      # recursive (include inherited dirs)
        -l                                      # show only file names
        -i                                      # ignore case sensetive
        --exclude="*\.svn*"                     # exclude
        --exclude-dir="dir" | {dir1,dir2}       # exclude dir
        -A <num>                                # show also <num> lines after
        -B <num>                                # show also <num> lines before

Word replacement:

grep -Rl -E "(SOURCE)" ./* | xargs sed -i "s/SOURCE/DEST/g"

sed -i "s@SOURCE@DEST@g" /path/to/file

Remove the line which contains string:

# print the output to standard out
sed '/pattern to match/d' <file>

# directly modify the file (and create a backup). Will create <file>.bak.
sed -i.bak '/pattern to match/d' <file>

Replace whole line started by:

sed "s/start_of_line.*/new_line_content/g"

Find

find ./ -name 'path*'
    -type (f=file, d=dir, l=link, p=pipe, s=socket)

find ./ -mtime 0 -type f -not -path "./" -exec cp --parents "{}" $TODAY_DIR/ \;

# chmod only files
find . -type f -exec chmod 644 {} \;

# faster chmod only files
find . -type f -print0 | xargs -0 chmod 644

# make iconv replace the input file with the converted output in one line
find . -type f -name '*.php' -exec sh -c 'iconv -f WINDOWS-1251 -t UTF-8 -o {}UTF_TMP {} && mv {}UTF_TMP {}' \;

# clear file
> filename

Delete old files

ls -t | sed -e '1,10d' | xargs -d '\n' rm -f
    ls -t               # lists all files in the current directory in decreasing order of modification time
    sed -e '1,10d'      # deletes the first 10 lines, ie, the 10 newest files
    xargs -d '\n' rm    # collects each input line (without the terminating newline) and passes each line as an argument to rm.
    -f                  # ignore if empty

Create backup tar(tar.gz) archive:

tar -cvpzf backup.tar.gz [dir1] [dir2]
    -c  # create
    -v  # verbose mode
    -p  # preserving files and directory permissions.
    -z  # compress (tar.gz)
    -f <filename>
    --exclude=<file_or_dir>

Extract archive at current dir:

tar -xf <archive.tar.gz>

PASSWORD GENERATE:

apg
    -t  # print pronunciation for generated pronounceable passwor
    -q  # quiet mode (do not print warnings)
    -n  # count of generated passwords
    -m  # minimum pass length
    -x  # maximum pass length
    -a  # choose algorithm:
        1 - random password generation according to password modes
        0 - pronounceable password generation

# Example:
apg -q -m 8 -x 8 -a 1 -n 5

SECURITY

Find empty passwords:

awk -F: '($2 == "") {print}' /etc/shadow

Find users with uid == 0:

awk -F: '($3 == "0") {print}' /etc/passwd

Port listening check:

netstat -tulpn

Add service to autorun

  • Ubuntu 16.04:

    sudo /bin/systemctl daemon-reload
    sudo /bin/systemctl enable <SERVICE_NAME>.service