CentOS 7 Cheat Sheet: Essential Commands and Configurations
Posted on Aug 1, 2024 in Technology
CentOS 7 Cheat Sheet
Root Password Recovery
- Go to CentOS rescue mode.
- Press e.
- Go to the end of the Linux line and add rd.break.
- Press Ctrl x.
- Type mount -o remount,rw /sysroot/ –> chroot /sysroot/ –> passwd root –> touch /.autorelabel –> and then “exit” * 2
Network Configuration
- nmcli d to check the network name.
- 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.
- ip a to see if applied.
- nmcli con up static to activate if not applied.
Software Package Installation
- dnf install (pkg name) -y
- systemctl enable (pkg name) for persistence.
- systemctl start (pkg name) to start the package.
Partitioning
- 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.
- After writing changes do – partprobe.
- mkfs -t ext4 /dev/sdxx – changes the file system to ext4.
- mkdir /mnt/sdxx – create mount points.
- mkswap /dev/sdxx – creates a swap file system.
- vi /etc/fstab – for persistent mounting.
- Add in a new line:
- For ext4: /dev/sdxx /mnt/sdxx ext4 defaults 0 0
- For swap: /dev/sdxx swap swap defaults 0 0
- systemctl daemon-reload; mount -a; swapon -a; lsblk
LVM Creation
- pvcreate /dev/sdxx /dev/sdxx
- vgcreate VGNAME /dev/sdxx /dev/sdxx
- lvcreate -n LVNAME -L SIZE(800M) /dev/VGNAME
- mkfs -t ext4 /dev/VGNAME/LVNAME
- mkdir /mnt/LVNAME
- Persistence: /dev/VGNAME/LVNAME /mnt/LVNAME ext4 defaults 0 0
- systemctl daemon-reload; mount -a; lsblk
Swap File Creation
- dd if=/dev/random of=/swapfile bs=1M count=1000 (Block size is 1 megabyte) [Write 1000 blocks (1000 * 1M = 1GB)].
- Root permissions only: chmod 600 /swapfile; mkswap /swapfile
- vi /etc/fstab
- /swapfile swap swap defaults 0 0
- systemctl daemon-reload; swapon -a; swapon -s
User Creation
- Add group: groupadd GRPNAME
- Add user: useradd -g GRPNAME USERNAME
- Add password for user: passwd USERNAME
File and Permissions
Permissions Guide
- Numeric Mode: read (r) is 4, write (w) is 2, execute (x) is 1 & no permission (-) is 0.
- Common numeric permissions:
- 777: rwxrwxrwx
- 755: rwxr-xr-x
- 644: rw-r–r–
- 700: rwx——
- Symbolic Mode:
- u: User/owner
- g: Group
- o: Others
- a: All (user, group, others)
- Operators:
- +: Add permission
- -: Remove permission
- =: Set exact permission
Example
- mkdir /tommy-share
- touch /tommy-share/practical-test.sh
- chown tommy.consultants /tommy-share/
- chown tommy.engineers /tommy-share/practical-test.sh
- chmod 755 /tommy-share/
- chmod 700 /tommy-share/practical-test.sh
Permission Modification
Example
- chmod g+s /tommy-share/
- su – tommy
- touch /tommy-share/testfile
- exit
- 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
- ls -l (long listing format) -a (include hidden files) -R (include contents of subdirectories)
- 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)
- ls [[:upper:]]* (files that start with uppercase), ls [[:lower:]]* (files that start with lowercase), ls [[:digit:]]* (files that start with a digit)
- ls [ac]* (find files starting with a or c) ls ???? (files with 4 characters), ls ???* (files with at least 3 characters)
Cron Job
- Edit crontab: crontab -e
- List crontab entries: crontab -l
- Remove all crontab entries: crontab -r
- Edit crontab as root for any user: crontab -u username -e
- 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)
- 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 * *
- 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 * * * *)
- 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