Skip to main content

Posts

Showing posts from 2022

Telnet alternative when you can't install telnet

 I wanted to check the connectivity to an email service from a server that belongs to a client. I had restricted access to this server so I couldn't install new packages like Telnet.  If I had telnet it's much easier, but fortunately, the CURL package was already there. Curl is usually available to any user in a typical Linux system. In this case, CURL comes to your rescue. Did you know that Curl supports telnet commands as well? Curl is known as a tool to test HTTP layer functions. Whereas Telnet supports lower TCP level connections. With Curl supporting telnet, it can also be used for testing the TCP connections. eg:  curl -v telnet://www.google.com:80 -v switch is being used to spill verbose output from the request. In this example, I've used port 80, but since it is capable of using TCP, it can use any port.

Find command: How to find and delete 0 bytes files in Linux

In Linux, "find" command is a powerful command that we can use not only to find files. I've used it for different requirements, so thought of posting some of the convenient usages of find commands in a series of posts. For some application issue, I've got one of my directories in Ubuntu system swarmed with empty files with 0 bytes. Some of the files got data in them but some of them were empty. I just wanted to delete the empty files and it wasn't possible to identify them by file names or dates. So find command comes to rescue. find . -size 0 -type f  If I needed to isolate the files by file extension, we can use something like this. find . -size 0 -type f -name "*.tar" You can get the file count by piping it to wc command. find . -size 0 -type f -name "*.tar" | wc -l The awesome thing about the find command is you can use it for deleting files just by adding the -delete flag to the command. find . -size 0 -type f -name "*.tar" -dele

First few commands to run on an unknown Linux server

 If you get to troubleshoot some unknown Linux server, running these few commands will be useful. 1. Check who's logged in w This is a cool one-letter command to see who else logged into the server. It will list the usernames and even you can find the IP and the connection type. In the following example, I've logged in as the "user". If you see, some other users, watch out for unauthorized access. 2. Identify the OS There are different commands to identify what kind of Linux distribution that you are dealing with. Knowing this is crucial to decide what commands to use later. cat /etc/os-release Usually, in Linux distributions, you have a file /etc/os-release . Basically, you can see everything you need to know about the OS in this file.  3. See running processes top This basically lists the processes running with the CPU and memory consumption. With this, we can get an idea about what kind of apps running on the server and if any of them uses too much CPU or memory. 4

Hosting a single page app with Nginx without 404 errors

 If you write a single-page app where the routing is handled on its own, it works perfectly on your local host with the dev server. For an instance, if there is a route in your app for login such as "/login", if you try this in the localhost it will work without any issues.  If you host this with an Nginx server as a static web app, using the direct route in the URL will end up with 404. There is an easy fix for this updating the Nginx configurations.  Nginx default static site configuration is located in " /etc/nginx/conf.d/default.conf ". This may differ based on your installation and OS. However, the configurations are pretty much the same. You can add the following line to the configuration  try_files $uri $uri/ /index.html?q= $uri&$args ; This makes the Nginx look for files in the file path, and if found then serve it. Or else, route the request to the index.html.  In our example, it will look for a file called login in the static directory, since we don

Why is it better to use Spring Constructor injection?

This has been one of the common interview questions for senior engineers for the Java Spring framework. I was pondering this question myself for a while about the pros and cons of using the constructor injections. Spring framework provides the means for dependency injections using 3 ways using annotations (@Autowired). Constructor injection Field injection Setter injection Out of these 3, the Field injections and Setter, injections are somewhat similar, but field injections are more commonly used than the Setter injections. I believe that is due to the fact that it is more convenient to use and implementing setters are kind of automated using libraries such as Lombok. I can think of a couple of reasons to use Constructor injections over Field injections. 1. Immutability Using constructor injections, it buys us immutable objects of the dependent class. Whereas if the Fields or Setters were used, they can be modified during the runtime which would make it not exactly thread-safe. Having

Ubuntu Linux useradd vs adduser which is the easier command?

There are two main commands in Ubuntu for creating users. I've found them confusing and I always forget which to use when. So, thought to write them down clearly as a quick reference.

Note to self: Extract TLS certificate from SMTP mail server

Replace the server name and the port. Usually the port will be 25.  openssl s_client -connect <hostname>:<port> -starttls smtp eg:  openssl s_client -connect smtp.office365.com:587 -starttls smtp Copy the output section of the certificate that looks like below. -----BEGIN CERTIFICATE----- ###bunch of encoded text### -----END CERTIFICATE----- You can copy it and save it to a file with ".crt" or ".cert" extension and that's the mail server's certificate. Make sure to include the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- lines. Note: OpenSSL is required. Ubuntu:  sudo apt install openssl Other OSs:  https://wiki.openssl.org/index.php/Binaries

