Linux Basics
Linux Basics
Instructors:
1) Dr.Sunil TT, Director , ICFOSS
2) Sri Shibu V , College of Engineering Karunagapally (Rtd)
3) Navaneeth S, Research Associate, ICFOSS
Introduction
This workshop will be conducted online. All participants will get a virtual machine installed with
Ubuntu to try out the experiments. The participants are also expected to install Ubuntu 22.04 or
later locally on their computers. The lessons/ lab plan will be available on ICFOSS moodle LMS for
later use.
The lesson plan is reorganized to suit the steep learning curve of students/ teachers. Initial lessons
will be based on an installed system. The installation of os / networking hardware will be taken up
towards the end of the workshop .
Since most of the students are new to linux, This experiment is designed to make them students
understand the organization of he file system, system architecture and how linux is organized as a
multi user system.
The following commands should be taught -: ls, cd, mkdir, pwd, rm , rmdir ,
adduser ,passwd, cpuinfo , du, top fdisk
chmod
At the completion of this experiment students should be familiar with file system organization, how
passwords and user info is maintained, inspect system specification, manipulate files and
directories and install software from repository. The instructor should take at least 2 lab sessions
with plenty of exercises the above theme. ( Excercises will be provided) Some of the commands
like wall are no longer used and can be dropped
How internet works: IP address and net mask routing , DNS system , DHCP
The following commands should be taught: ip , route traceroute nslookup,ping ifconfig( mention
about windows equivalents)
Remote access: ssh , how ssh works, how ssh can be used with keys,
Introduce open sshserver and putty ( on windows)
Experiment 3 OS Installation
It is suggested that students be introduced to installation after they are familiar with commands.
Give the virtual machines to experiment. Virtual box or proxmox environment can be setup.
(ICFOSS has a proxmox platform which can server virtual machines over internet. )
Install on virtual box and make students confident about the steps to follow.
• How to trouble shoot if they fail . Introduce tools like supergrub boot cd
Introduce how layered architecture in TCP/IP and mention about http , https
• Analyze basic protocols (HTTP, TCP/IP) Inspect specific protocol packets using wireshark
Experiment 5: Shell Programming
Commands:
• pwd - Print Working Directory
• ls - List directory contents
• cd - Change directory
The command line, or terminal, is a text-based interface used to interact with the operating system.
It provides powerful tools for navigating and managing the Linux file system. Here are some
essential commands:
•
•
• To display the entire file system tree starting from the root directory, use tree /.
• To display only the first level of subdirectories, use tree -L 1 /.
• 2. Managing Files and Directories
• Commands:
• cp - Copy files or directories
• mv - Move or rename files and directories
• rm - Remove files or directories
• mkdir - Create a directory
• rmdir - Remove an empty directory
cat
Create a file named myfile.txt in itws folder using editor.
You can view contents using cat command
rm : delete file
• /boot: This directory contains files required for booting the system. It is crucial not to
modify or delete any files in this directory, as doing so could prevent the system from
starting.
• /dev: This directory contains device files, which represent hardware devices connected to
the system. These files are often generated automatically when the system boots or when a
new device is connected. For example, when you plug in a USB drive, a device entry will
appear in /dev.
• /etc: This directory contains system-wide configuration files. The name /etc historically
meant "et cetera" as a place to put system files, but now it can be understood as "Everything
to Configure". It contains files that control system settings, such as users, passwords,
network settings, and disk mount points. It is best not to modify files in /etc unless you
fully understand the changes you are making.
• /home: This directory contains the personal directories of each user on the system. For
instance, if your username is "sunil," your personal files would be stored in /home/sunil.
• /lib: This directory contains libraries, or shared code files, used by applications. These
libraries contain code that helps applications draw windows, control hardware, and write
files. Like /bin, there are often libraries in /usr/lib, and these may be linked to each
other. The /lib directory also contains essential kernel modules.
• /media: This directory is used for automatically mounting external storage devices, such
as USB drives, when they are inserted and accessed.
• /mnt: This directory is used for manually mounting storage devices or partitions. It is less
commonly used nowadays.
• /opt: This directory is often used for software that is compiled (built from source code)
and not installed from your distribution's software repositories. Software will be located in
/opt/bin and libraries in /opt/lib. Another location for such software is
/usr/local, with /usr/local/bin and /usr/local/lib directories.
• /proc: This is a virtual directory that contains information about the system, including the
CPU and running kernel. The files and directories in /proc are generated dynamically
when the computer starts or while the system is running.
• /root: This is the home directory of the superuser (also known as the "Administrator"). It
is a separate home directory from other users. It is not meant to be modified, so keep
personal files in your own directories.
• /run: This directory is used by system processes to store temporary data. Like /boot and
/root, it's best not to touch this directory.
• /sbin: This directory contains system binaries, or executable programs, that are used by
the superuser (system administrator) for system maintenance. These commands can be used
with the sudo command, which grants temporary superuser privileges. For example, tools
to install, delete, and format software and hardware can be found here.
• /srv: This directory contains data for servers. If you are running a web server, your website
files would go in /srv/http or /srv/www. If you are running an FTP server, your files
would go into /srv/ftp.
• /sys: This is another virtual directory that contains information about devices connected to
your computer. You can sometimes manipulate devices by modifying files in /sys, but this
should only be done by advanced users. Avoid modifying this directory unless you know
what you are doing.
• /tmp: This directory contains temporary files. Unlike many other directories, users can
create, modify, and delete files in /tmp without requiring superuser privileges.
• /usr: This directory was originally where users' home directories were kept, but now it
contains a mix of directories, applications, libraries, documentation, and other shared
resources. It contains /usr/bin, /usr/sbin, and /usr/lib directories which may
mirror those found directly under the / root directory, but /usr subdirectories usually
contain user-installed applications, whereas those found directly under the root contain core
system functionality.
• /var: This directory contains variable data that changes frequently. For example, log files
are stored in the /var/log subdirectory. It also includes spools for tasks like print jobs and
email.
Many of the directories mentioned above have their own subdirectories with their
own functions and purpose. To explore them use the cd command.
Example:
# Add a new user called 'john'
sudo useradd john
2. Process Management
Commands:
• ps - View current running processes
• top - View processes in real-time
• kill - Terminate a process
• htop - Interactive process viewer (better than top)
Example:
# Show all running processes
ps aux
# Kill a process by PID (replace PID with the actual process ID)
kill 1234
# Install a package
sudo apt install nginx
# Remove a package
sudo apt remove nginx
Example:
# Show disk space usage
df -h
# List partitions
sudo fdisk -l
# Mount a filesystem
sudo mount /dev/sda1 /mnt
# Unmount a filesystem
sudo umount /mnt
5. Networking
Commands:
• ifconfig or ip - Display or configure network interfaces
• ping - Test network connectivity
• netstat - Network statistics
• ss - Socket statistics (newer tool than netstat)
Example:
# Display network interfaces
ifconfig
# Or using ip
ip addr show
Example:
# View kernel messages
dmesg | less
Example:
# Create a tar archive of a directory
tar -czvf backup.tar.gz /home/user/
• /etc/fstab
• Configuration file for mounting file systems automatically at boot.
• Specifies details like file system type, mount point, and options.
• Example:
UUID=abc123 / ext4 defaults 0 1
• /etc/hosts
• Maps hostnames to IP addresses for local name resolution.
• Example:
127.0.0.1 localhost
• /etc/resolv.conf
• Contains DNS server information for domain name resolution.
• Example:
nameserver 8.8.8.8
3. Networking Configuration
• /etc/network/interfaces (Debian-based systems)
• /etc/httpd/ or /etc/apache2/
5. Security
• /etc/sudoers
• Defines user privileges for running commands as root or other users.
• Edited using visudo to avoid syntax errors.
• Example:
user1 ALL=(ALL) NOPASSWD:ALL
• /etc/pam.d/
• Pluggable Authentication Module (PAM) configurations for authentication policies.
• /etc/selinux/
• Configuration for SELinux, a mandatory access control system.
6. System Services
• /etc/crontab
• System-wide schedule for cron jobs.
• Example:
0 5 * * * root /usr/bin/backup.sh
• /etc/rsyslog.conf
• Configuration for system logging via rsyslog.
• /etc/udev/
• Rules for dynamic device management.
7. Package Management
• /etc/apt/ (Debian-based systems)
8. User Environment
• /etc/profile
• System-wide environment variables and startup scripts for all users.
• /etc/bashrc or /etc/bash.bashrc
• Global configuration for Bash shell.
Variables
• Define variables without spaces around =:
name="Linux"
echo "Welcome to $name!"
User Input
• Read input from the user:
echo "Enter your name:"
read name
echo "Hello, $name!"
Conditional Statements
• Use if statements for decision-making:
echo "Enter a number:"
read number
if [ $number -gt 10 ]; then
echo "The number is greater than 10."
else
echo "The number is 10 or less."
fi
Loops
• Use loops to repeat tasks:
• For Loop:
for i in 1 2 3 4 5; do
echo "Number: $i"
done
• While Loop:
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
3. Working with Files
Check if a File Exists
file="test.txt"
if [ -e $file ]; then
echo "File exists."
else
echo "File does not exist."
fi
6. Example Scripts
Example 1: Backup Files
#!/bin/bash
backup_dir="backup_$(date +%Y%m%d)"
mkdir -p $backup_dir
cp *.txt $backup_dir
echo "Backup completed. Files copied to $backup_dir"
7. Best Practices
• Use Comments: Document your script for clarity.
• Error Handling: Handle errors gracefully using set-e or conditional checks.
• Permissions: Use chmod to control access to scripts.
• Modular Code: Use functions for reusable code blocks.