Tech Junkie Blog - Real World Tutorials, Happy Coding!

Latest Posts

Monday, February 28, 2022

 In Linux managing processes is an essential part of keeping your system running in an efficient state.  In this post we are going to look at the commonly used ps command.

If you just type ps you will get the processes that's currently running along with the PID (process ID), TTY (terminal it is running in), Time (CPU utilization time), and the command that is used to run the process






Here are some of the options that are useful with the ps command:

1. ps -e this will show you all the processes









2. ps aux this will show you all the processes that are not assigned to a user terminal










3. ps -e --forest this will show you the process tree view of the process








There's a nicer way to look at the process tree with the command pstree










4. ps -f gives you the full ps process information which includes the user id and the parent process id





ps -F gives you even more information about the process, it gives you the size and RSS (memory size) of the process




5.  ps -l shows you the long listing, which shows you different kinds of information like the UID (number) and the process priority





6. You can combine options with commands like ps -elf which will give you the full ad long listing








7. As with other commands, you can search for a process when you combine it with the grep command.  Let's say we want to search for all the gnome processes we can type the following ps -elf | grep gnome





8. ps -ly will replace the address column with the resident size (memory size)



Monday, February 21, 2022

 GRUB2 is a wonderful tool, and one of the cool thing you can do is create a custom entry on the boot menu.  Let's say you built a system especially for HR and it's different than the rest of the organization's system.  Well with GRUB2 you can create an entry for the HR system as one of the choices in the boot menu.

Here are the steps to create a custom entry in GRUB2:

1. The first thing we want to do is create a custom file in our root home directory with the following contents.  Log in as root and make sure you are at the root home directory and type vi custom your linux16 entry might be different so make sure you have the right path in production.  In this post we are more concerned about making it show up in the boot menu

menuentry 'HR Department' {
    insmod gzio
    insmod part_msdos
    insmod xfs
    set root='hd0,msdos1'
    linux16 /vmlinuz-3.10.0-327.3.1.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap 
    initrd16 /initramfs-3.10.0-327.3.1.el7.x86_64.img
}







2. To make it show up in the boot menu we want to edit the 40_custom file in the /etc/grub.d/ directory.  So type vi /etc/grub.d/40_custom in the terminal.  Now you want to go to the end of the file and append the custom file to the end of it.  The way you can do that in vi is type esc then type :r /root/custom and press ENTER








The completed file should look something like this

#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.

menuentry 'HR Department' {
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        linux16 /vmlinuz-3.10.0-327.3.1.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap 
        initrd16 /initramfs-3.10.0-327.3.1.el7.x86_64.img
}

Type esc, :x to save the file

3.  Now regenerate the grub.cfg file with the following command grub2-mkconfig -o /boot/grub2/grub.cfg


Now if you reboot the machine you will see the custom entry in the boot menu




Monday, February 14, 2022

 From the past few posts you can see that GRUB2 is a very powerful utility in Linux.  It's so powerful that you can create and change the root password.  One way to prevent unauthorized access to this feature is to password protect it and encrypt the password as well.

Here are the steps to password protect GRUB2:

1. Make a copy of the file /etc/grub.d/01_users with the command cp /etc/grub.d/01_users . in the terminal

2. Go into the grub.d directory with the command cd /etc/grub.d

3. Now you want to edit the 01_users file with the command vi 01_users




4. The file should look something like this, by looking at the file you can see that it's currently using the root encrypted root password for authentication.  What we want to do is use a user that's not in the system to control access to GRUB2.

#!/bin/sh -e
cat << EOF
if [ -f \${prefix}/user.cfg ]; then
  source \${prefix}/user.cfg
  if [ -n "\${GRUB2_PASSWORD}" ]; then
    set superusers="root"
    export superusers
    password_pbkdf2 root \${GRUB2_PASSWORD}
  fi
fi
EOF

Here is what the file will look like with the new user

