0% found this document useful (0 votes)
12 views

Tutorial 16 Basic Linux Commands

Uploaded by

fernandostcampos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Tutorial 16 Basic Linux Commands

Uploaded by

fernandostcampos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Computer Systems

Tutorial 16
Basic Linux Commands
This tutorial takes you through the process of installing and using VirtualBox to create and use
a Debian Linux distribution. In addition, the tutorial takes you through the process of executing
basic tasks on it using Linux commands.
We will use a Debian Linux distro during this. Debian is one of the most popular and powerful
GNU distros which is considered an excellent choice for server environments. Debian has also
been used as the base for many specialist distributions such as Ubuntu, Xandros, Knoppix.

I. Downloading VirtualBox and Debian Linux


To install the VirtualBox and Debian, perform the following steps:
1. Download and install the latest 64-bit version of Oracle VirtualBox, this was 7.0.12 at the time
of writing. After completing the installation, you should have the following screen.

2. Download the 64-bit version of Debian Linux by clicking on the amd64 link that can be found
under the Small CDs or USB sticks section of the webpage. The file was called debian-12.2.0-
amd64-netinst.iso at the time of writing and is around 628MB.

Note: You can similarly download other distributions of Linux and install them.
Week 8
Computer Systems

II. Installing Debian Linux


Once you have downloaded the above files, you are ready to install Debian Linux distro. Open
the VirtualBox to install and create a new Debian virtual machine by following the steps below.
1. Click on the New button or the New command in the Machine menu as shown in the
figure to create a new virtual machine.

2. Firstly, you need to configure the virtual machine by identifying its name, OS type and
version, and the memory and hard disk sizes allocated to it. Thus, name the new virtual
machine as Debian and change the type to Linux and the version to Debian (64bit) and
click Next as shown in the figure.

3. Proceed with the default values for the Base Memory of 2048 MB and click Next. Then,
proceed with the default values to “Create a Virtual Hard Disk Now” with a Disk Size
of 20.00 GB and click Next and then Finish as shown in the figures.

Week 8
Computer Systems

4. Now, you have completed the configuration of the virtual machine and hence the virtual
machine name Debian appears on the left side part of the VirtualBox as shown in the figure.

5. Secondly, you need to install the Debian Linux and configure Linux distro itself. Thus, click
the Start button. As you have not yet installed Debian, the VirtualBox responded by
clarifying that “The virtual machine failed to boot” and asking for the install DVD for the
Debian operating system. So, you need to use the image file (debian-12.2.0-amd64-
netinst.iso) that you have downloaded in Section 1 as shown in the figure.

6. Once the server has been booted, you will be asked to choose the installation option. Choose
the Graphical install option and press enter as shown in the figure.

Week 8
Computer Systems

7. You might have already noticed that the size of the Debian virtual machine is too small which
does not enable you to read its contents and follow up the installation. You can increase the
window size of the virtual machine by selecting the command Scaled Mode from the View
menu and click on Switch to confirm as shown in the figure.

8. Once you have selected the type of installation, to complete it you need to perform the
following actions:
1) Choose the English language from the list provided and then click on Continue.
2) When asked for the selected location, choose the United Kingdom as this will be used for
the time zone. Click Continue for the next step.
3) For the keyboard layout choose the British English option (American English if your
keyboard is compatible with US English) and click Continue.
4) Set the hostname of the server to a name of your preference. In this example, the
hostname debian will be used. Then click on Continue to proceed to the next step.
Week 8
Computer Systems

5) Set the domain name could be set to .local, then just click Continue.
6) Now you will be prompted to set the root password. Type in a password that you will
remember and the retype it to confirm the password and click Continue. [It is
recommended to use a simple password such as 123 for the purpose of the study to avoid
forgetting it later].
7) Next, you need to type in a Full name of a user account for the non-administrative user
account. [It is recommended to type user1 to have the same exact screenshots available in this
document and because you will create other users in the next tutorial. Otherwise, you can use a name
of your preference that is easy to remember] and click Continue to proceed.
8) Now, you are prompted to select a username for the new user account (recommended to
leave the default username user1) and click Continue to proceed.
9) Then, you will be asked to set the password for the new user account. Type in a password
that you will remember and then retype it to confirm the password. [It is recommended
to use a simple password such as 123 for the purpose of the study to avoid forgetting it
later].
10) For the Partition disks, choose the default option Guided – use entire disk option and
click Continue.
11) Next, select the default option SCSI1 and click Continue. Then, select the default option
All files in one partition and proceed to the next step by clicking Continue again. Then,
select the default option Finish partitioning and write changes to disk and complete
the partitioning process by clicking on the Continue button. Finally, confirm the changes
by selecting the Yes option that will allow the system to write those changes to the disks.
To move to the next step, click on Continue.
12) When you are asked whether you wish to scan extra installation media, select the default
option No and click Continue.
13) To configure the package manager, select the default option United Kingdom that will
find a mirror of the Debian archive on the respective network and hit Continue. Then,
choose the mirror ftp.uk.debian.org from the list and click Continue.
14) For the HTTP proxy, leave it blank and click Continue to proceed.
15) Finally, answer No to the question “Participate in the package usage survey?” and
click Continue.
16) In this step, you are required to choose the installation of additional software packages.
To install some useful software, leave the default option as they are and then click the
Continue button.
17) Furthermore, you need to install the GRUB boot loader by choosing Yes and then clicking
Continue to proceed. Finally, select the option /dev/sda from the list and click Continue
for the completion of the installation.
Debian Linux distro with a graphical environment is now installed and you can shut the system
down by closing the window and selecting the option Power off the machine and click OK.
Later, and you can boot up the virtual machine anytime using the start button on the VirtualBox.

