3.2.3.8 Lab – Basic Linux Commands (Answers)

3.2.3.8 Lab – Basic Linux Commands (Instructor Version)

3.2.3.8 Lab - Basic Linux Commands (Answers) 4

Objectives

Part 1: Accessing the Shell Interface using PL-App

  • Access the Official Course Materials from PL-App
  • Connect to the Linux Shell using PL-App

Part 2: Explore the Shell Interface and Commands

  • Discover basic file and directory commands
  • Getting help
  • Monitoring system processes
  • Managing processes
  • Determine basic network information
  • Setting file permissions
  • Display memory, disk usage information and system shutdown
  • Perform basic package management

Background

Linux is an open source operating system that runs on various devices from thermostats and smartphones to big data server farms. Numerous integrated libraries are available that provide a very stable, fast, secure, scalable, and customizable environment.

A Linux distribution is an operating system built around the Linux kernel. The main difference between different distributions is the directory structure, commands available, and the package manager. The package manager is used to install new software from the distribution repositories. The Raspberry Pi board usually runs a Linux distribution called Raspbian as the operating system. Raspbian is based on a very popular Debian Linux distribution.

As with any other operating system, Raspbian allows you to control the board via a Shell Command Line Interface (CLI) using standard Linux commands. Although this sounds intimidating, especially today when the Graphical User Interface (GUI) makes everything intuitive and extremely easy, the command line still remains a very powerful tool for advanced operations. This lab will allow you to experiment with some of the basic CLI commands available on most Linux distributions.

Required Resources

  • PC with Internet Access
  • Raspberry Pi with power cable and either a wired or wireless network connection
  • Raspberry Pi that is configured and imaged for PL-App access

Part 1: Accessing the Shell Interface using PL-App

Step 1: Connect to the Linux Shell using PL-App.

a. Click the New button in PL-App and from the menu select Terminal:

3.2.3.8 Lab - Basic Linux Commands (Answers) 5

b. A new window will open with the Linux shell running on the Raspberry Pi board. It is necessary to have both this notebook and the Linux Terminal open together in separate windows to complete this lab.

Part 2: Explore the Shell Interface

Step 1: Discover basic file and directory commands

1. Enter the pwd command followed by the Enter key to display the current directory.

2. Type cd /home/pi to change to the pi directory.

3. Enter the pwd command followed by the Enter key to display the current directory.

4. To return back to the home directory type cd ~.

5. Enter the pwd command followed by the Enter key to display the current directory.

root@chestnut:/home/pi/notebooks# pwd
/home/pi/notebooks
root@chestnut:/home/pi/notebooks# cd /home/pi
root@chestnut:/home/pi# pwd
/home/pi
root@chestnut:/home/pi# cd ~
root@chestnut:~# pwd
/root
root@chestnut:~#

Note: The ~ represents the path to a user’s home directory. Each Linux user has its own home directory.

6. Type the ls command to list the contents of the current directory.

7. Type the ls -l command.

8. Type the ls -la command.

root@chestnut:~# ls
root@chestnut:~# ls -l
total 0
root@chestnut:~# ls -la
total 4684
drwx------ 13 root root    4096 Dec 1 09:20 .
drwxr-xr-x 21 root root    4096 Jul 20 00:57 ..
-rw------- 1 root root     2868 Jul 28 21:38 .bash_history
-rw-r--r-- 1 root root      565 Jul 20 02:00 .bashrc
drwxr-xr-x 4 root root     4096 Nov 30 14:40 .cache
drwxr-xr-x 4 root root     4096 Mar 4 2016 .config
drwxr-xr-x 5 root root     4096 Mar 4 2016 .ipython
drwx------ 3 root root     4096 Dec 4 22:17 .jupyter
drwx------ 3 root root     4096 Mar 3 2016 .local
-rw-r--r-- 1 root root      140 Nov 19 2007 .profile
drwx------ 2 root root     4096 Jul 20 21:40 .ssh
root@chestnut:~#

Note: You can either type ls -l -a or you can join the options together ls –la. The Options are explained below:

-l – display a detailed version of the output

-a – display all files and directories, including hidden files. In Linux, hidden files start with a period.

Use nano to create a new file for use with upcoming tasks.

root@chestnut:~# nano myPythonScript.py

Once in the editor type the lines shown in the window. After the last line use the key combination Control-X. The editor will prompt for confirmation to save the file, answer by pressing y and accept the proposed file name of ‘myPythonScript.py’ by pressing ENTER.

3.2.3.8 Lab - Basic Linux Commands (Answers) 6