AWS CodeDeploy events don't start issue

Issue The code deploy events keep hanging in the pending status without starting.  Possible Reasons 1. CodeDeploy agent not installed? Make sure to install the CodeDeploy agent in the EC2 instance (If you are using EC2 instances to deploy the application) Refer to the documentation for the CodeDeploy agent installation . 2. Is the code deploy service role properly created? Make sure to check the proper permissions for the service role that you have used for the code deploy service role.  https://docs.aws.amazon.com/codedeploy/latest/userguide/getting-started-create-service-role.html 3. Make sure the IAM instance profile is properly created https://docs.aws.amazon.com/codedeploy/latest/userguide/getting-started-create-iam-instance-profile.html 4. Is the code deploy endpoint command endpoint reachable?  For each region, there is a CodeDeploy command endpoint. Make sure that you can reach the command endpoint from your EC2 instance.  eg: for us-east-1 ping codedeploy.us-east-1.amazonaws.c

Google cloud (GCP) UI is not loading issue

Issue: If you log in to the Google Cloud console from a secondary Google account (while you have logged into your primary account), it doesn't render the UI properly. Some of the styles are missing and if you look at the browser dev console, there are a bunch of errors. It is to be mentioned that I've selected the correct Google account and the project from the top nav. First I thought this is due to their service down (which would be highly unlikely), but their health page , but everything was fine. With a quick search, I realized that this is a known issue for some more users, and also there's a bug already logged for this. Even though it states fixed, a few more folks seem to have experienced this recently as per this date.  Workaround: Use an incognito window or another browser to log in to the Google Cloud console from your second Google account. When you have logged in with multiple Google accounts, even though you have selected the proper account from the navigation

How to use WSL Linux commands in Windows CMD and PowerShell

If you're a Linux user and recently move to Windows like me, must be missing the familiar Linux commands in the terminal. WSL comes to help in this case.  Assuming you have already installed WSL in Windows, you can just use the keyword wsl followed by the Linux command that you want to run.                        wsl <command>                                                         eg: If you haven't installed WSL yet, see the post below. Installing WSL Ubuntu 22.04 on Windows 11

Installing WSL Ubuntu 22.04 on Windows 11

  Installing WSL Ubuntu 22.04 on Windows 11 1. Enable Windows Subsystem for Linux and Virtual Machine platform Go to  Control Panel -> Programs -> Programs and Features -> Turn Windows features on or off 2. Switch to WSL 2 Open Powershell and type in the below command. wsl --set-default-version 2 If you don't have WSL 2, download the  latest WSL 2 package  and install it.  3. Install Ubuntu Open Microsoft Store and search for Ubuntu. Select the version you intend to install. I'd use the latest LTS version Ubuntu 22.04. Click on the Get button. It will take a couple of minutes to download and install. 4. Open up the installed Ubuntu version that was installed. If you get an error like the below image, make sure to install the  WSL2 Kernel update .  If it's an older Ubuntu version the error message would be something like the image below. Error: WSL 2 requires an update to its kernel component. Installing the  WSL2  kernel update should fix this issue. Then close the

How to login to a running Docker container as root?

How to login to a running Docker container as root? ------------------------------------------- ---- TL DR: docker exec -it --user root <container> bash ------------------------------------------- ---- Most of the times the application would run in a different user. as specified in the docker file. Such a docker file is mentioned below. FROM openjdk:17-alpine RUN apk update && apk add --no-cache gcompat && apk add curl bash RUN addgroup -S appuser && adduser -S appuser -G appuser USER appuser:appuser # some custom code to run an app In this Dockerfile the line " USER appuser:appuser" specifies the user and group which the application should run. Also, any commands following this line will run as the same user, and if you login the container without specifying the user explicitly, it will be the default user. eg: Docker exec accepts another parameter to specify the user. With that we can login as root or any user we want. 

WSL enable copy/paste shortcuts

 I moved to Windows recently, but still, I need Ubuntu installed in WSL for some of my work. You can enable Ctrl + Shift + C / Ctrl + Shift + V shortcuts for copy/paste. Right-click on the title bar and select Properties from the context menu. In the Options tab, tick the Use Ctrl+Shift+C/V as Copy/Paste option. Click OK. Now, we are good to copy/paste similar to the Ubuntu native shortcuts.

