An eternal truth seeker in the quest of clarity. I am a keen tech-enthusiast with deep interests in cloud computing, data-science, machine learning and information security. Currently, I 'm simulataneously learning and applying Python and cloud concepts, and learning about information security and Linux to gain the mastery in a Linux System Administration. Also, I 'm a fresh graduate of the AWS re/Start. As a learner from the very first cohort of the AWS Restart Program from Nepal, not only did I got the opportunity to learn about the foundations of cloud computing but also the practical real-world implementation of cloud computing through the combination of myriad services and features I was lucky to get acquainted with.
Bootstrap your instance with some automation scripts and instantly access the live website from your browser.
In this brief article, I ‘ll walk you through the fully-automated process of installing the dependencies, fetching the artifact and deploying it on an EC2 instance ( AWS lingo for a virtual server ) in its entirety.
Let’s get going, shall we ?
First of all, let’s proceed to launch an EC2 instance like this:
Here, I ‘ve labeled my EC2 instance as webserver and additionally tagged it as a server launched in development environment.
Next up, let’s select the AMI for our EC2 instance. Here, I’ve chosen the Ubuntu Server 22.04 LTS as my base image for the web server I ‘m about to launch.
You are free to choose any AMI from the available AMIs but I ‘ll proceed with Ubuntu Server 22.04 for this demonstration.
This refers to the resource specifications for the server that includes pre-configured packages with a certain processing and computational capability. I’ve chosen the following:
Additionally, to ensure a secure ssh login to the remote EC2 instance from my local machine, I ‘ve also set up a key-pair. However, I’ll not open any ssh connections here.
Create your own.
In this section, you’ll have to configure the firewall to protect your EC2 instance that helps to filter the incoming requests destined to your EC2 instance and outgoing requests originating from your EC2 instance.
Here, I ‘ve pre-configured my security group as crispy_kitchen-web-dev-sg that allows incoming traffic at port 80 and port 22 from my own IP only. You can configure it as per your wish but make sure that the port 80 is kept open for the incoming traffic as it is the default port for incoming web traffic.
Now leave the rest of the settings as-it-is and then head-over to the Advanced Settings’ user data section and add the following bootstrap scripts to the user data field:
#!/bin/bash
sudo apt update -y
sudo apt install wget unzip apache2 -y
wget https://www.tooplate.com/zip-templates/2129_crispy_kitchen.zip && unzip 2129_crispy_kitchen.zip
cp -r 2129_crispy_kitchen/* /var/www/html/
sudo systemctl restart apache2
Then, launch the EC2 instance.
Wait until the server becomes live and then copy its public IP address and paste in any browser of your choice.
The website is live.
Congratulations! You’ve successfully automated the deployment of a website within a matter of a minute.
The sixth one in #TheLinuxSeries (06)
The root-level access is needed to perform administrative tasks on a Linux Machine and there are a few ways you can do that.
The Linux system has two types of users.
The normal user belongs to the first category. This allows him/her to perform only the limited set of actions as stated under their permission scope at the time of their account creation.
They are barred from performing tasks such as adding and /or deleting a user and/or a group and updating the system, to name just a few administrative tasks, with these normal permissions.
So, who gets to perform the above alike actions ?
Well, the answer is pretty simple: the root user.
Can a normal user become a root-user then ?
Well, yes they can if there’s an entry in the sudoers file to grant the user with such magical permissions. The scope of sudoers file is beyond the scope of this article so let’s get back to question that got you here; how to gain the root-level privilege as a normal user ?
In order to perform the administrative tasks, the root-level permission is required. So, how do we gain a root-level access ? There are two ways you can accomplish that:
But first of all, I would like to address the latter. That is, gaining a direct root-level access.
Provided that you’ve the necessary and sufficient permissions in the sudoers file, you can gain the root-level access by using the following command.
sudo su
Here, sudo and su are the abbreviations meaning super-user-do and switch-user respectively.
That is, you’d like to switch the user from a normal mode to a privileged mode.
After you’ve entered the above command, you’ll be swiftly prompted to enter “your” password. This will assign you with the root-privilege necessary to perform the administrative tasks with a fresh shell starting at the exact same location that you’re at the moment.
However, if you were to append the above command with a whitespace and a hyphen like this
sudo su -
your shell would start at the home directory of the root user located within /root directory.
You can check this by printing your working directory.
Now you can perform n-number of tasks that necessitate root-level privilege. Let me demonstrate one for you.
Since a root-user can perform any and all kinds of actions on a machine, it’s also called a superuser. But is it only after switching to root that we gain full-fledged root-level access ? Isn’t there any alternative to this approach ? Do think about it.
Well, before you start to mull over it, go on and exit the privileged mode either by entering the exit command or pressing Ctrl + D on the keyboard.
Well, probably you’ve guessed it right. We can indeed gain a temporary root-level access and there is no need to switch the user to the privileged mode either.
Prefix the command you want to run with sudo. Enter your password and voila, you can perform the action right away without a fuss.
This is also the preferred approach to perform any administrative task. It follows the principle of “Using it when you need it” 😃.
After you temporarily login to the shell using the above command, your root credentials will be saved for 5 minutes.
If you want to refresh and renew the temporary permission you can do the following:
sudo -v
You can easily revoke the permission at any instant by replacing this flag with the one included in the following command.
sudo -k
Direct access to root is disabled in distributions like Ubuntu so you’ll get an error if you want to have a shot at it with
su root
With this, I would like to bring this brief article on #TheLinuxSeries to an end.
This is the fifth article on #TheLinuxSeries
One of the important tasks of a Linux administrator is to keep a proper track of the command history that logs in all the commands entered by the users of the system.
By default, the history command would provide a list of entries in the history buffer ordered in a way that the commands were entered by the user with the latest commands appearing at the bottom.
Now, this would merely list the position number of the command and the command that was run.
But looking at this alone might be insufficient if we want to zero in on the exact time and date of the command execution.
In this article, we are going to learn what and how of doing it.
Configuring this environment variable, we can get a timestamp of when the command was exactly executed.
To do so, set the environment variable to the following value.
HISTTIMEFORMAT="%d%m%y %T"
where,
%d => day
%m => month
%y => year
%T => time
The selection of the date format is totally personal and you can choose your own date format too.
Now, let’s see what we get after managing this configuration and running a history command thereafter.
Well, there you have it. Through such a simple configuration now you are able to track the commands on the machine by looking at the precise date and time of command execution .
That’s it for this time.
The fourth in #TheLinuxSeries (04)
By default, when you launch the bash shell, it opens up the shell in the home directory of the logged in user. All the commands that we enter there gets subsequently stored in the .bash_history file.
Now in order to review your command history on the bash shell, you can do the following:-
cat .bash_history
As we go further and run commands on the bash shell, the command would get automatically appended to the new line on the .bash_history file. The environment variable called HISTFILESIZE, meaning history file size, dictates the numbers of entries made to the .bash_history file. It is limited to 2000 entries by default on my Ubuntu 22.04.2 LTS.
You can verify this for your machine by typing in the following command in the bash shell.
echo $HISTFILESIZE
The history command lists the entire command stored in the history file buffer of the bash shell. It prints out the command line number to the left, indicating the order in which the commands were executed, with the corresponding command name to the right.
The history file stores these entries in a buffer. The entries made on the bash shell would only be written onto the .bash_history file after the user logs out of the shell.
The environment variable called HISTSIZE provides the size of the history buffer. This can be reviewed from the following command.
echo $HISTSIZE
The result would be the following.
That is, a total of only 1000 commands from the .bash_history file will be stored in the memory and thus becomes available to the history command.
We can use the double exclamation (!!) to run the recently run command once again.
To use the particular command entered previously, run the history command to list all the historical commands, then type the command line number towards the left preceded by an exclamation mark. This will execute the command linked to that number.
!<command line-number>
I listed my command history at first and then ran the command on line 1889 to re-execute the command once again.
You can provide the name of the command itself to execute the last instance of such a command.
!<command-name>
But isn’t is wise to check the command first and foremost and then run it thereafter. Yes, that is also possible. All that you need to do is append a colon and a lower-case p to the above command as: —
!<Command-name>:p
And at the end…
Now, here is a bonus tip for you. The bash has a recall mode to reverse search all the commands that you ‘ve entered. In order to do so, you can search through the .bash_history file using the Ctrl+ R key and then search for the appropriate command you would like to run by typing in the command. To select the command, press enter and to exit the reverse search simply press Ctrl + G.
This brings the fourth article in #TheLinuxSeries to an end.
The third one in #TheLinuxSeries (03)
In this brief article, I‘m going to walk you through the types of Linux commands available for us to use.
Broadly speaking, there are two types of Linux commands that we can execute from the shell.
Now, let’s understand what these actually mean.
These are the commands that comes bundled with the shell, the program that checks for syntactical correctness of the user-typed commands and takes it to the kernel for execution, provided that the commands are syntactically sound.
The common commands used to change directory ( aliased as cd ), print working directory (aliased as pwd ), echo , et cetera are some of the examples of shell built-in commands.
We can check the type of a command doing the following :-
type <command-name>
Additionally, there is no dedicated man page for the shell built-ins. Rather, we need to use the help command and pass the name of the command as an argument to see through the documentation for the command.
The other type of the Linux command are the executable files that can be invoked as commands through the specific alias that contains the path to these executables.
The common commands such as ifconfig, rm and rmdir fall under this category.
And, of course these commands do have dedicated man pages available to document their concept and usage.
The above command opens up the documentation for rm command through the less pager as shown below.
That’s all we are covering today. Thanks for staying till the end.
The second one in #TheLinuxSeries (02)
In this article, we ‘ll learn to create and edit files in Linux through bash shell. If your interested about Linux, the first article of the series is the perfect place for you to start.
Creating Files in Linux
There are various ways to create file on Linux. You can create one through the bash shell by simply using the touch command followed by the file name you want to use.
touch <file_name.txt>
As you can see in the screenshot following this statement, I have created an empty file name file1.txt in practicetoday directory.
Creating Files through a Text-editor
You can also create files through the use of the default text-editors that comes up with your Linux machine. I already have nano and vim pre-installed in my Linux machine.
First of all, let’s create a file using the above-mentioned text-editors in Linux.
All you need to do is replace the touch command with nano or the vim command in the previous command entered in the bash shell.
#using nano to create a file
nano <file_name.extension>
#or you can use vim instead of file1.txt
vim <file_name.extension>
Did anything else happen when you used either of the above command? You might have a text-editor opened in-front of you. Well, here comes the advantage of using the text-editor to create a file. This way we do not merely create a file but also get to perform write operation in the file too, provided that we have the necessary and sufficient privilege to do so.
You can write anything you wish to inside the file that you just created and save the changes using Ctrl + O as the keyboard shortcut and exit from the editor using Ctrl + X command.
Or
If you used vim command instead of nano you ‘ll get to see the vim editor opened in your machine just as shown in the screenshot provided below.
Unlike nano text-editor, the Vim editor comes up with dual modes. One is the control mode, where you can issue commands to perform file operations on the file you just created, and the other is the insert mode, which enables you to write on the file that you just created.
Remember to change the modes in vim to perform the desired actions with it. You ‘ll need to enter the i keystroke, to enter the insert mode in vim-editor and to be able to write anything on it. After you are done writing on the file, you’ll again need to enter the control mode using the escape(Esc) keystroke.
To quit the vim-editor without saving any changes to the file, press colon (:) and then enter q.
However, if you want to save changes to the file and then quit, write wq after the colon and hit enter. You ‘ll land back to your bash shell.
Editing Files
Just as you created the file using the text-editor, enter the same command again to re-open the file in that text-editor.
There you can specify the changes you want to make in the file and exit the file after saving recent changes to the file performing the text-editor specific actions.
At the End
As an addendum to this article, I ‘ll now tell you how we can remove a file through the Linux bash shell
Use rm command and then issue the file name to delete the specific file.
rm <file_name.extension>
As you can see, we have successfully deleted the file we had created at the start of the article.
With this, I wrap up the second article on the Linux series. Thank you for staying till the end.
The first in #TheLinuxSeries ( 01 )
Previously, we took a generic approach to learn about some of the basic Linux commands 🔗. But we did not really looked into the specifics, and most important of all, the use cases of those Linux commands.
Now, through a structured approach, we will learn when, where and how ( I like to call it the W2H 😉 approach) to apply the Linux commands and put them to specific uses.
So let’s get the show on the road, shall we ?
Directory Management
First of all, let’s learn to manage our directories(also known as folders) in Linux.
To do so, we need to learn how to create directories and perform several actions in and around them.
Creating Directory and/or Directories
We use the mkdir command to create a directory in Linux.
Passing the name of the directory we wish to create following the mkdir command, we can create a directory.
In the Linux bash shell, the convention to issue commands is an argument preceded by a command. In this context it is the name of the directory-to-be-created that gets passed as an argument preceded by the command, which in this case is mkdir.
mkdir <directory_name>
Here, I have created a repository named MyPlaylist using the mkdir command.
We can also provide a path to a specific location as an argument and then create a directory therein. Let’s see how is that possible.
mkdir /path/<directory_to_create>
Currently, I am inside the home directory. You can type the pwd command to check your location.
Now, let me create a directory right from this location. The directory that gets created, however, will reside in a different location.
As you can see just above this statement, I was able to create a new directory named Favourites inside the MyPlaylist directory without navigating to that location at all.
This is not the end of it. The real magic of Linux comes now. We are able to create multiple directories at a go. All that we need to do is pass the name of the directories we want to create as arguments to the mkdir command like this:-
mkdir <directory1> <directory2> <directory3>
Removing Directory and/or Directories
Now in order to remove an empty directory we can type the following command in the bash shell:-
rmdir <directory_to_be_removed>
Of course, you can bundle other empty directories and also successfully remove them through a single command line if you want to.
rmdir <directory1> <directory2> <directory3>
You can see the result of such a command execution right below this line.
However, any attempt to remove a non-empty directory would prompt you with such a message.
But what if we want to do away with the directory any way ❓ Well , you don’t need to take the trouble of deleting the contents within a directory to achieve this. Rather you can do the following:-
rm -r <non_empty_directory>
As you can see below, we managed to successfully remove a non-empty directory by chaining an additional option r after the mkdir command.
When you chain this option after the mkdir command, it commands the system to recursively search through the directory and remove whatever that resides inside of it one after another, and of course, ultimately, remove the directory in the process as well.
At the End
Now you can take this wisdom 😉 of directory management with you and do many or most of the tasks related to the same in Linux. With this, I would like to bring this brief article on managing directories in Linux to a conclusion.
Thank you so much for staying till the end. 👍
In this brief article, I will thoroughly guide you through the entire process of connecting to a MySQL database using a popular source-code editor called Visual Studio Code.
Now to get started, first of all, you will need to download MySQL server onto your machine and make sure that the application successfully installs on your machine. To do so, we need to visit the official MySQL website,and therefrom, download MySQL and MySQL server file for the OS(Windows, MacOS or Linux) that is currently running on your machine.
For the sake of brevity, I will be demonstrating the installation process of MySQL server on a Linux OS system in this demonstration.
Steps to Install MySQL server
Step 1: Head over to the Terminal window and check for the updates in the existing package list using the command sudo apt update.
Since you are requesting the root privilege to initiate the command, you will be prompted to enter the root user password.
Step 2: Installation of the MySQL server
Since I already have an installation beforehand, the Terminal would print such a message. The same will install MySQL server onto your machine.
Creating user and granting privileges
Here comes the stage where we create a user and grant them with specific privileges. But before we do that let us check whether the MySQL installation was successful or not. If the installation succeeds, then type mysql — version to check the MySQL version that you just installed onto your machine.
If you get the same and/or similar result to the one shown above, you are all good to go to the next step. If not, wait till the installation successfully completes on your machine. You will need to duly reinstall MySQL if there is any problem associated with the installation process.
Creating user
First of all, we need to login as a root user. In order to do so, head over to the command line or the Terminal and type the following command :-
sudo mysql -u root -p <your_root_user_password>
Here, mysql command is chained with the username and password of the user passing the -u and -p options respectively. This prompts you to enter the root password to complete the command execution .
This opens up the MySQL prompt in the bash shell. Now you can run you SQL queries from here. In order to create a user using the following command:-
(Information alert 🔔 : The code block in Medium gets formatted to such quotation symbol ▶️ ‘ ’ by default. If you copy and paste the code within the code block and run it, this might throw you a syntax error. So make sure that you properly format the SQL command and make changes to the quotation symbols if it appears otherwise than that which is provided in the screenshot just below the code block.)
CREATE USER ‘<your_user_name>’@’localhost' IDENTIFIED WITH mysql_native_password BY ‘<root_user_password>';
I have used password a the user password in this demonstration. Make sure to create your own user password and thereby secure the login credentials.
Granting Access through MySQL privilege
Now that we have a user, we also need to configure the access of the recently created user through MySQL privileges. Here, we will grant the administrative privilege to the user using one of the Data Control Language(DCL) commands. Do not grant unnecessary access to users when you are configuring grant policies for users, allow the least privilege they need.
GRANT ALL PRIVILEGES ON *.* TO ‘<your_user_name>’@’localhost';
Now let’s review the grant privileges provided to the user through the following command:-
SHOW GRANTS FOR <your_user_name>@localhost;
As you can see, there are almost every privilege to this user for we have granted an administrative privilege to it.
Finally, in order to reload all the privileges once again, we need to enter the following command:-
FLUSH PRIVILEGES;
Creating a database
Now that we have created a user with login credentials and granted the required and necessary access to it, let us create a database that we can connect to using the following SQL command:-
CREATE DATABASE <DATABASE_NAME>;
Finally, exit the MySQL prompt typing EXIT; command on the Terminal.
Installing Extensions on Visual Studio Code(hereinafter referred as VS Code)
Launch your VS Code and open up VS Code extensions. Once there, search for SQL SERVER(mssql) extension added by Microsoft itself. Install this extension on your VS Code.
Then after, you will need to install the SQLTools extension.
Now keep you eyes on the left pane. Select the SQLTools Extension icon appearing at the tail-end in the screenshot below.
Adding the Database Connection
Leveraging the above extensions, now we will connect to the database that we just created.
Select the last icon displayed on the left pane
Click on MySQL. This will pop-up a form as illustrated below:
Carefully fill-out all the fields using the recently created credentials and then test the connection.
VS Code will display an alert message asking you whether to allow the SQLTools extensions when the user makes an attempt to login using theSQLTools Credentials . Enter Allow here.
Next, you ‘ll be prompted to enter the password of the recently created user.
After you provide the user password, you will get a message at the bottom on the saying Successfully Connected. That means we have now successfully connected the user to the new database connection called NewConnection that we configured previously.
Now, click on SAVE CONNECTION. This lands you to the following view.
Click on CONNECT NOW and you’ll login into a session.
Congratulations! You have successfully made a connection to your database using the user credentials you ‘ve just created.
Before making our first git commit let us look at Git itself.
Brief Introduction of Git
Let’s assume that you are a developer working remotely in a XYZ Company. The company teams you up with fellow developers who are also working remotely on a project. Now, how are you going to keep track of other’s progress or the changes they have made on the file that you created. This dilemma persistently aches your fellow developers as well. Well that is where git comes in.
Git is a very popular version control utility. It facilitates collaborative changes by providing the details on what I call the 5Ws. That is, who did what, where, why and when. And this is made possible by maintaining the history of content changes made over time by an individual member or members of a team working in tandem on a project.
GitHub is one such hosting service that uses Git for software development and version control.
Now lets take these concepts forward and see to it how we can actually implement them.
You Very First Git Commit
A GitHub account is a prerequisite to initiate this process. So, create one for yourself here. Also, I haven’t yet told you to download Git. So, download it from here.
Now kindly go through the following steps.
Step 1:- To ensure that git successfully installed on your system, head to the command line or terminal and do the following:
If this doesn’t show up, you don’t have your git properly installed. Make sure your installation completes. Then come back here to check the Git installation.
Step 2:- Create a directory/folder that will include that files and folders you want to commit to a GitHub repository. (repository is a GitHub jargon for a directory)
You can follow GUI approach to perform the above-mentioned task. I have used the command line approach here through Linux bash shell. If you want to try out these commands, you can read my article on the basic Linux commands.
Step 3:- Initialize Git in your directory that houses all the files and folders you intend to push to your GitHub repository. Enter git init in the terminal window as illustrated below.
Step 4:- Next, configure your GitHub username and email through git config — global user.name “<Your user name>” and git config — global user.email “<Your email address>”
Step 5:- Check the Git status using git status command.
Step 6:- Since we have not yet added the files to there are no commits to be made. So, lets add these files first.
You can add individual files at a time or add everything at a go. When we work with a lot a files, it is completely inefficient to add one file at a time so here we will add all the files at a go using git add . command.
Check the Git status again. Do you spot any changes ?
The files are now added to the staging area which readies the files to be committed onto a repository. You can understand this concept by looking at the next illustration.
Step 7:- Now commit the files using git commit -m “message for you to reference”. I have done the following.
Check for the git status again. This is what you will get.
You can also check the history of changes to the repository using git log command.
Step 8:- Now head over to your GitHub account and create a repository. You can launch the new repository using several available options. I will use clickable + icon at the top right corner of the navigation pane to create one for this demonstration.
Click on the plus sign and you will see the option to create a new repository.
Then click on New. You will be redirected to the following view.
Here, insert the name of the repository you want to create. You can add description to the repository for comprehension when you visit this repository later on but it is optional and you can leave it as it is.
Step 9:- Leave the rest of the options as it is and create a repository.
Step 10:- Next, you will land to your repository page. Scroll down a bit and you should see something like this.
Copy this highlighted section and paste in on to the terminal like this.
Step 11:- Next enter git push -u origin master on the terminal.
The result would look like this:-
For Linux user, this might not suffice so in such a situation a token needs to be generated. This token needs to be configured as your GitHub password rather than your default password.
You can do this by navigating to Settings > Developer Settings > Personal Access Tokens.
You can generate your token from there.
After that head over to the terminal window and do the following:-
Then, repeat the step 10 and 11. You are all good now.
Head back to the browser window and refresh the page view of your recently created repository. And yes you did it.
Thanks for reading.
Learn how to enable and disable termination protection before and after launching an EC2 instance
Before Launching the EC2 instance
At the time of configuring your EC2 instance prior to a launch, you will reach at the Advanced details pane after you have configured your storage for the instance-to-be launched.
When you expand the Advanced details pane and scroll a bit, you will get to see field called Termination protection.
Here, you can choose the Enable option provided in the drop-down.
After Launching an EC2 instance
First of all head-over to the EC2 dashboard and there, on the left-hand side, click on Instances under Instances. You will see your EC2 instances and their configurations in the form of a record.
Next, select the desired instance and then click on Actions. From all the available options, click on Instance settings.
This gives you further options available under Instance settings to select from. Select Change termination protection.
Now a window pops-up before you. As you can see I had enabled termination protection at the time of launching my EC2 instance.
If I attempt to terminate this instance, i will be prompted with an alert
So, how can I disable the termination protection then ? Well, head over to the previous change termination protection window and uncheck the Termination protection. Then, save the changes.
Now, when I try to terminate my instance, this happens.
As you can see we have successfully terminated the instance.
Termination protection: the why of it
This self-explaining term, termination protection, prevents anyone from terminating the particular instance with termination protection enabled.
There might be a situation where you can mistakenly terminate a instance that needs to be live at all times. Or, someone else might terminate the instance if they have the privileges needed to do so.
Thus, in order to safeguard the instance from any unintended shutdown it is better to enable termination protection. Of course, we can easily make changes and terminate an instance navigating to the change termination protection pane when we don’t need it.