Login

Services/daemons

List services started automatically (Redhat):

/sbin/chkconfig --list | grep 3:on

Enable service to start automatically (Redhat):

sudo /sbin/chkconfig --level 2345 "service" on

Enable service to start automatically (Debian):

sudo /sbin/update-rc.d "service" defaults
sudo /sbin/systemctl enable "service"

Disable service from starting automatically (Redhat):

sudo /sbin/chkconfig --level 2345 "service" off

Disable service from starting automatically (Debian):

sudo /sbin/update-rc.d "service" remove
sudo /sbin/systemctl disable "service"

Package Management

List packages installed:

yum list | grep installed
apt list installed

User Management

Add a user:

sudo /usr/sbin/useradd -G "group" -m -s /bin/bash "username"

Add a system user:

sudo /usr/sbin/useradd -d /var/"homedir" -r -s /sbin/nologin "username"

Change user's home directory:

sudo /usr/sbin/usermod -d /home/dir username

Create a password hash for ldap:

/usr/sbin/slappasswd -u -s "PASSWORD" -h "{ssha}"
/usr/sbin/slappasswd -u -s "PASSWORD" -h "{crypt}" -c '$1$%.8s'

Create a random string of 12 printable characters:

echo `< /dev/urandom tr -dc [:graph:] | head -c12`
echo `< /dev/urandom tr -dc [:alnum:] | head -c12`

Add ldif file to ldaps:

sudo /usr/sbin/slapadd -l ./ldif/FILE

Create base64 encoded hash:

perl -MMIME::Base64 -e 'print encode_base64("username\@example.orgpassword'

Miscellaneous

Update Spamassassin Rules:

sudo /usr/bin/sa-update

View files installed by RPM package:

rpm --query --filesbypkg "packagename"

View kernel version:

uname -r

LVM Management

Display Tags assigned to LVM Logical Volumes

sudo /sbin/lvs --options 'vg_name,lv_name,lv_size,tags'

Add Tag to LVM Logical Volume

sudo /sbin/lvchange --addtag "tag" /dev/"VolumeGroup"/"LogicalVolume"

Delete Tag from LVM Logical Volume

sudo /sbin/lvchange --deltag "tag" /dev/"VolumeGroup"/"LogicalVolume"

Rename LVM Logical Volume

sudo /sbin/lvrename /dev/"OldVolumeGroup"/"OldLogicalVolume" /dev/"NewVolumeGroup"/"NewLogicalVolume"

Add new disk to LVM Volume Group

fdisk /dev/"disk"
Create new primary partition with disk label 8e
pvcreate /dev/"partition"
vgextend "VolumeGroup" /dev/"partition"

Expand LVM Logical Volume

lvextend -L +SizeGB|SizeGB /dev/"VolumeGroup"/"LogicalVolume"
lvextend -l +SizePE|SizePE /dev/"VolumeGroup"/"LogicalVolume"
resize2fs /dev/"VolumeGroup"/"LogicalVolume"

Shrink LVM Logical Volume

e2fsck -f /dev/"VolumeGroup"/"LogicalVolume"
resize2fs /dev/"VolumeGroup"/"LogicalVolume" SizeLessThanFinal
lvreduce -L -SizeGB|SizeGB /dev/"VolumeGroup"/"LogicalVolume"
lvreduce -l -SizePE|SizePE /dev/"VolumeGroup"/"LogicalVolume"
resize2fs /dev/"VolumeGroup"/"LogicalVolume"

Activate LVM Volume Group (makes LV available)

vgchange -ay "VolumeGroup"

Convert LVM Logical Volume to mirrored

sudo /sbin/lvchange -m1 --type mirror [--mirrorlog mirrored] /dev/"VolumeGroup"/"LogicalVolume" [/dev/"PhysicalVolume1" /dev/"PhysicalVolume2" /dev/"PhysicalVolume3":0]

Convert LVM Logical Volume to linear

sudo /sbin/lvchange -m0 /dev/"VolumeGroup"/"LogicalVolume" [/dev/"PhysicalVolume"]

File Management

Copy an entire directory with subdirectories:

wget -m ftp://my.site.com/path/to/dir/*

Change SELinux context of a file or directory:

sudo /usr/bin/chcon -v type="SELINUX_CONTEXT" /path/to/file

Change SELinux context of a directory recursively:

sudo /usr/bin/chcon -Rv type="SELINUX_CONTEXT" /path/to/dir

See the size of a directory, including sub-directories:

du -sh /path/to/dir

See the size of a directory, excluding sub-directories:

du -Sh /path/to/dir

Get directories recursively via sftp

sftp "server"
cd "directory"
get -r *
or
get -r "directory"

Delete Files Modified More Than X Days Ago:

find "path" -mtime +X -execdir rm{} \;
find "path" -mtime +X -delete

Allow Apache to access Network Services with SELinux Enforcing

sudo /usr/sbin/setsebool -P httpd_can_network_connect 1
sudo /usr/sbin/setsebool -P httpd_can_network_connect_db 1

Xen Management

Clone Xen domU:

sudo /usr/bin/virt-clone --original "domain name" --name "new domain name" --file /path/to/new/disk/image

Undefine Xen domU:

sudo /usr/bin/virsh undefine "domain name"

Mount a Xen disk image (in order):

sudo /sbin/kpartx -av "path to file"
sudo /sbin/vgscan
sudo /sbin/vgchange -ay "Volume Group"
sudo /usr/sbin/lvs
sudo /bin/mount /dev/"Volume Group"/"Logical Volume" "mount point"

Unmount a Xen disk image (in order):

sudo /bin/umount "mount point"
sudo /sbin/vgchange -an "Volume Group"
sudo /sbin/kpartx -dv "path to file"

Mount a Xen disk image non-LVM partitions (in order):

sudo /sbin/losetup /dev/loop0 "path to file"
sudo /sbin/fdisk -lu /dev/loop0
sudo /bin/mount -o offset=$(("Start"*512)) /dev/loop0 "mount point"

Unmount a Xen disk image non-LVM partitions (in order):

sudo /bin/umount "mount point"
sudo /sbin/losetup -d /dev/loop0

Virtualization/Turnkey Linux

Add new mountpoint to Proxmox Container

pct set "vmid" -mp[n] mp="/path/in/container","storage":"volume",size="disksize"

Create OpenVZ template from root filesystem

cd "rootfs"
tar --numeric-owner -czf ../"template name".tar.gz .

Create tklpatch from directory

tar --numeric-owner -czf "patch name".tar.gz --exclude='.git' "patch folder"

Extract ISO for tklpatch

tklpatch-extract-iso "isofile"

Apply tklpatch to extracted ISO

tklpatch-apply rootfs-dir patch-dir|patch.tar.gz

SSL/x509 Certificates

View SSL Certificate

openssl x509 -in "cert_file" -text -noout

Verify SSL Certificate

openssl verify -verbose -purpose sslserver -CAfile "ca-cert_file" "cert_file"
openssl verify -verbose -purpose sslserver -CApath "path/to/ca-certs" "cert_file"