Install Docker on Windows 11 with WSL Ubuntu 22.04

This is to install Docker within Ubuntu WSL without using the Windows Docker application. Follow the below steps. Install Ubuntu 22.04 WSL 1. Enable Windows Subsystem for Linux and Virtual Machine platform Go to Control Panel -> Programs -> Programs and Features -> Turn Windows features on or off 2. Switch to WSL 2 Open Powershell and type in the below command. wsl --set-default-version 2 If you don't have WSL 2, download the latest WSL 2 package and install it.  3. Install Ubuntu Open Microsoft Store and search for Ubuntu. Select the version you intend to install. I'd use the latest LTS version Ubuntu 22.04. Click on the Get button. It will take a couple of minutes to download and install. 4. Open up the installed Ubuntu version that was installed. If you get an error like the below image, make sure to install the WSL2 Kernel update .  If it's an older Ubuntu version the error message would be something like the image below. Error: WSL 2 requires an update to its

Ubuntu DNS issue fix DNS_PROBE_FINISHED_BAD_CONFIG

Issue  I've been playing with a VPN and somehow it messed up my DNS resolution configurations. Chrome gives  DNS_PROBE_FINISHED_BAD_CONFIG  error and can't ping google. So it seemed to be an issue with the DNS. Of course, restarting didn't fix it. I tried DNS lookup which gave me below. To make sure this is somehting to do with my DNS confgis, I ran the same by providing the google DNS servers.  It works, which means my default DNS is not working for some reason. To make sure this, ran the below command. systemd-resolve --status Output has an entry for DNS Servers, which was  ::1 Fix 1. Edit the file /etc/systemd/resolved.conf. sudo vi /etc/systemd/resolved.conf 2. Add new DNS entries. I added 2 google DNS and the cloudflare DNS sever. [Resolve] DNS=8.8.8.8 8.8.4.4 1.1.1.1 3. Restart the systemd-resolved and check the configuration is persisted in /run/systemd/resolve/resolv.conf file. sudo service systemd-resolved restart cat /run/systemd/resolve/resolv.conf Same contents

Fix Error with git even after adding the public key. Git bash Permission denied (publickey).

 This could be a common issue while using git bash in windows.  Before fixing this issue, it is assumed that the public/private key pair is generated and the public key is configured in the git server (github, bitbucket or whatever). Still, if you get the "Permission denied (publickey)." which means, the ssh agent might not have recognized your key. To solve this you'll have to make sure the ssh agent is running and the key is added. Run the ssh agent, execute the below command in git bash exec ssh-agent bash To check if the key is added run the below command. ssh-add -l If there is no key, it will print "The agent has no identities." To add the key, run the below command with the path to your private key. ssh-add ~/.ssh/thilinarsa (My key file is located in C:/User/Thilina/.ssh/thilinarsa) If you run the ssh-add -l command again you should see the key file added. Run the git clone or pull command again and see if it works!

Facebook videos are too loud

Not sure if it is only me, but as of this writing, I find some of the Facebook videos are too loud compared with other websites or players on the machine. Whenever I play a video, I usually turn the volume down. Then whenever there's an ad, it screams in my ears. Same when I play the next video, the former adjusted volume level doesn't stick. This could be a bug in FB, so I even suggested a feature improvement.  As a solution, I found this chrome plugin. Sound and video fixes for Facebook Don't know who developed it, but the dude has fixed one of Facebook's problems, and many thanks to the developer. I might as well help him out as it is an open-source project. No more screaming videos! 

A Silly New Year Wish with a Java code

TJ's silly new year wish Happy New Year y'all!!! package com.tjisblogging ;   import java.time.LocalDate ; import java.time.Month ; import java.util.Vector ;   public class NewYear {   private static final LocalDate END_OF_2022 = LocalDate . of ( 2022 , Month. DECEMBER , 31 ) ;   public static void main ( String [ ] args ) { Life yourLife = Life. getInstance ( ) ; while ( ! LocalDate. now ( ) . equals ( END_OF_2022 ) ) { yourLife. push ( "Love" ) ; yourLife. push ( "Joy" ) ; yourLife. push ( "Happiness" ) ; yourLife. push ( "Prosperity" ) ; yourLife. push ( "Money" ) ; yourLife. push ( "Health" ) ; } } }   class Life { private Vector < String > lifeQueue = new Vector <> ( ) ; private static Life instance = new Life ( ) ;   private Life ( ) { }