Linux Cheatsheet

This would typically warrant just a blog post, but I refer to my Linux command entries so often a "greatest hits" list/page is in order.

Most of the commands will be CentOS/RHEL related since that comes across my screen the most often. Hopefully others will find this stuff useful as well.

And a quick shout out to a few of the sites that have provided invaluable help in the last few years, thanks so much for saving my bacon!
All commands are listed as if run by root, rather than prefix everything requiring elevated access with sudo. Variables are in red italics for easy recognition.

Vi Cheat Sheet

Shell Scripting Cmd Arguments

Disabling selinux
# vi /etc/selinux/config
# reboot

Add date stamps to History cmd
# HISTTIMEFORMAT="%d/%m/%y %T "
Add it permanently...
# echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile

Software management using Yum
# yum -y install mlocate
# yum search ifconfig (search for pkg, helps when app is part of a larger install)
# yum whatprovides  ifconfig (search for pkg, helps when app is part of a larger install)
# yum erease telnet
# yum list q* (list installed pkgs that start with q)

Software management using RPM
# rpm -ivh cool-pkg-1.2-3.el7.x86_64.rpm (-i installs a pkg, v=verbose, h=hashes for progress)
# rpm -Uvh cool-pkg-1.2-3.el7.x86_64.rpm (-U upgrades)
# rpm -e cool-pkg-1.2-3.el7.x86_64.rpm (-e uninstalls/erases)
# rpm -qa | grep cool-pkg (-qa lists installed packages, while grep (optional), search through results)

List files and directories, and sort by Size
# ll –hSr

List files and directories with Year
# ll --full-time

Unpacking a tar.gz file
# tar -xzf  cool-pkg.tar.gz (-x expands the file, -z unzips gzip files, -f specifies which file (must be last))

Rerun a command
# !!

Changing Directory
# cd ~  (current user's home directory)
# cd - (back to last directory)

Save previous cmd as a Script
# echo “!!” >

Managing Services (CentOS7/RHEL7)
# systemctl list-unit-files (list services and status)
# chkconfig --list (lists older services)
# chkconfig --list | grep 3:on (lists older services that start with runlevel 3)
# systemctl enable snmpd.service (for auto-start)
# systemctl start snmpd.service (start/stop/reload/restart)

Managing Services (CentOS/RHEL 5&6)
# chkconfig --list (lists all services)
# chkconfig --list | grep 3:on (lists services that start with runlevel 3)
# chkconfig network on or off (for auto-start setting)
# service network start (stop/restart/reload)

Repair XFS
# xfs_repair -L /dev/mapper/centos-root

Drive Health Cmds
# smartctl -H /dev/sda
# badblocks -b 512 -s /dev/sda

Network Speed
# ethtool eth0 (em1, p1p2, etc.)

Determine DHCP Server Info
# cat /var/lib/dhcp/dhclient.eth0.leases

Install IP Command Missing from RHEL Distro
# yum -y install iproute

Finding your extrnal IP at the cmd line
# dig +short
# host | grep " has" | awk '{print $4}'
#wget -qO- | xargs echo

Using NetworkManager at the cmd line (NetworkManager service must be running)
# nmtui

Disabling NetworkManager (allows direct editing of ifcfg-eth0 files)
# systemctl stop NetworkManager.service
# systemctl disable NetworkManager.service
# systemctl enable network.service
# systemctl start network.service
# reboot

Managing Firewall in RHEL/CentOS 7
See what's being allowed through:
# firewall-cmd --list-services
# firewall-cmd --list-ports
# firewall-cmd --list-all

List current zone(s): default is public
# firewall-cmd --get-active-zones

Adding access to a specific zone:
# firewall-cmd --permanent --zone=public --add-port=2888/tcp
# firewall-cmd --permanent --zone=public --add-service=http
Or all zones:
# firewall-cmd --add-port=132/tcp  --permanent
# firewall-cmd --add-services=ntp

To make the changes active:
#firewall-cmd --reload

Show mounted file systems
# mount -l
# mount -l -t ext4 (-t designates type, i.e. xfs, nfs4, etc.)

Reload the NFS exports after changing /etc/exports
# exportfs -ra

Unmounting NFS share
# umount /builds

Finding iDRAC/iLO Address
# modprobe ipmi_si; ipmitool lan print

# ipmitool lan set 1 ipsrc dhcp

Changing an iDRAC/iLO IP Address
# ipmitool lan set 1 ipsrc static (if it is currently DHCP)
# ipmitool lan set 1 ipaddr
# ipmitool bmc reset cold (restarts IPMI HW)

Even more IPMI info

Uninstalling ipmitool and components (in case you had to install it temporarily)
# yum -y erase ipmitool OpenIPMI net-snmp-libs OpenIPMI-libs OpenIPMI-modalias

List version of Red Hat/Fedora/CentOS
# cat /etc/redhat-release

List version of SuSE
# cat /etc/SuSE-release

List verion of Ubuntu
# cat /etc/lsb-release

List verion of Debian
# cat /etc/debian_version

Kernel info
# uname -a

View network info
# ifconfig  or  # ip addr