III. Test Your Knowledge


Complete the following tasks to install a second Debian Linux distro. To complete them, you need
to do some research and refer to manual pages online.
Note: You can install any other Linux distro such as Ubuntu or a light Linux distro such as Alpine.

Week 8
Computer Systems

1. You have installed a Debian Server with GUI (Debian Desktop). You need to install a Debian
Server without GUI (Debian Server with Command Prompt) which you can name it as
Debian CP (we will use it in the next tutorials).
2. Repeat the commands that you have completed in the previous sections on the new Debian
CP server.

IV. Working with Linux Commands


In this section, you can use either Debian or Debian CP virtual machines which you have created
in the previous sections to execute the Linux commands. For demonstration, the screenshots in
this tutorial execute the Linux commands on the virtual machine Debian CP.

Note: For the Debian virtual machine, you need to log into the machine, then click on
Activities tab in the top left and type Terminal in the search box in the top middle.
Once you find it, open it to enable the writing of the Linux commands.

1. Working with Directories & Files


This section examines the filesystem, looking at how files are organised in directories. All Linux
distributions have a very similar directory structure. Unlike Windows systems, there are no
drive letters, all physical storage devices are mounted on subdirectories of /, called root.
When you login, your current working directory is your home directory. Your home directory
has the same name as your username, and it is where your personal files and subdirectories are
saved. The ls (list) command lists the contents of the current working directory. To find out
what is in your home directory, type the following command:
ls
ls does not list the files beginning with a dot (.) which are hidden files and usually contain
important program configuration information. To list all files in your home directory including
those whose names begin with a dot, you need to use the command option -a (options change
the behaviour of a command) by typing the following command:
ls -a
You need to create a subdirectory in your home directory to hold the files you will be creating
and using. The command mkdir (make directory) creates a new directory. To make a
subdirectory called My_Docs in your current working directory, type the following command:
mkdir My_Docs
The command cd (change directory) changes the current working directory to a specific
directory position in the file-system tree. To change to the directory you have just created, type
the following command:
cd My_Docs
The command pwd (print working directory) displays the full pathname of the current
directory. To display your current directory, type the following command:
pwd
The following figure shows the response of running all the previous command.

Week 8
Computer Systems

Note: There are some special directories such as single dot (.), double dots (..) and tilde
(~). While (.) refers to the current directory and (..) refers to the parent directory
respectively, (~) refers to the home directory. So, for example, the command cd .. takes
you one directory up the hierarchy.
Now, let us start by navigating to the root (/) directory and exploring the top-level directories.
The cd (change directory) command takes a single argument, the directory you want to
navigate to root (/) directory, this can be done by typing cd /. The ls (list) command lists all the
files and directories in the current working directory.

Let us take a look at what is stored in some of the important directories:


• /bin contains the binaries (executables) for the essential system programs
• /boot is where the Linux kernel and boot loader files are stored.
• /dev contains the devices available on the system
• /etc is where the system configuration files are stored.
• /home contains the home directories for the system users
• /lib contains shared libraries
• /media this is where removable media devices are mounted
• /sbin contains special binaries needed by the system administrator.
• /tmp used for temporary files
• /var contains the files that change such as log files and printer spools

Use the cd command to navigate to the /bin directory. Use the ls command to list the contents.
The command cp (copy) makes a copy of file1 which is located at the pathname of the first
argument into file2 which is located at the pathname of the second argument. To make 2 copies
of the file hostname which is stored at the directory /etc into the recently created directory
My_Docs and the current working directory, type the following commands:
cd ~
cp /etc/hostname ~/My_Docs
cp /etc/hostname .
Week 8
Computer Systems

