Linux tips
linux ubuntuUser management
Add user to group
usermod -a -G groupName userName
Disks
Read some info about file systems and disks
df -h
sudo fdisk -l /dev/sda
sudo lvdisplay
sudo pvdisplay
Rescan disk (if needed)
sudo bash -c "echo 1 > /sys/class/block/sda/device/rescan"
sudo parted
print all
fix
Resize disk
Check where your LVM partition is on the disk. This will prompt you to fix the disk, as certain things that should be on the last sectors of the disk no longer are (as the underlying disk has been extended). Letting it do the fixes is OK.
sudo parted -l
If for example the LVM partition is on /dev/sda3, run this command to extend it to use 100% of the free space on the disk
sudo parted /dev/sda resizepart 3 100%
Check the name of the physical volume for lvm
sudo pvdisplay
Then we need to resize the physical volume for LVM
sudo pvresize /dev/sda3
Then we extend the logical volume (here we found the VG by typing df -h)
sudo lvresize -r -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
# sudo lvresize -r -L +1G /dev/mapper/ubuntu--vg-ubuntu--lv
The -r
switch from the lvresize command will automatically resize the file system. If you omit this you can manually resize the filesystem with this:
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
Kernels filling up /boot
https://askubuntu.com/questions/620266/how-does-apt-decide-how-many-old-kernels-to-keep
There is a file that is auto-generated that tells apt-get what kernels to autoremove and which ones to keep. The file that tells apt-get which kernels they are is /etc/apt/apt.conf.d/01autoremove-kernels which is generated from /etc/kernel/postinst.d/apt-auto-removal.
Usually what happens is that when you are receiving kernel updates, when the kernel version changes, say from 3.13 to 3.16, /etc/apt/apt.conf.d/01autoremove-kernels is then updated to keep the 3.16* kernels and is then set to remove all of the 3.13 kernels unless specified by the generating script to not be removed.
Forcibly cleaning up /boot
List kernels installed
sudo dpkg --list 'linux-image*' | awk '{ if ($1=="ii") print $2}' | grep -v `uname -r`
Delete any kernels with a version between 104 and 150, modify this to your needs. It could be wise to keep at least the 1 or 2 newest kernels.
sudo rm -rf /boot/*-4.4.0-{104..150}-*
Remove all but the current kernel. This assumes the kernel type is generic
(all files have generic
as suffix)
cd /boot
ls -1 | grep "\-generic" | grep -v $(uname -r) | xargs rm
Add new /boot partition
-
https://askubuntu.com/questions/1396609/extend-resize-boot-partition
-
Add new disk in Hyper-V / VMWare
-
Find the newly added disk with
sudo fdisk -l
fdisk /dev/sdb
m # Print help
p # Print partition table
g # Create new GPT partition table (this will remove everything on the disk)
n # New partition table
p # Primary partition
+512M # Start at default, then extend 512MB
t # Change type (look at fdisk -l /dev/sda for the types on the current /boot and /boot/efi)
Format the two new partitions, mount and copy data from the old /boot partitions
# Check the file systems on the current partitions
df -Th
sudo mkfs.fat /dev/sdb1
sudo mkfs.ext2 /dev/sdb2 -L boot
sudo mkdir /mnt/boot
sudo mount /dev/sdb2 /mnt/boot
sudo cp -av /boot/* /mnt/boot/
sudo mount /dev/sdb1 /mnt/boot/efi
sudo cp -av /boot/efi/* /mnt/boot/efi/
Mount the new /boot partitions on the correct place and run update-grub
sudo umount /mnt/boot/efi
sudo umount /mnt/boot
sudo umount /boot/efi
sudo umount /boot
sudo mount /dev/sdb2 /boot
sudo mount /dev/sdb1 /boot/efi
sudo update-grub
sudo grub-install --recheck /dev/sdb
# blkid /dev/sdb1
Change /etc/fstab
uuids to match new partitions
sudo blkid /dev/sdb1
sudo blkid /dev/sdb2
Networking
Test network speed
On the listening computer, listen to port 2222 and pipe everything to null
nc -v -l 2222 > /dev/null
On the connecting computer, read 512 blocks of 1024K from /dev/zero and send it with netcat to the listening computer
dd if=/dev/zero bs=1024K count=512 | nc -v $HOSTNAME 2222
Misc
Restart networking
netplan apply
Check listening ports
sudo lsof -i @127.0.0.53:53
Iptables
Sources:
Drop incoming requests from ip
iptables -I INPUT -s 1.2.3.4 -j DROP
Drop only from specified interface
iptables -I INPUT -i eth0 -s 1.2.3.4 -j DROP
-s
can specify both an ip address or a subnet.-I
inserts a rule.-A
appends a rule.
Show rules (-v
for verbose, -n
for supressing reverse DNS lookups)
sudo iptables -L -v -n
Tcpdump
Basic usage:
# tcpdump -i <interface> <filter>
sudo tcpdump -i eth0 port 443
Some useful switches:
-n
Do not resolve host names-X
Print data in hex and ascii
Updates
This will set set –force-confold (force old configuration) and –force-confdef (force default configuration), hopefully preventing prompts
Unattended upgrade
apt-get -q -y -o DPkg::Options::=--force-confold -o DPkg::Options::=--force-confdef upgrade
Crontab update
0 1 * * * bash -c 'for i in update {,dist-}upgrade auto{remove,clean}; do apt $i -y; done'
0 2 * * * bash -c 'if [ -f /var/run/reboot-required ]; then /sbin/reboot; fi'
SSL
Converting from pfx to pem
openssl pkcs12 -in cert.pfx -nodes -nokeys -chain -out domain.cer
openssl pkcs12 -in cert.pfx -nodes -nocerts -out domain.key
Hot add CPU
This script was created for adding CPUs in VMWare. However it should work in all cases (Tested on Hyper-V)
#!/bin/bash
# William Lam
# http://engineering.ucsb.edu/~duonglt/vmware/
# hot-add cpu to LINUX system using vSphere ESX(i) 4.0
# 08/09/2009
for CPU in $(ls /sys/devices/system/cpu/ | grep cpu | grep -v idle)
do
CPU_DIR="/sys/devices/system/cpu/${CPU}"
echo "Found cpu: \"${CPU_DIR}\" ..."
CPU_STATE_FILE="${CPU_DIR}/online"
if [ -f "${CPU_STATE_FILE}" ]; then
STATE=$(cat "${CPU_STATE_FILE}" | grep 1)
if [ "${STATE}" == "1" ]; then
echo -e "\t${CPU} already online"
else
echo -e "\t${CPU} is new cpu, onlining cpu ..."
echo 1 > "${CPU_STATE_FILE}"
fi
else
echo -e "\t${CPU} already configured prior to hot-add"
fi
done