Add an IPv6 address
# /sbin/ip -6 addr add fd63:6f75:6368:20d3:ac3c:257e:9c5:6619/64 dev eth0

DNS servers (also may be in /etc/sysconfig/network-scripts/ifcg-eth0)
# cat /etc/resolv.conf

Hostname and Gateway
# cat /etc/sysconfig/network

Set hostname in RHEL/CentOS 7
# hostnamectl set-hostname yourcoolhostname

Shell being used
# echo $SHELL

List cron jobs (scheduled tasks)
# crontab -l (-e to edit)

Motherboard Info
# dmidecode -t 2

CPU Information
# cat /proc/cpuinfo

Amount of Memory
# cat /proc/meminfo

Mounted filesystems (in MB/GB)
# df -h

Check PCI Buses and Devices
# lspci

Check USB Buses and Devices
# lsusb

Crazy amount of Hardware Data
# dmesg | less
# lshw

Show a list of recent logins
# last (-F will also list year, only works with newer OSes)

Process list (check out additional switches)
# ps -A
# ps aux | grep program (search for running processes by name)

Current performance and PIDs (you can use kill to end a runaway process)
# top

Stop a runaway process
# kill -9 32440 (get process number from using top)

Current memory usage (in MB/GB)
# free -h

Disk usage (directories by size, based on your current location, usually best to start from root /)
# du -sh *
Single directory
# du -sh /some/dir

Check type of filesystem
# file -sL /dev/sd*

Network status (lots of switches to drill down with)
# netstat -a

Time since last Reboot
# uptime

Searching for text within a file, or files, throughout the OS
grep -Ril "text-to-find-here" /
-R recursive
-i ignores case
-l show file name

Finding a file
# find / -name "filename" (searches the whole file system)
# find -name "filename" (will search just the current directory)

Change Default GRUB2 Entry
List entries
# grep "^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2

Change the default boot selection
# grub2-set-default 1 (the list starts with 0)

Confirm the change
# grub2-editenv list

Save settings
# grub2-mkconfig -o /boot/grub2/grub.cfg
Mounting a new drive:
Find disk to add, i.e. /dev/sdb
# fdisk -l

Select disk to edit
# fdisk /dev/sdb

  Add a partition
   : n (for new)
   : p (for primary partition)
   : 1 (partition #)
   : w (write changes & exit)

Format the new partition (here you can choose xfs, ext3, etc.)
# mkfs -t ext4 /dev/sdb1

Add a mount point
# mkdir /data

Add to /etc/fstab
# /dev/sdb1    /data   ext4    defaults     0 0

Mount the new drive
# mount -a

Mounting a new drive over 2TB:
Find disk to add, i.e. /dev/sdb
# fdisk -l

Use Parted to setup the disk
# parted /dev/sdb
(parted) mklabel gpt (answer yes to the warning)
(parted) unit TB
(parted) mkpart primary 0.00TB 3.5TB
(parted) print
(parted) quit

Format the new partition (here you can choose xfs, ext3, etc.)
# mkfs -t ext4 /dev/sdb1

Add a mount point
# mkdir /data

Add to /etc/fstab
# /dev/sdb1    /data   ext4    defaults     0 0

Mount the new drive
# mount -a

Increasing an LVM partition (after some work with Volume & Logical Groups)
# resize2fs  or  #xfs_growfs

Wiping the password in Single User Mode (useful for CentOS/RHEL6)
# passwd -d root 

Single user mode in Ubuntu
1. Interrupt boot and click "e" to edit the GRUB config
2. Add 1 to the end of the linux statement
3. Press Ctrl+x to start the system

Enable Root Login on Ubuntu
(Login as standard user)
$ sudo su -
# passwd
# sed -i 's/prohibit-password/yes/' /etc/ssh/sshd_config
# systemctl restart sshd

Sample Ubuntu /etc/network/interfaces file
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0 (makes the interface start with the OS)
iface eth0 inet static

# ifdown eth0 && sudo ifup eth0 (to make change, service networking restart often fails)
# /etc/init.d/networking restart (may also work)

Sample Netplan Configuration /etc/netplan/01-netcfg.yaml file
    version: 2
    renderer: networkd
       dhcp4: no
       addresses: []
          addresses: [,]

Test the config with # netplan try  and  confirm with # netplan apply

Misc. notes:

Add a space before a command to omit from history

Perform a command repeatedly over a range of systems (x is variable , {range of #'s}, do cmd)
for x in 172.16.125.{29..73};do ping -c 1 $x; done (this pings thru .73)

Defaut config location: /etc/postfix/

Check mail queue
# mailq

Flush mail queue
# postqueue -f

Restart Postfix
# postfix reload

List non-default config items
# postconf -n

Installing Python 3.8.x on Ubuntu

1. Install Prerequisites
# apt-get install build-essential checkinstall
# apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

2. Download and extract Python 3.x.x (latest was 3.8.0a2 at time of writing)
# cd /usr/src
# wget
# tar xzf Python-3.8.0a2.tgz

3. Installing Python
# cd Python-3.8.0a2
# ./configure --enable-optimizations
To keep it from replacing the default python binary
# make altinstall

4. Check and confirm version
# python3.8 -V

No comments:

Post a Comment