Skip to main content

Posts

Showing posts with the label Linux for noob

Linux Top check a single process

Monitor a single process in Linux, to check if the process is the culprit,.or just simply monitor the resources consume by the process in real time. This can be done easily using Linux, top and pgrep commands Example: Check MySQL daemon. Open terminal and type this:    top -p $(pgrep -f mysqld )  It will display top with only mysqld displayed on the screen. That's it you can monitor the process, observe what it's doing. Till next time, enjoy exploring the command line, Happy Linuxing... Prayer is free, brings comfort to your life. Trust Jesus He will guide you to righteousness. https://soulrefresh.netlify.app

View files in Linux from old to latest aka chronological order

Viewing files in Linux terminal from old to latest, is one of the common tasks a Linux sysadmin will do. And this can be done simply with the good ls command. Plus add a few parameters to make the output more readable and friendly. ls -lhtra this command will view all the files, hidden or not hidden from old to latest Example image below, shows all files; arrange in chronological order. ls -1  the command is ls - plus the number one not the small letter "L"; this is quite useful if need to get the filenames only on a specific directory and feed to xargs command or as an input on any bash script for further processing. Example, typing ls - 1 will output only the filenames on a specific directory. That's it till next time. Enjoy Linux and keep learning. Keep praying and stay open to God's messages. Fiat Voluntas Tua | Catholic Prayers & Quotes

Display GUI Boxes for Linux bash script

 Displaying GUI Boxes in Linux Bash scripts can be done easily using whiptail. In Windows OS, msgbox, inptubox is quite helpful on VBA, Visual Basic and even in .NET world. Displaying Graphical boxes in Linux bash scripting is quite simple to do. To navigate on this GUI boxes, used "Tabs","Arrow Keys" and "Space Bar" as enter key. Here's a simple example of Whiptail that display GUI Boxes and ping the sites that are selected. #!/bin/bash # Step 1: Checkbox URL menu CHOICES=$(whiptail --title "Checkbox Ping URLs Example" \   --checklist "Select one or more options:" 15 50 5 \   "ping -c 2 bing.com;" "Ping bing" OFF \   "ping -c 2 duckduckgo.com;" "Ping ddg" OFF \   "ping -c 2 search.com;" "Ping search" OFF \   "ping -c 2 google.com" "Ping google" OFF \   3>&1 1>&2 2>&3) # Exit gracefully if nothing was selected or user pressed Cancel ...

Bash script safely remove or delete files

Automation is a buzzword in today's technology; it makes things faster, effortless, and most of all ease the burden of system administrators. However, automation should be tested thoroughly to avoid any unforeseen circumstances or else instead of making life easier it might deliver some nightmares. For example, removing log files to save space or remove any unwanted or old log files to make room for new files; this task can be done manually or using some automation via a script. In Linux, cron jobs that fires automatically as per schedule is quite ideal to automate removing or deleting log files. This can also be done manually, by simply logging in to the server and delete files. There is nothing wrong in deleting manually but would not be ideal if the operation of the server is operating day and night.  There might be a chance that the operation will halt if there is no more room to write the log files. Or simply when there is some issue and wanted to troubleshoot; if log files ar...

Practice Linux vim in Windows using gVim

Vim is a cool and powerful editor in Linux system. It is also available for Windows via this download page: https://www.vim.org/download.php Choose: gvim_9.2.0000_x64.exe (64bit installer) or any format of your choice. After downloading and installing, the installer it will create two desktop icons; something similar to the image shown below. Why there are two icons? As the name suggests "gVim Easy 9.2", gVim Easy is the filename suggests it's "easy", upon opening or clicking the icon it will open gVim in "insert" mode and start typing immediately. While the other icon "gVim Read only 9.2", upon clicking or opening this icon. This will open gVim on "read only" as it names suggest. It's quite good if you only need to review or read the file. However, it says "read only" it doesn't mean that you cannot edit, modify or add text to the file. While inside "gVim Read only", press "insert" key or pre...

