Skip to main content

Posts

Showing posts with the label Linux

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...

Linux find up interfaces

If a Linux box VM, WSL or a physical server is unable to connect to Network, one possible issue is the network interface is not UP. Typing:   ip address show   Or:   ip a s  The output will show which interfaces are UP. If the output shows like:  eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 UP after multicast means that the interface is not disabled on the OS or administratively enabled LOWER_UP means that a cable is connected  and is able and is able to find an active switch port  Running this command below: tcpdump -D The output will also show that the interface is Up, Running and connected. Example: 1. eth0 [Up, Running, Connected] Basic commands that will help to save the day if ever there is some network or connectivity issues. That's it.. till next time. Stay close to Jesus, you will find comfort and amazing peace. All you need is to surrender wholeheartedly whatever you're going through.

HAProxy cannot bind port not listening

HAProxy is an open-software used as a high-performance load balancer and reverse proxy for TCP. To check whether the proxy config before reloading or restarting the HAProxy service can be done via this command:  haproxy -c -f /etc/haproxy/haproxy.cfg It will show 'configuration file is valid' if everything is okay or the config file syntax is valid. However, the command cannot check whether it can bind the port, or bind the socket. Example, typing: systemctl reload haproxy to reload the new config file. And to check whether the reload is successful or not status of the service can be checked. Typing, systemctl status haproxy to check the status and if it shows Reload: failed then binding issues or other errors causes reloading to fail. Or some alert is shown as Binding like /etc/haproxy/haproxy.cfg cannot bind socket or cannot assign requested address for 192.168.x.x. IP Technically, the error is already giving some hints that the IP on the HAProxy config file cannot be used fo...

Newly built Linux box cannot SSH

Fully functional and brand new built VM, Physical server, Container or any boxes with Linux distro, however, SSH is not working. Newly built Linux boxes with Server OS, the SSH service or remote access to the box won't start functioning even though the server is up and running. For the SSH to start it needs to have the required SSH keys for the service to start. The solution is to run this command: ssh-keygen -A After typing the command, try systemctl start ssh or systemctl start sahd Also check the status by: systemctl status ssh / systemctl status sshd And also good idea to start SSH once the system is restarted. Type: systemctl enable ssh / systemctl enable sshd Once the service or status of the SSH is confirmed running, then the system or device can connect remotely or other devices can connect to the local system as well. That's it, till next time. Enjoy exploring the Linux World! Prayer connect us to God, line is never congested! Have Faith and Trust Jesus, He always do g...

Linux variables to get UID/GID

User ID(UID), GID (Group ID) in Linux are numerical identifiers to set access to files, directories and system resources, just like rwx which in octal equivalent is r=4, w=2, x=1 and the desperate permission which will equal to 777.  777 is quite useful when nothing is working and an easy way out but yes use sparingly. UID and GID can be used also to restrict permissions or access. Quite a few ways to get UID value for the current login user. At terminal typing: echo "$UID" id -u echo/run/user/$UID All commands above are the same, get the user id of the currently logged in user. To get the GID, or the group membership of the currently logged in user is quite straight forward, at terminal type: id The output will be the  group-id(name of the group) and those are the group membership of the currently logged in user. id -Gn will just list the groups without the GID. id -G will just list the GIDs without name group Another quite useful variable, if let's say someone is asking...

vim or vi searching for whole words or string

Search for a whole word or string in VI or VIM, can be done easily and a time savee so no need to keep pressing next or 'n' for the next match. How to search for a whole word or string in VI or VIM? Example open a file with a list of IP addresses. vim list-of-local-ips.txt Example contents: 192.168.13.130 192.168.13.131 192.168.13.1 192.168.13.135 192.168.13.137 192.168.13.23 192.168.13.13 192.168.13.100 If vi or vim is insert mode, press ESC until it's in command mode, If need to search for 192.168.13.1, or just 13.1 In comand-line mode type:  :/\<13.1\> and press enter  Or see picture below: After pressing enter it will go directly to 192.168.13.1,  if /13.1 is type then it will match also 192.168.13.135, 192.168.13.131 etc.. But with the /\<word or string\> syntax it will find the whole word. That's it, explore vi/vim for time saver tips. Cheers! Lift up your needs in Prayer; with a humble and contrite heart. God listens to the broken hearted....

Move specific log files via Bash