11. Use the ls command to verify the file was created.

root@chestnut:~# ls
myPythonScript.py

12. Use the cat command to display the contents of the newly created file.

root@chestnut:~# cat myPythonScript.py
#!/usr/bin/env python3

a = 1
b = 2
if (a < b):
        print ("a")
else:
        print ("b")
print ("bye bye")

1. Use the ls -l iot.txt command to check for a file named iot.txt.

root@chestnut:~# ls -l iot.txt
ls: cannot access ioe.txt: No such file or directory

2. Use the touch command to create the iot.txt file by typing touch iot.txt.

root@chestnut:~# touch iot.txt

3. Use the ls –l iot.txt command to display a file listing.

root@chestnut:~# ls -l iot.txt
-rw-r--r-- 1 root root 0 May 13 14:15 iot.txt

4. Use the head command to display the first few lines of a file.

5. Use the tail command to display the last few lines of a file.

root@chestnut:~# head -3 myPythonScript.py
#!/usr/bin/python

a = 1
root@chestnut:~# tail -3 myPythonScript.py
        print "b"
print "bye bye"

Note: The numeric options of head and tail define the number of lines from a file to display.

1. Use the mkdir. command to create a new directory namedmyFolder`.

root@chestnut:~# mkdir myFolder

2. List the contents of the directory to verify that the new directory has been created using the ls command.

root@chestnut:~# ls
iot.txt     myFolder     myPythonScript.py

3. Use the cp command to create a backup of myPythonScript.py named

 backupPythonScript.py.

4. Use the ls command to verify the file was created.

root@chestnut:~# cp myPythonScript.py backupPythonScript.py
root@chestnut:~# ls
backupPythonScript.py iot.txt myFolder myPythonScript.py

5. Use the mv command to move the backupPythonScript.py to the myFolder directory.

Use the ls command to verify the file was created.

root@chestnut:~# mv backupPythonScript.py myFolder/
root@chestnut:~# ls myFolder
backupPythonScript.py

Step 2: Getting help

Issue the command man ls to display the help manual for the Linux command ls. You can navigate the output line by line by pressing the enter key, page by page by pressing the spacebar key, and using the PageUp and PageDown keys. To quit, press the q key.

Use the more and less commands to control output scrolling. Note: The less command works like the more command, but the user can use PageUp and PageDown to navigate through the file. The output from the man command uses less to display the information making it much easier to navigate the manual pages.

Issue the command less /etc/passwd to display the password file. Press ‘q’ when finished.

root@chestnut:~# less /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
pi:x:1000:1000:,,,:/home/pi:/bin/bash

Step 3: Monitoring system processes.

Use the ps command to display basic process information for the current user.

root@chestnut:~# ps
PID TTY          TIME CMD
7195 pts/0    00:00:00 bash
7223 pts/0    00:00:00 ps

Use the ps aux command to display information for all running processes.

root@chestnut:~# ps aux                                              
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND      
root         1  0.0  0.3   2148  1360 ?        Ss   13:57   0:02 init [        
root         2  0.0  0.0      0     0 ?        S    13:57   0:00 [kthre        
root         3  0.0  0.0      0     0 ?        S    13:57   0:00 [ksoft        
root         5  0.0  0.0      0     0 ?        S<   13:57   0:00 [kwork        
root         6  0.1  0.0      0     0 ?        S    13:57   0:44 [kwork        
root         7  0.0  0.0      0     0 ?        S    13:57   0:10 [rcu_p        
root         8  0.0  0.0      0     0 ?        S    13:57   0:00 [rcu_s        
root         9  0.0  0.0      0     0 ?        S    13:57   0:00 [rcu_b        
root        10  0.0  0.0      0     0 ?        S<   13:57   0:00 [khelp        
root        11  0.0  0.0      0     0 ?        S    13:57   0:00 [kdevt        
root        12  0.0  0.0      0     0 ?        S<   13:57   0:00 [netns        
root        13  0.0  0.0      0     0 ?        S<   13:57   0:00 [perf]        
root        14  0.0  0.0      0     0 ?        S    13:57   0:00 [khung        
root        15  0.0  0.0      0     0 ?        S<   13:57   0:00 [write

Use the top command to display which processes are using system resources.

top - 20:34:35 up  6:36,  0 users,  load average: 0.29, 0.25, 0.32     
Tasks:  72 total,   2 running,  70 sleeping,   0 stopped,   0 zombie  
%Cpu(s): 19.9 us,  6.8 sy,  0.0 ni, 73.0 id,  0.0 wa,  0.0 hi,  0.3 si,
KiB Mem:    445804 total,   149900 used,   295904 free,    53924 buffer
KiB Swap:   102396 total,        0 used,   102396 free,    41940 cached

PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAN   
2551 pi        20   0 98.4m  14m 6820 R  25.0  3.3 124:50.45 wyliodri 
7342 pi        20   0  3140 1988 1672 R   1.0  0.4   0:00.48 top      
6 root      20   0     0    0    0 S   0.3  0.0   0:45.06 kworker/ 
2715 root      20   0     0    0    0 S   0.3  0.0   0:44.91 kworker/ 
1 root      20   0  2148 1360 1256 S   0.0  0.3   0:02.59 init     
2 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kthreadd 
3 root      20   0     0    0    0 S   0.0  0.0   0:01.00 ksoftirq 
5 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kworker/ 
7 root      20   0     0    0    0 S   0.0  0.0   0:10.59 rcu_pree 
8 root      20   0     0    0    0 S   0.0  0.0   0:00.00 rcu_sche 
9 root      20   0     0    0    0 S   0.0  0.0   0:00.00 rcu_bh   
10 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 khelper  
11 root      20   0     0    0    0 S   0.0  0.0   0:00.01 kdevtmpf

Note: To exit the top command, press the q key on the keyboard.

Step 4: Managing processes.

The following sequence will create a process that runs in the background and sleeps for 500 seconds. Once the process is started the process ID can be determined and then terminated.

  1. Issue the command sleep 500 & to start a process in the background.
  2. Issue the ps command to determine the process ID, in the example the sleep process is 8178.
  3. Issue the command kill 8178 to terminate the process associated with the previous sleep command.
root@chestnut:~# sleep 500 &                                         
[1] 8178                                                               
root@chestnut:~# ps                                                  
PID TTY          TIME CMD                                            
7195 pts/0    00:00:01 bash                                           
8178 pts/0    00:00:00 sleep                                          
8182 pts/0    00:00:00 ps                                             
root@chestnut:~# kill 8178
[1]+  Terminated              sleep 500

Note: The kill command expects a Process ID (PID) as a parameter. The PID can be found in the first column of the output from the ps command. If the process does not die, you can try to kill it with the option kill -9 command. In order to kill a process you must be the owner of the process or have elevated privileges.

Step 5: Determine basic network information.

1. Issue the ifconfig eth0 command to display basic networking information.

root@chestnut:~# ifconfig eth0                                       
eth0:  flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500                  
      ether b8:27:eb:ae:ce:5e  txqueuelen 1000  (Ethernet)       
      RX packets 0  bytes 0 (0.0 B)                              
      RX errors 0  dropped 0  overruns 0  frame 0                
      TX packets 0  bytes 0 (0.0 B)                              
      TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Note: The eth0 interface currently does not have an IP address if you are connected wirelessly. You can issue the ifconfig wlan0 command to display networking information about the wireless interface.

2. Issue the ip route to display additional network information.

root@chestnut:~# ip route                                            
default via 10.100.80.1 dev wlan0                                    
10.100.80.0/24 dev wlan0  proto kernel  scope link  src 10.100.80.132

Step 6: Setting file permissions.

This section focuses on file permissions and the effect they have on the ability to execute the instructions in a file.

1. Display the current file attributes for the myPythonScript.py created previously by issuing the command ls –l myPythonScript.py.

root@chestnut:~# ls -l myPythonScript.py                          
-rw-r--r-- 1 root root 88 May 13 14:08 myPythonScript.py

Note: The file permissions are broken up into four distinct sections. The first section is the ‘-‘ at the beginning of the line indicating whether this is a normal file, a directory or a linked file. The ‘-‘ indicates this is a normal file. The next three sections consist of three specific characters indicating the permissions. The three sections refer to the owner, group, and other users. This file has read and write permissions for the owner (rw-), read permissions for the group (r–) and read permissions for other users (r–). 

2. Attempt to execute the file by typing in the name of the file. It is necessary to precede the name of the file with ‘./’ so the system will know where the file is. Issue the command ./myPythonScript.py.

root@chestnut:~# ./myPythonScript.py
bash: ./myPythonScript.py: Permission denied

Note: In order for the file to run the execute bit must be set. At least the owner section of the permissions should indicate read/write/execute permissions (rwx) as opposed to (rw). Be sure to use the exact file name as well. Uppercase and lower case characters in a file name are significant in a Linux environment. 

3. Use the command chmod to add the executable flag to the myPythonScript.py file. Enter the command chmod +x myPythonScript.py.

root@chestnut:~# chmod +x myPythonScript.py
root@chestnut:~# ls -l myPythonScript.py
-rwxr-xr-x 1 root root 88 May 13 14:08 myPythonScript.py

Note: Notice the permissions now read (rwx) for the owner, (r-x) for group, and (r-x) for other users. The file now has a different color as well indicating it is now executable. 

4. Execute the file now by issuing the command ./myPythonScript.py.

root@chestnut:~# ./myPythonScript.py
a
bye bye

Step 7: Display memory, disk usage information and system shutdown.

1. Use the free command to check memory usage.

root@chestnut:~# free                          
       total       used       free     shared    buffers     cached
Mem:      445804     234824     210980          0      97948      71208
Swap:     102396          0     102396

2. Use the –h option along with the free to display the memory in “human readable” format listing values with Mbytes instead of Bytes only. Enter the command free –h.

root@chestnut:~# free -h                         
       total       used       free     shared    buffers     cached
Mem:        435M       229M       205M         0B        95M        69M
Swap:        99M         0B        99M

3. Use the df and df –h commands to check the amount of free disk space on the file systems.

root@chestnut:~# df                         
Filesystem                     1K-blocks    Used Available Use% Mounted on
/dev/root                        7065848 2748116   3982176  41% /
devtmpfs                          443788       0    443788   0% /dev
tmpfs                             448396       0    448396   0% /dev/shm
tmpfs                             448396    7412    440984   2% /run
tmpfs                               5120       4      5116   1% /run/lock
tmpfs                             448396       0    448396   0% /sys/fs/cgroup
/dev/mmcblk0p1                    510984  139580    371404  28% /boot
posix-overlay(/boot/notebooks)    510984  139580    371404  28% /home/pi/notebooks
root@chestnut:~# df -h                                          
Filesystem                      Size  Used Avail Use% Mounted on
/dev/root                       6.8G  2.7G  3.8G  41% /
devtmpfs                        434M     0  434M   0% /dev
tmpfs                           438M     0  438M   0% /dev/shm
tmpfs                           438M  7.3M  431M   2% /run
tmpfs                           5.0M  4.0K  5.0M   1% /run/lock
tmpfs                           438M     0  438M   0% /sys/fs/cgroup
/dev/mmcblk0p1                  500M  137M  363M  28% /boot
posix-overlay(/boot/notebooks)  500M  137M  363M  28% /home/pi/notebooks

4. The shutdown command is used to turn the Pi off. The command must be run with elevated privileges. Enter the command sudo shutdown –h now to shut down the system immediately.

root@chestnut:~# sudo shutdown –h now

5. If the previous command was executed it will be necessary to restart the Pi by unplugging the power and plugging it back in. Reconnect to the PL-App when the Pi comes back online.

Step 8: Perform basic package management.

1. Use the apt-get command to retrieve, install and manage packages on the Raspberry Pi.

root@chestnut:~# sudo apt-get install mc                     
Reading package lists... Done                                  
Building dependency tree                                       
Reading state information... Done                              
Suggested packages:                                            
zip links w3m lynx arj dbview odt2txt gv catdvi              
djvulibre-bin imagemagick python-boto python-tz              
The following NEW packages will be installed:                  
mc                                                           
0 upgraded, 1 newly installed, 0 to remove and 100 not upgraded.                                                              
Need to get 405 kB of archives.                                
After this operation, 1160 kB of additional disk space will be 
used.                                                          
Get:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main 
mc armhf 3:4.8.3-10 [405 kB]                                   
Fetched 405 kB in 1s (239 kB/s)                                
Selecting previously unselected package mc.                    
(Reading database ... 95174 files and directories currently ins
talled.)                                                       
Unpacking mc (from .../mc_3%3a4.8.3-10_armhf.deb) ...          
Processing triggers for menu ...                               
Processing triggers for desktop-file-utils ...                 
Setting up mc (3:4.8.3-10) ...                                 
Processing triggers for menu ...

Note: The Raspbian Linux distribution used on the Raspberry Pi is Debian based. You can use apt-get to install any package that is in the official repositories. In the example above, Midnight Commander (CLI graphical file manager) was downloaded from a repository and installed using apt-get.

Reflection

1. Based on the commands presented, what command could be used to get further information about the use of the built in manual?

2. The +x option was used earlier in the lab with the chmod command. How would you remove the executable permissions from the file? (hint: check the man pages)

 

 

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x