Linux find files that requires sudo or root access

Root access on a Linux operating system is needed, if you need to maintain the server or if something goes wrong and requires some changes that only root account is able to do. However, sudo can be used to give certain access to files that can be managed by specific users. For example, for whatever reason the system requires changing the DNS server IP address. Then sudo can be used to grant editing privileges to /etc/resolv.conf If just curious what are the files that require sudo or root access, on a Linux VM or server. Find command be used to locate or identify files that requires root privileges. On Linux VM, by typing: sudo id root This will show the output below: uid=0(root) gid=0(root) groups=0(root) The output shows that the user id for root is 0, and it's the same with group id and any other groups. Technically, 0 (zero) is the id for any files on the Linux system that is owned by root or requires root to modify or make some changes. For example, by typing: cd...

Linux: Displays Interface Status in Up or Down State

The `ip link show` command, available in more recent versions of Linux that support the `ip` command, provides information about network interfaces and their respective states. Executing `ip link show` yields extensive details regarding the interface, including its MAC address, operational state, IP address, and additional relevant information. The command below identifies the names of interfaces currently in either an UP or DOWN state. The output is piped to `grep` to filter and display only the state alongside the interface name. To display interfaces in a DOWN state, use the following command: ```ip command: echo "Interface on DOWN state: $(ip link show | grep "state DOWN" | grep -oP '^[0-9]+:\s+\K\S+' | cut -d: -f1)" ``` To display interfaces in an UP state, the command is: ```ip command: echo "Interface on UP state: $(ip link show | grep "state UP" | grep -oP '^[0-9]+:\s+\K\S+' | cut -d: -f1)" ``` The follo...

Linux get default gateway and assigned IP address

How to check default gateway and assigned IP address in Linux? There are quite a few ways to do this in a Linux system. However, in systemd or newer version of Linux systems which support the ip route command all the information is there already. The output of the ip route command shows the default gateway and the assigned or primary IP address of the VM or server. Here's an example to show the default gateway of a Linux system. The IP Address after the word "default via" is the default gateway of the system. Command is: ip r | grep default To show only the default gateway IP Address, we need to use RegEx and match the IPV4 address only. Here's the command: ip r | grep default | grep -Eoh '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' Sample output image: To show the assigned primary IP address on the VM or server, type the command below. Here's the command: ip r | grep -oP 'src \K\d+\.\d+\.\d+\.\d+' Sample output imag...

Postfix read or view the email in queue from the terminal window

Check or read the contents of the email on Terminal for troubleshooting purposes. The Postfix command below will show the entire original message with headers and body, or the email itself in raw format. Like reading email as a text file. Get or grab the postfix ID, by typing: mailq or postqueue -p Once the ID is known use the postcat command to view the contents of the ID. postcat -bh -q F0ABD910CDE33 | less ## this command will show the contents of the email that is on the queue To delete an email use the command below: postqueue -d ID ## delete the queue ID / delete the email with the specific ID, the email will be deleted on the queue used sparingly Proverbs 3:7 Do not consider yourself wise, fear God, and turn away from evil.

Check Chrony logs on.Linux