#!/bin/sh -e
cat << EOF
    set superusers="johndoe"
    password johndoe Topsecret1!
EOF

Save the file with esc then :x enter

5. The next step is we have to regenerate our configuration file with this command grub2-mkconfig -o /boot/grub2/grub.cfg

If you get this error message 

/etc/default/grub: line 7: unexpected EOF while looking for matching `"'

that means your /etc/default/grub file is missing a double quote, this might be a bug in the Linux OS, some people don't seem to have this issue. So the solution is to edit the file and add a quote to the second to last line












6. Now if you press e at the GRUB menu you will  be prompted a username and password, you can authenticate by typing in the username and password you just specified




After typing in the username you will have access to functions for system administrations, just type Ctrl+x to go through the normal boot process












7.  That's great and all but the password is stored in clear text and a someone can just look at the file to figure out what the password is.  To encrypt the password type in the command grub2-mkpasswd-pbkdf2 to get the encrypted password.  Copy the encrypted password into the clipboard





8. Go into the grub.d folder with the command cd /etc/grub.d/ and edit the 01_users command again to change the password to an encrypted password, the file should look like this

#!/bin/sh -e
cat << EOF
    set superusers="johndoe"
    password_pbkbf2 johndoe grub.pbkdf2.sha512.10000.D923C3338B8C00DEA2546724EF33CD91B37DB0B52502148B387ACDFDA2A3628777A8D68ADD009044E6A590E59EECEE5B243D594EC11ED25EF502227EBA425FDC.CEB89F60CE9826B57A116B7049CB2F9C359BF0793B9AB210E75E394A503EFAB8C9C56EF4C2CF7BB7A55E267C938D578AED9D26ABEC3677E92EE4203128558BE4
EOF

Obviously your encryption string will be different depending on your password, save the file by typing esc, :x.

9.  Regenerate the grub.cfg file with the command grub2-mkconfig -o /boot/grub2/grub.cfg

10.  Now when you are confronted with the password prompt at the GRUB menu you can type in the password as you were before but now it's encrypted

Monday, February 7, 2022

 Having to change our grub menus with a text editor every time we want to make a change and be cumbersome and error prone.  Luckily there's a tool that we can use to work with grub called grubby.  In this post we are going to go over some of the useful commands that comes with grubby.

The first command we are going to look at is the grubby --default-kernel as you may have guess this will display the current default kernel



In addition to showing the default kernel we could set the default kernel with the command --set-default kernel command, with this command though you need to know the exact path to the kernel.  Let's set the default kernel to be the second choice.  To do that we want to see all the choices first, to list all the kernels we type the command grubby --info=ALL












From the command we see that the second choice is kernel=/boot/vmlinuz-3.10.0-1160.el7.x86_64

Now we can set the default kernel to be the second choice by typing gubby --set-default /boot/vmlinuz-3.10.0-1160.el7.x86_64





Now if you reboot you will see that the second choice is selected in the boot menu instead of the first choice






Set the default kernel to the first choice again.

grubby --set-default /boot/vmlinuz-3.10.0-1160.25.1.el7.x86_64

Another useful use of grubby is the ability to add and remove arguments so let's we want to add the arguments rhgb and quiet to the default boot option we can just type grubby --args="rhgb quiet" --update-kernel /boot/vmlinuz-3.10.0-1160.25.1.el7.x86_64










To remove the argument just add the --remove to the args command like so  grubby --remove-args="rhgb quiet" --update-kernel /boot/vmlinuz-3.10.0-1160.25.1.el7.x86_64










Monday, January 31, 2022

 There are times when you need to reinstall GRUB which is the tool to manage Linux boot system. It could be corrupted and such.  It's also important to know the defaults that you have to work with once it's installed.  In this post we are going to go through the steps to do both.

Here are the steps to install GRUB.

1. Log in and open a terminal session as root

2.  There's two ways install GRUB that first is to install it locally by typing grub2-install /dev/sda and the second way is to use the yum package manager to install.  Type yum reinstall grub2-efi shim notice we are using the reinstall option for the yum command because grub should already be in the system we are just reinstalling because something is wrong with it
















2.  Once you have GRUB installed you can work with the defaults by using a text editor to edit the /etc/default/grub configuration file.  So type vi /etc/default/grub to edit the grub default.

We are going to change the countdown to 10 seconds instead of 5 and set the GRUB_DISABLE_RECOVERY="true" to get rid of the recovery options in the boot menu, esc :x to save the changes






3. Once the changes have been made to the file you have to run the command grub2-mkconfig -o /boot/grub2/grub.cfg to regenerate the grub configuration file








4. Now if you reboot you should not see the recovery options anymore in the boot menu and the countdown will be 10 seconds instead of 5



Monday, January 24, 2022

 On normal startup of the Linux system you probably see this menu every time you boot up















In this post we are going to add a recovery mode for each of these choices.

Here are the steps to create recovery mode options in the GRUB menu:

1. Log into the system normally

2. Open a terminal session, switch to root privilege or root user

3. Type vi /etc/default/grub to edit the grub file

4. Once you are in the grub file find the line that says GRUB_DISABLE_RECOVERY="true" and change it to GRUB_DISABLE_RECOVERY="false" then press esc then type :wq!










5.  When you get your command prompt back type grub2-mkconfig -o /boot/grub2/grub.cfg to regenerate the grub file



6. When you reboot the system you will see that for each choice on the menu there's an equivalent recovery mode option, you can press esc to stop the menu countdown




Monday, January 17, 2022

 There are times when you need to change the root password, it could be that you are trying to prevent a malicious former employee or that you yourself have forgotten the password.  Well you don't have to rebuild the system.  That would be an option and a costly one at that.  What you could do instead is to reset the root password on the existing install.

Here are the steps to reset the root password on your Linux system:

1. Boot up your Linux system normally when you get the boot menu press esc to stop the countdown, then press e to edit the boot file.



2. Go to the line where you see rhgb and quiet then remove the options from the line









3. Replace the options with rd.break and enforcing=0 the option tells Linux to break at the ram disk break point in the boot process and the if you are using SELinux to enforce it, be mindful that the \ is not part of the command, it's a line break symbol in the boot configuartion file.





4. Press Ctrl+x to continue with the boot process, as you can see you are now stopped at the switch_root:/# prompt which will give you the opportunity to switch to the root's filesystem













5.The first thing we have to do is remount the root filesystem because it's currently set read only.  We want to set it to read and write so that we can reset the root password., if you want to clear the screen type Ctrl+l
6. You accomplish this by typing mount -o remount,rw sysroot
7. Now we want to set a false root call chroot and point it to the root filesystem with the command chroot /sysroot

8. So with the /sysroot filesystem remounted with read and write permission and  chroot as the false root we can now run the passwd command and change the password like we normally would






9. Now it's very important that we set everything back to normal, so type exit to get out and type 

mount -o remoumt,ro /sysroot

to set the root filesystem back to read only, type exit to continue with the boot process

10. Depending on weather you have SELinux enabled or not, your experience will might be different.  If you don't have have it enable you will have it enabled then you will be taken to the login screen and your machine will operate the way that it should with the new root password. But regardless of your experience you should revert boot file configuration back to the way it is by doing the reverse of 2 and 3

11. Before we even do that we want to reinforce security if you have it enabled before by typing the following commands

restorecon /etc/shadow

setenforce 1





Monday, January 10, 2022

 I've done a post on this before, but it was for an older version of Linux CentOS it no longer works for CentOS 8 Stream.  So instead of updating the existing post, I am doing another post so it won't confuse you.  The older post is for older version of CentOS and this one is for CentOS 8

The first thing you noticed after your install of CentOS on VirtualBox is that it's pretty small even if you put it in Fullscreen mode, it doesn't not fill the whole screen at all, as a matter of fact the size stayed the same.

In this post we are going to install the Guest Addition tool in VirtualBox to make the VM fullscreen truly fullscreen. 

Here are the steps to make the VM fullscreen:

1. Log into your system as root

2. Run dnf update -y, if you have no internet access type dhclient -v to get access

3. Next we want to install Red Hat's repository of tools with this command dnf install epel-release















4. Once the update is finished run the command dnf install gcc make perl kernel-devel kernel-headers bzip2 dkms -y to install headers and build tools





5. Once the headers and tools are installed run the following command dnf update kernel-* to get the latest version of the kernel

6. Reboot the system
7. Install the Guest Addition tool by clicking on "Devices" and select "Insert Guest Additions CD image"






















8. Click on the "Run" button






9. Type in the root password and click on "Authenticate"















10. If you don't see your VM going fullscreen, then try reboot

11. Once rebooted click on "View", then "Full-screen Mode" to use your VM in fullscreen mode



Monday, January 3, 2022

LVM in Linux allows multiple disks to be combined into one called logical volumes via software.  In this post we are going to create LVMs for a brand new system.  There are two scenarios, the first is LVM on a new system and the second scenario is to create LVM on an existing system.

I am going to create the new system on VirtualBox, I will not go through the process of create a new VM on VirtualBox because I've covered that in the past.  You can search for it on my site if you are lost.

Here are the steps to creating LVM during installation:

1.  On the first screen of installation select the first option




















2. Select your language, then click "Continue"



























3. On the "Installation Summary" screen click on "Network & Hostname"



























4. Give your system a hostname and click "Apply", then click "Done"



























5. Click on "Installation Destination"



























6. Select disk on the screen and make sure you select "Custom




























7. On the "Manual Partitioning" screen select LVM




























8. Then click on the + sign
9. The first mount point you are going to create is the /boot mount point because it is required, but it doesn't require a lot of disk space, so we are going to allocate 500m.  A custom Logical Volume basically let's you divide the 8GB on the disk anyway you like instead of letting the operating system decide for you.  That's why most systems run out of disk space at the root level which is / because the operating system did not allocate enough space at installation. Click on "Add mount point" when done

10. On the confirmation screen reselect LVM on the drop down if Standard is selected, accept the default, notice now you only have 7.5GB of disk space left, that's because you allocated 500MB to the /boot mount.

11. Click on the + sign again, this time assign 2 GB to the /home mount, follow the steps above to create the /home mount point with 2 GB




12. Repeat the steps for /var and allocate 2 GB to it and the mount swap with 1 GB, your screen should look like the following when you are finished


13.  You should be left with about 3 GB of disk space left, the best thing we can do right now is to assign it to the root / mount so that we are using all the capacity of the disk.  To do that click on the + sign again, select / from the mount dropdown and type all in the "Desired Capacity" field, then click on the "Add mount point" button


Your screen should look like the following when you are done, click "Done"


Error checking will be performed and a message will pop up telling you to click "Done" again.  "Accept Changes"

14.  While Error checking is performed on the disk, create a root password by clicking on "Root Password" follow the direction to create a root password

15. For Error checking, it says that /boot cannot be LVM so change it to Standard

Note: I had to add a bigger disk on VirtualBox because apparently CentOS required more disk space for installation.  It would not pass the installation requirements, so I used a 25 GB disk.  But the instructions should be the same.


16.  Once the root password has been created click on the "Begin Installation" button.


17.  This step is going to take awhile.

18.  Once installation is complete remove the installation media and click on "Reboot System"

19. On reboot for the first time you will be asked to create a new user, that's up to you.  I usually create one just for convenience.  If you don't want to create a user just accept the license and click "Finish Configuration"

20. Once you completed the configuration steps and logged in, type df -h in the terminal and you will see that the disk's setup is as you specified in your installation






Search This Blog