CentOS 7 Cheat Sheet: Essential Commands and Configurations

CentOS 7 Cheat Sheet

Root Password Recovery

  1. Go to CentOS rescue mode.
  2. Press e.
  3. Go to the end of the Linux line and add rd.break.
  4. Press Ctrl x.
  5. Type mount -o remount,rw /sysroot/ –> chroot /sysroot/ –> passwd root –> touch /.autorelabel –> and then “exit” * 2

Network Configuration

  1. nmcli d to check the network name.
  2. Example command: nmcli con add con-name static ifname enp0s8 type ethernet ipv4.method manual ipv4.address 192.168.2.200/24 ipv4.gateway 192.168.2.1 ipv4.dns 192.168.2.254 +ipv4.dns 172.16.5.25 autoconnect yes
    • nmcli: This is the command-line tool for controlling NetworkManager.
    • con add: This subcommand is used to add a new connection.
    • con-name static: This sets the name of the connection to “static”.
    • ifname enp0s8: This specifies the interface name as “enp0s8”.
    • type ethernet: This sets the connection type to Ethernet.
    • ipv4.method manual: This indicates that the IPv4 configuration will be set manually, not via DHCP.
    • ipv4.address 192.168.2.200/24: This sets the IPv4 address to 192.168.2.200 with a subnet mask of 255.255.255.0 (indicated by /24).
    • ipv4.gateway 192.168.2.1: This sets the default gateway to 192.168.2.1.
    • ipv4.dns 192.168.2.254: This sets the primary DNS server to 192.168.2.254.
    • +ipv4.dns 172.16.5.25: This adds a secondary DNS server at 172.16.5.25. The ‘+’ indicates that this is an additional DNS server.
    • autoconnect yes: This enables the connection to automatically start when the system boots up.
  3. ip a to see if applied.
  4. nmcli con up static to activate if not applied.

Software Package Installation

  1. dnf install (pkg name) -y
  2. systemctl enable (pkg name) for persistence.
  3. systemctl start (pkg name) to start the package.

Partitioning

  1. Common commands:
    • m: Display help menu.
    • p: Print partition table.
    • n: Create a new partition.
    • d: Delete a partition.
    • t: Change partition type.
    • w: Write changes and exit.
    • q: Quit without saving changes.
  2. After writing changes do – partprobe.
  3. mkfs -t ext4 /dev/sdxx – changes the file system to ext4.
  4. mkdir /mnt/sdxx – create mount points.
  5. mkswap /dev/sdxx – creates a swap file system.
  6. vi /etc/fstab – for persistent mounting.
  7. Add in a new line:
    • For ext4: /dev/sdxx /mnt/sdxx ext4 defaults 0 0
    • For swap: /dev/sdxx swap swap defaults 0 0
  8. systemctl daemon-reload; mount -a; swapon -a; lsblk

LVM Creation

  1. pvcreate /dev/sdxx /dev/sdxx
  2. vgcreate VGNAME /dev/sdxx /dev/sdxx
  3. lvcreate -n LVNAME -L SIZE(800M) /dev/VGNAME
  4. mkfs -t ext4 /dev/VGNAME/LVNAME
  5. mkdir /mnt/LVNAME
  6. Persistence: /dev/VGNAME/LVNAME /mnt/LVNAME ext4 defaults 0 0
  7. systemctl daemon-reload; mount -a; lsblk

Swap File Creation

  1. dd if=/dev/random of=/swapfile bs=1M count=1000 (Block size is 1 megabyte) [Write 1000 blocks (1000 * 1M = 1GB)].
  2. Root permissions only: chmod 600 /swapfile; mkswap /swapfile
  3. vi /etc/fstab
  4. /swapfile swap swap defaults 0 0
  5. systemctl daemon-reload; swapon -a; swapon -s

User Creation

  1. Add group: groupadd GRPNAME
  2. Add user: useradd -g GRPNAME USERNAME
  3. Add password for user: passwd USERNAME

File and Permissions

Permissions Guide

  1. Numeric Mode: read (r) is 4, write (w) is 2, execute (x) is 1 & no permission (-) is 0.
  2. Common numeric permissions:
    • 777: rwxrwxrwx
    • 755: rwxr-xr-x
    • 644: rw-r–r–
    • 700: rwx——
  3. Symbolic Mode:
    • u: User/owner
    • g: Group
    • o: Others
    • a: All (user, group, others)
  4. Operators:
    • +: Add permission
    • -: Remove permission
    • =: Set exact permission

Example

  1. mkdir /tommy-share
  2. touch /tommy-share/practical-test.sh
  3. chown tommy.consultants /tommy-share/
  4. chown tommy.engineers /tommy-share/practical-test.sh
  5. chmod 755 /tommy-share/
  6. chmod 700 /tommy-share/practical-test.sh

Permission Modification

Example

  1. chmod g+s /tommy-share/
  2. su – tommy
  3. touch /tommy-share/testfile
  4. exit
  5. ls -l /tommy-share/testfile (You should see that consultants should have file permissions (Tommy, consultants) even though Tommy is part of engineers and the file permission should have Tommy, engineers.)

File Searching

  1. ls -l (long listing format) -a (include hidden files) -R (include contents of subdirectories)
  2. ls a* (files starting with a), ls *a (files ending with a), ls *a* (files containing a), ls [!a]* (files that do not start with a)
  3. ls [[:upper:]]* (files that start with uppercase), ls [[:lower:]]* (files that start with lowercase), ls [[:digit:]]* (files that start with a digit)
  4. ls [ac]* (find files starting with a or c) ls ???? (files with 4 characters), ls ???* (files with at least 3 characters)

Cron Job

  1. Edit crontab: crontab -e
  2. List crontab entries: crontab -l
  3. Remove all crontab entries: crontab -r
  4. Edit crontab as root for any user: crontab -u username -e
  5. Crontab format is: minute hour day month day_of_week username cmd_to_run
    • min = 0-59
    • hr = 0-23
    • day = 1-31
    • month = 1-12
    • day_of_week = 0-7 (Sunday is 0 or 7)
  6. Common Time Specifications:
    • Every minute: * * * * *
    • Every hour at 0 minutes: 0 * * * *
    • Every day at 2:30 AM: 30 2 * * *
    • Every Monday at 6:00 PM: 0 18 * * 1
    • Every 15 minutes: */15 * * * *
    • First day of every month at 3:00 AM: 0 3 1 * *
  7. Special Keywords:
    • @reboot: Run at startup
    • @yearly or @annually: Run once a year (0 0 1 1 *)
    • @monthly: Run once a month (0 0 1 * *)
    • @weekly: Run once a week (0 0 * * 0)
    • @daily or @midnight: Run once a day (0 0 * * *)
    • @hourly: Run once an hour (0 * * * *)
  8. Example: 6:32 PM on the 17th, 21st, and 29th of November plus each Monday and Wednesday in November each year = 32 18 17,21,29 11 mon,wed