Chrony service maintains synchronizing with external time sources like NTP servers. In Redhat, Rocky or Alma Linux, Chrony logs can be checked using journalctl. Example: sudo journalctl -u chronyd -xe The output will be limited only to the.log or entries of Chronyd service. To check status of Chronyd, use systemctl status chronyd To further check any log entries pertaining to chronyd, browse to /var/log cd /var/log And type: egrep "*chrony*" . This will check all log entries that has the string chrony, include the dot at the end to check all files on the current path. You can examine those files.if need to troubleshoot further, or just view the configuration of Chrony for any misconfiguration such as typo error, or unsupported Chrony settings. To view the chrony configuration,.type: grep ^[^#] /etc/chrony.conf This will view only the configuration that are currently enabled or uncommented lines, and also remove any blank lines. chronyc sources -v  The above command will show ...

Linux clear logs or big files

To easily clear a log file or empty a big file. Type this on a terminal,.make sure to zip or backup the file if you feel you might need it later. > MyBigLog_file.txt Yes, just type greater than sign,.followed by the filename of a log file or any file. Don't do this in Production if not sure of the consequences. If the file exists it will empty the file, if the file doesn't exist it will create the file. Test it on a VM, to see what it really does before rolling out to production. Cheers,. enjoy Linux and command line. Be still and know God is in control!

Linux VM always disconnected after a few seconds or minutes

VM on VMware or vSphere always got disconnected after a few seconds or minutes. CentOS or RHEL VM cannot be accessed via SSH and unable to activate or make the Interface UP. Typing: ip link show nmcli dev status nmcli gen staus Shows that VM interface is not yet UP. Typing: ip link set ens192 up Will bring the interface UP but still SSH is not working and not able to reach the VM. Typing: nmcli con up ens192 Shows "STATE" connecting but will never shows "connected". Restarting the VM and creating new interface will not help either. Typing: nmtui (network manager text user interface) It shows that the interface has been assigned with correct IP Address, DNS and Gateway But the VM still not available on the network and SSH cannot access to the VM. Well the solution, might just be an overlooked by configuring so fast the VM that a simple option was not carefully set. If the VM was set with IP Address, DNS and Gateway manually, however, ...

Linux change hostname without rebooting

Changing hostname in Linux without rebooting, is quite straight forward. Changing the hostname will also change the name on the Terminal prompt, and will be displayed beside the current login user on the system. Open a terminal window and edit /etc/hostname. Use vi, nano or even echo command to change the content or value of the hostname file. Then type this: sysctl kernel.hostname=new-hostname-office.internal After typing the command, if the new hostname doesn't appear yet. Disconnect from SSH and login again via SSH and the new name will be displayed. That's it, simple as that. If unsure, try on a VM before applying to production. And check whether the desired output is the expected result. Cheers! Till next time. Do ASAP, Always Say A Prayer... Practice O.T.G. = Obedience To God Make time for Prayer and Meditation. Take time to kneel down and Pray! Practice F.IF.T.H (Firm In Faith Towards Heaven) Your attitude will depends your altitude, stay humble!

Check Time zone in Linux and windows machines

In Linux, open a terminal window and type the following command: timedatectl | grep "Time zone" In Windows, open a command promot and type the following command: tzutil /g To set a new timezone in Windows, open an elevated command prompt. Check out this link to set a new Time zone: https://quickbytesstuff.blogspot.com/2014/08/view-and-set-time-zone-via-command-line.html Checking Time Zone will clear up things, if the machine timing is properly sync or not. Or why machine is having a different time with the current location. Stay safe! and Keep things up! Another year is coming! Do ASAP,  A lways  S ay  A   P rayer ... Practice O.T.G. =  O bedience  T o  G od Make time for Prayer and Meditation. Take time to kneel down and Pray! Practice F.IF.T.H (Firm In Faith Towards Heaven)

Check multiple IPs via bash one-liner loop using ping or dig

Ping multiple Linux servers via Bash script. Pinging a Linux server sounds a very common and simple task for any SysAdmin. However, if there are quite a few IP Addresses to check, it's quite daunting as typing on the terminal window is quite tedious and prone to error or mistype. Bash script is a way to go for this type of task. Just prepare all the IP Addresses that needs to be checked in a single text file. Do a loop to check all the IP Addresses on the text file and just watch the Bash do its job. Here's a simple code to do it: for ipx in $(cat ipx.txt); do ping -c 3 $ipx; done ipx.txt <<< should contain all IP Addresses to be checked Example of ipx.txt contents: 1.2.3.4 4.5.6.7 10.10.10.1 .... .... etc.. Above command can also be used to check DNS Reverse record using dig. Example: for ipx in $(cat ipx.txt); do dig -x $ipx; done The above will perform a reverse DNS lookup. There's a lot of uses cases that can be done, with the above command. That's it. Enjo...

Linux sudo revoke permissions

 sudo is quite a good tool in Linux world in which you can specify commands with full access without giving root permissions to everything on the system.   However, sudo can also be used to have access or root permissions to everything on the system.   Why use sudo, when it can access everything? Why not just use or switch to root?   When entering or executing a command, sudo is a safe haven to make sure you know exactly what you are doing or it can provide you a second chance to abort the command. If the command being entered isn't the right one, before entering the password or pressing enter "Ctrl + C" can be used to abort the command and stop the execution. When running on a root session, any commands executer after pressing enter. There's no way to cancel or abort the command. That's why sudo is a good practice to use, so any of change of mind before pressing enter can is still possible to cancel. And running in a non-root session, if the command is...

Zip log files via script or cron job

Zipping log files to save disk space is quite a common task for system admin. Logs can be kept for a certain period of time depending on the company policy. To keep logs files and not delete them is quite a good strategy. If something goes wrong, you will be able to trace and go back in time, and check what went wrong. Zipping log files can be done manually, or via Bash or Shell script. If the folder has a standard pattern, which is quite ideal then creating a shell script to automate the zipping of log files will be easier. For example, if  the log files is on this path /mnt/log folder. And it has this pattern, YYYY-MM-DD. Creating a script to avoid manual task can be done easily. Depending on how busy the application is, log files can grow easily and may fill-up the disk quickly. If the log folder structure is based on Year, Month and Date. Then the shell script knows what pattern to look for and the task can be automated. Example Screenshot of the log folder structure: Below is ...

Linux / WSL vi do save as or create password encryption

Vi / Vim is a powerful tool and for Linux enthusiast, this is a must tool to learn. Text file or basic editing in command line is quite important, to take notes, create a script, store text data or just anything that needs editing or creating via command line or terminal. How to create a text file in command line and put a password or encrypt the file? Text file is readable via lot of tools, cat, less, more, grep or any text editing files. One way to protect the file or its content is to encrypt the file. Screenshot below shows, how to create the file and encrypt it. Example: vi -x TestPasswordEncrypt.txt After entering the command, it will ask for an encryption key twice. And after entering the password, it will open the vi text editor, pressing "i" or the "insert" key then user can start entering text or typing on the vi window. After typing anything, press "esc" key and press :w (colon and w)  to save the file. If user need to save the file with a new f...

How to use Rsync to copy file one after another at a limited bandwidth

Copying files at the same time will ultimately consume all the bandwidth. So, to avoid such issue it would be good to tell Rsync to copy only once the other previous Rsync has finished copying. Copying or transferring files from one system to another location without impacting the bandwith or consuming all available bandwidth, when using Rsync. Using Rsync in Linux to copy files and folders is a common task for Sys Admin. When using Rsync and limiting the upper bandwith or telling Rsync the maximum bandwidth to use can be achieved using the "--limit" option. --limit is in KBps (Kilo Bytes per second and not Kilo bits) --partial is another good option if there is frequent disconnection this will tell Rsync to pick-up or include the files that has been transferred or copied Here's the command on how to use Rsync in Linux to limit the bandwidth when copying: rsync -Pz --limit=30000 <source> <destination> -P = will show the progress -z = tells Rsync to compress fi...

Unable to login to SSH even with a correct password

 In Linux world, SSH is a life. Especially, now with remote work, data center, servers without graphical interface. If you can't login via SSH,  even with a correct password but other user accounts is able to login. Then the person need to check with HR whether he or she is still on the payroll. :)  😄 Of course, glitch, errors and other issues will always be a part of this technology. Nothing is perfect in this world, so errors, technological issues will always be there it won't go away. So, what will will you do if you are sure that the SSH password and username is correct? Like you can login to Server 1, Server 2 but unable to login with Server 3 using the same credentials? If other users with sudo credentials are still able to login, the sudo account can be used to troubleshoot the specific SSH account that is having the issue. What would be the first thing to do? Well, joke as it seems that "restart works always" but the reality is not far from there. Restart SSH...