The command mv (move) file1 which is located at the pathname of the first argument into file2
which is located at the pathname of the second argument. The command is used to either moves
a file from a directory to another or rename a file if both files are in the same directory. To move
the file hostname into the /tmp directory and then rename it as new, type the following
command:
mv hostname /tmp/hostname
mv /tmp/hostname /tmp/new

The command rm (remove) file1 which is located at the pathname of the first argument while
the command rmdir (remove directory) is used to remove a directory which should be empty.
To remove the file from the /tmp directory and remove the directory My_Docs, type the
following command:
rm /tmp/new
rm ~/My_Docs/*
rmdir ~/My_Docs

Week 8
Computer Systems
2. File Permissions
Access control on a Linux system mirrors that of a UNIX system with each file and directory
assigned access rights for:
1. The owner of the file/directory
2. The group to whom the file/directory belongs
3. Everybody else
For each of these, there are three possible permissions:
1. Read
2. Write
3. Execute
If you combine these, you get 9 flags that can be set. if you view a directory using the long
listing format flag, you can see this information. For example, using the following commands,
you can get the list of files that are in the /boot directory along with their permissions. Here,
we only show some file with different permissions.
cd boot
ls -l

Reading left to right for the file on the top:


1. The first character represents the type, this is either a file (-), a directory (d) or a
symbolic link (l).
2. The next three characters are the owner permissions, read (r), write (w) and execute
(x). In this example, the user owner of the first file has all three permissions (rwx).
3. The next three characters are the group permissions that follow the same format as the
owner permissions. In this example, the group of the first file has read and execute
permissions (r-x).
4. The last three characters in the block represent the permissions for all other users. In
this example, all the other users have no permissions at all (---).
5. Next the username of the owner and group are shown. In this example, because the
owner and group are root, the file is fully owned by the root account.
6. The number to the right is the file size in bytes. By using the -h flag, these will be
displayed in a human readable format such as KB and MB.
7. After this, there is the last modified date and time with the file/directory/symlink
name at the end.
Week 8
Computer Systems
a. Changing Permissions
The owner of the file can change the permissions of any directory/file/symlink they own
using the chmod command. Create an empty file myfile1.txt in the home directory (~) by
typing the following commands:
cd ~
touch myfile1.txt
Display the default permissions of the file using the ls command as follows
ls -l

To change the default permissions, there are two ways to use chmod command. The first is to
assign the permissions as binary octet for a class:
1. The first digit describes the owner permissions (convert to binary!) as 111 which
assigns rwx
2. The second digit describes the group permissions as 101 or r-x
3. The third digit describes the permissions for all other users, in this case 000 or --- The
alternative is to describe the change you want to make.
chmod 750 myfile1.txt

The second is to use symbols for a class and the assigned permission as follows:
1. The first character is the class (u=use/owner, g=group, o=others, a=all)
2. The second character is the operator (+ means add and - means remove)
3. The third character is the mode affected (r=read, w=write, x=execute)
chmod a+x myfile1.txt

Note: This is just an example a file. Create another file and change its permissions as you
wish.

3. Edit, Display & Search the Contents of a File


The vi editor is the most popular built-in classic text editor in the Linux family. The vi editor
has two modes: command and insert. When you first open a file with vi, you are in Command
mode. command mode means that you can use keyboard keys to navigate, delete, copy, paste,
and do several other tasks except entering text. To enter insert mode, press i. In insert mode,
you can type a text, use the Enter key to go to a new line, use the arrow keys to navigate text.
To return to command mode, press the Esc key once. To save and exit the vi editor, you have

Week 8
Computer Systems
to type :wq while to exit the vi editor without saving the changes, you have to type :q!. To create
a new file myfile.txt and edit its content as shown in the figure bellow, type the following
command:
vi myfile2.txt

The command cat can be used to display the contents of a file on the screen. To display the
contents of myfile2.txt file, type the following command (see figure below)
cat myfile2.txt

The command grep is used to search a given file for lines containing a match to the given strings
or words. To search for the lines that contains the word first in the file myfile2.txt, type the
following command (see figure below)
grep first myfile2.txt

4. Pipes
You may have noticed that there are a lot of files in
the /bin directory, including many of the most
common commands you will be using. What if you
want to search for a particular file?
It is the time to learn about Linux pipes. A pipe
(indicated by the | character) allows Linux to send
the output of one command to another command
for processing. For example, when you run the ls
command, its output is sent to the screen stdout
(which is the default).
So, if you want to display the files that only contain
“ls” (letter “l” followed by letter “s”). We can pipe
the output of ls command to list only the programs
that includes “ls” in their names. Typing the
command ls | grep ls in the /bin directory, you
should get the following outcome (or similar).

Week 8

You might also like