Getting Started with Ubuntu and Bash Shell

Introduction

Ubuntu is free, open-source Operating System which you can install on your laptop, or desktop. It comes in desktop and server versions. Ubuntu is easy to install, simple to use, safe, secure, fast and massively powerful.

Ubuntu is a distribution of Linux. The Linux kernel is at the core of the operating system and it does all the essential low level stuff.

Other distributions of Linux you may heard of are Redhat, Suse, CentOS, Andriod etc.

Ubuntu is based on Debian (another Linux distro) and Ubuntu itself have some other flavors e.g. Kubuntu and Linux Mint e.g.

Why Ubuntu

  • Security.
  • Customizable
  • Easy to administer, e.g. run jobs, disk management etc.
  • Open Source / Free.
  • Can run efficiently on cheap hardware.

Installing Ubuntu

Installing Ubuntu is very easy and you can download the software from official website on this link. On the official website you can find detailed information about installation process. You can install it on your laptop and Ubuntu-desktop comes with a nice UI.

Ubuntu-server also have the same core, but it is much lean and does not comes with a UI and ideal for running background processes e.g. running a web-server, database server etc. I will be using this for the examples.

The other route you can take is to use cloud services. I created an ubuntu-server virtual machine on Azure and following screenshot shows part of that step:

Once the VM is setup and running. I opened a git-bash terminal on my windows machine (I had git instaslled on my windows machine so I can use its bash shell and my windows machine act as a client and can access the ubuntu-server on azure cloud) and following picture shows that I am connected to ubuntu VM:

Now you can see the prompt has changed to reflect my user-name and computer name: ~ is a special character telling that I am at home directory and at this point, I can start writing bash commands, which we will see next.

Using the Command Line

In the rest of the article, I will focus on the command line. Though Ubuntu desktop is very interesting and you can explore it easily by using it.

Command line is a different way of working with your computer instead of graphical representation like windows, icons etc.

You can access the command line by running the terminal app. Bash is the default terminal in Ubuntu.

UserName and HosName

whoami //outputs the username
hostname //outputs the computer name

Working With Files/Folder

>>ls command lists the files in current directory.

>>ls -l command outputs the long list of directory contents:

This output tell me each entry whether it is file or directory (d for directory). What different users are allowed to do with it. who owns it, size and when it was last changed.

using ls we can list the contents of different directory as shown below:

we can also filter entries using wild-cards etc as shown below:

if we want more details about a particular command, we can see its manual by entering >>man ls:

Press q to exit from manual.

We can create a file by using touch command:

Getting and Applying Updates

sudo apt-get update && sudo apt-get upgrade

This command will update your operating system.

Files and Users

Here is the output of ls -l command for my home directory. We will focus on the 2nd entry which is for a file (notice missing d in first column).

The first column is telling us some information about the file. Each character tells us a different thing about the file:

  • – tell us that it is a file so its blank, hence a dash (-).
  • the next three (rw-) are the permissions for the owner of the file (Read, Write, Execute). In this case owner has read and write permission but no execute permissions (hence the dash).
  • the next three (rw-) are group permissions for which file belongs to.
  • the last set of three charaters (r–) are permissions for any other users (people who are not the owner and not in the group).
  • 1 shown above, is the number of links to this file.
  • (first) jawadhasan80 is the owner of the file.
  • (second) jawadhasan80 is the group.
  • next is the size of the file, last modified date and name.

In Linux, everything is a file. Even your computer memory and network connections are files and every file has that set of possible permissions.

User, Permissions and Sudo

The first entry tells me that root is the owner of the var directory. And only root has write permission to this directory. Terminal shows a different user is connected (jawadhasan80). if I try to create a file inside var directory, it will not allow me:

There is no root user in ubuntu and we can use sudo (which is a command to run as a super user) which will effectively elevate my user account to have root privileges’ for a short time. So if I execute following command, file will be created and no error.

if we list var directory, we can see owner of the new file is root:

we can write to the file using echo command but we have to use sudo again or I can switch from my user to root user for the session by using su command:

You can see, the prompt is now changed to root user.

we can output the file contents using cat command as shown below:

You can also use tail (which shows the last few lines) or less (which uses pages to show file contents).

Extending Access to Files

chmod (change-mode) command can be used for his purpose:

With this command I am adding the write permissions for all users for the file. (a means all users and +w means add write permissions):

Now, I can write to this file without sudo first:

we can make the file read-only in similar fashion (a-w):

Change the owner of the file

chown command can be used for this purpose:

Now, I can change (u+w) the write permissions for myself by using chmod command as shown below:

Summary

Getting started with Ubuntu is easy and in this post, we took some small steps towards this learning journey. Let me know of your comments and few next posts we will continue our journey from this point onward. Till next time, Happy Learning.

My Recent Books