Log files are important, for audit purposes, to check what's going on and a file to consult when something doesn't work as expected. However, log files can fill up disk space easily especially if the application or server being monitored is quite buya, thus it will also record quite a few lines on the log file. Of course the easy way, is always to do an mv command and just move the file somewhere. This is not practical if you need to move hundreds of files. To make things easy and efficient is to use a group or series of commands. For example, if you need to move files other than the log files for the month of November, then ls and egrep can do it. Example: ls | egrep -iv "*2025-11*" | egrep -iv "2025-11-template"  Above will list all files except files for the month of November and the template for the month of November. -iv tells egrep insensitive case, and don't include the pattern specified and list all files Double check the output if the ls and egr...

Add SSH keys to known-hosts in Linux

Setting up a new environment of Linux boxes either Physical servers, VMs or Containers, one thing to consider is how you will login or manage those devices. In Linux world, SSH is the most common way to manage headless servers or VMs. SSH keys must be added to ~/.ssh/known_hosts to preload to the host keys before connecting to the new servers. There quite a few ways to do it, via bash script, or simply logging in and copying manually the key.but quite tedious and not the practical way if there are hundreds of more servers. ssh-keyscan server1 server2 server3 >> ~/.ssh/known_hosts This will suppress the question whether you want to add the keys or not, and type yes to continue. ssh-keyscan won't require any manual intervention once the connectivity is established it will just add all the keys available on the remote servers to known_hosts. Yes rather than typing ssh-keyscan server1 server2 it would be better to have a list on a text file and use while loop to read the file and...

Linux Bash rename multiple files

Bash script/command below shows how to rename multiple files. If the filename shows some pattern, then it will be easier to rename.  Example filename: file_text_a.txt file_text_b.txt file_text_c.txt file_text_etc... ... .txt Script/command below, shows some magic to rename files all at once. The command below can be integrated to a script or simply run the command on the terminal. Create some dummy files for testing before running on actual files to be renamed or in production. find . -name "*.txt" -exec sh -c '   for file; do       newf=$(echo "$file" | sed "s/text/tested/" )  &&   mv "$file" "$newf"   done ' sh {} + Output is: file_tested_a.txt file_tested_b.txt Since the command uses sed, replace 'text' with any string to be searches and replace 'tested' also with any new string for the filename. Till next time.. Enjoy Linux and scripting... Put your trust in God and have Faith. Exodus 14:14, "The...

Systemd *-ctl commands to manage Linux system

Systemd ecosystem provides tools to manage the Linux system. Here are some quick commands to check Linux system status. systemctl -  manage system services and units timedatctl - check system time, date and NTP localectl - quickly check system locale, language, kb layout hostnamectl - check static hostname, OS, kernel, architecture x86 or x64 resolvectl - quickly check DNS IP networkctl - quickly check interfaces names, operational status of interfaces, and setup whether managed or unmanaged loginctl - check session if, username, and for how long the user is logged in journalctl - to check services logs why some services are not starting  Those are the basic and most common ones. Enjoy Linux-ing. Keep the Faith alive, Trust Jesus! God bless!

Reading uncommented line in Linux and Windows

Reading uncommented line is almost a daily live for most Linux and Windows Sys Admin. Uncommented in .conf or .ini lines are the active or the lines that are currently in used for the loaded configuration. In Linux, it's quite straight forward to do it using grep. grep ^[^#] service_file.conf The above command uses regex, to search for lines in the .conf file that doesn't start with #. In regex ^ means the first character, but if it is inside the brackets it has other meaning. It negates whatever character that follows. So, the regex means search the first character that doesn't start with #. Technically, it's teling grep to search for uncommented lines. Yes, the above solution is for Linux. How can we do it in Windows? Well, it turns out to be almost the same but with a little twist of course since its a different operating system. So, here's how we can do it in Windows. findstr /b /v ^# file.txt Hopefully, it makes life easier and the above commands...

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, ...

How to run a command after x minutes in Linux/Windows

In Linux command below will trigger an rm commands after 5 minutes. echo "sleep 5m && rm -f /var/www/html/products.html" | at now In Windows the command below will also trigger a copy command after 5 minutes. Start-Job -ScriptBlock { copy /html/update.html /shared/} -ArgumentList (New-TimeSpan -Minutes 5) Faith as big as a mustard seed will create a magnificient outcome in your life.