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.

Commands:
Disabling selinux
# vi /etc/selinux/config
SELINUX=permissive
# reboot


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 delete 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 “!!” > script.sh

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

Finding your extrnal IP at the cmd line
dig +short myip.opendns.com @resolver1.opendns.com

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

Set iDRAC/iLO to DHCP
# ipmitool lan set 1 ipsrc dhcp

Even more IPMI info

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

Kernel info
# uname -a

View network info
# ifconfig

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)

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
or
# 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)
or
# 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

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
  address 172.16.1.25
  netmask 255.255.255.0
  gateway 172.16.1.1
  dns-nameservers 172.16.0.11 10.250.1.12

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

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 172.16.125.29 thru .73)

Postfix
Defaut config location: /etc/postfix/main.cf

Check mail queue
# mailq

Flush mail queue
# postqueue -f

Restart Postfix
# postfix reload

List non-default config items
# postconf -n


Python
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 https://www.python.org/ftp/python/3.8.0/Python-3.8.0a2.tgz
# 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


Vi Cheat Sheet


No comments:

Post a Comment