Skip to main content

Posts

Showing posts with the label Linux for noob

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

How to uncomment a conf file in Linux via ansible

How to uncomment or enable a setting in a config file in Linux using Ansible? Ansible is quite a good tool,  as it helps to make a lot of changes on a few servers with a minimal effort. Of course, the challenge is you need to know what you want. And also create an Ansible yaml file that will accomplish the task. Example YAML file for Ansible below is able to make some changes on the Linux logrotate.conf file. On Linux logrotate.conf, it has some text like the string below. Note this is just an example and you can tweak it to other settings as desired. # uncomment this if you want your log files compressed #compress The logrotate.conf file, the compress setting is commented. To enable the setting the # or the comment key must be removed. How to remove the comment in order to enable the setting? Well, if there's only a single server do it the simple way. Login to the server and edit the file using vim or nano or other text editor. However, if there are quite a few servers. Ansible ...

Ping sweep with Bash script in Linux

Ping is a good tool to easily check or troubleshoot a system whether it is online or not. Provided the system or device is allowed to reply to any ICMP request. A simple Bash script below shows how to ping a Class C subnet. Replace the IP Address range below if you want to use it depending on the IP Address set on your network. It also illustrates how to make use of while loop, increment a variable counter, and use a variable to substitute as part of the whole command in Bash.  #====================== #!/bin/sh i=0 while [ $i -ne 255 ]   # initialize the counter don't exit until 255 counter is reached do         i=$(($i+1))   #increment the variable counter by adding 1 to it         echo "$i"     # just show the counter as it is incremented         ping -c2 192.168.0.$i   # ping the IP Address twice $i will be substituted with ...

How to use regex in Linux Bash

Regex or regular expression uses a pattern that is compared to a string and will check if the pattern exists or the string matches the pattern. In Bash script a simple if then else and a regex pattern can be used to check if the pattern matches a set of input on a text file. Bash script below is a simple example on how to use regex in Bash using if then else. Script below uses this sample file "number_file.txt" with the sample text or contents shown below. a12567 b23567 c34568 12w678 23908 35107 23469 x1234 y3432 12094x 123y8 ==== #!/bin/sh patx="^[0-9]+$" while IFS= read -r line; do     echo "Text read from file: $line"     if [[ $line =~ $patx   ]]                then                  echo "Hey this one is alpahumeric --> $line."      else ...

Linux get IP and Gateway and DNS subnet from CLI

In windows getting gateway, IP, subnet and DNS is quite straight forward using ipconfig command via command line.  ipconfig /all or ipconfig -all will show the settings of the IP Address, DNS IP, Subnet mask and Gateway. In Linux world, well it can also be done but need to remember a few command line to get all these settings. Here's how it's done from the command line or terminal. hostname -I;netstat -rn;cat /etc/resolv.conf So, it's hostname -I <--capital letter i and netstat -rn plus the resolv.conf file where the DNS IP Address is set. The ";" semi-colon is to execute the command line one after the other. hostname -I <-- will show the IP Address netstat -rn <-- will show the gateway and the subnet mask (need to install net tools if command is not found) /etc/resolv.conf <-- where the DNS IP Address is set It's quite daunting if you're a newbee, but hey it is just a command line it doesn't bite it just shows good information to know abo...

No bootable medium found error VirtualBox

Migrating from physical to virtual or P2V is a common thing to do. In this virtualization era. If hardware or a server is showing some issues, like rebooting by itself when no changes are made and the server is at the age of a primary 3 kid. Then this could be that the server is showing some signs that it needs to be retired. P2V is a good option if an existing powerful server that is not fully utilized and is able to handle another load. Migrating from OpenStack or exporting OpenStack VM to a volume so it can be used outside of OpenStack is another practice also. But sometimes things don’t go smoothly and few bumps or issues here and there may arise at times. For example doing P2V in VirtualBox for a Linux system shows an error, “FATAL: No bootable medium found! System halted.” And you let the VM run for hours hoping that it will just boot by itself but it won’t just budge the error persist no matter how long you wait. P2V is migrating or cloning a physical server to a virtu...