Skip to main content

Posts

How to install MariaDB on Amazon Linux 2

 Use the following commands to install MariaDB 10 in Amazon Linux sudo yum install mariadb105-server.x86_64 sudo s ystemctl enable mariadb sudo systemctl start mariadb sudo systemctl status mariadb
Recent posts

Java, how to create a list with a single element

 I wanted to create a Java List with a single element. Yet, I wanted to add more elements later. So, I was looking for a couple of ways to do this. So far there are multiple elegant ways to create a list with a single element with a one-liner. Not so much for a modifiable list though. Here's what I gathered so far. Followings are a few ways of creating a list with strictly a single entry. Can't add more elements. 1. Collections.singletonList() This returns an immutable list that cannot be modified or add more elements. // An immutable list containing only the specified object. List<String> oneEntryList = Collections. singletonList ( "one" ) ; oneEntryList.add( "two" ) ; // throws UnsupportedOperationException 2.  Arrays.asList() This returns a fixed-size list consisting of the number of elements provided as arguments. The number of elements provided would be an array hence the size is fixed to the length of the array. // Returns a fixed-size list List&

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.