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

01-introduction-to-linux-system-os-2024

The document provides an introduction to GNU/Linux, covering key concepts such as files, applications, commands, and system calls. It explains the structure of the Linux filesystem, user permissions, and basic command-line navigation. Additionally, it discusses the shell, process management, and mounting partitions, along with examples of useful commands and configuration files.

Uploaded by

ndipteofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

01-introduction-to-linux-system-os-2024

The document provides an introduction to GNU/Linux, covering key concepts such as files, applications, commands, and system calls. It explains the structure of the Linux filesystem, user permissions, and basic command-line navigation. Additionally, it discusses the shell, process management, and mounting partitions, along with examples of useful commands and configuration files.

Uploaded by

ndipteofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Introduction to Applications, Files,

Linux commands
and
Basics of System Calls

Abhijit A. M.
[email protected]
Why GNU/Linux ?
Few Key Concepts
What is a file?
File is a “dumb” sequence of bytes lying on the hard
disk (or SSD or CD or PD, etc)
It has a name, owner, size, etc.
Does not do anything on its own ! Just stays there !
Few Key Concepts
What is an application?
Application is a program that runs in an
“environment” created by the operating system,
under the control of the operating system
Hence also called “User Applications”
Words: Program, Application, Code.
Code: Any piece of complete/incomplete
apps
programming language
Programs
Program: Any piece of “complete” code (OS,
Code
device drivers, applications, ...)
Few Key Concepts
Files don't open themselves
Always some application/program open()s a file.
Files don't display themselves
A file is displayed by the program which opens it.
Each program has it's own way of handling files
It’s possible NOT TO HAVE an application to
display/show a file
Few Key Concepts
Programs don't run themselves
You click on a program, or run a command -->
equivalent to request to Operating System to run it.
The OS runs your program
Users (humans) request OS to run programs, using
Graphical or Command line interface
and programs open files
Path names
Tree like directory structure
Root directory called /
Programs need to identify files using path names
A absolute/complete path name for a file.
/home/student/a.c
Relative path names, . and .. notation
concept: every running program has a current
working directory
. current directory
A command
Name of an executable file
For example: 'ls' is actually “/bin/ls”
Command takes arguments
E.g. ls /tmp/
Command takes options
E.g. ls -a
A command
Command can take both arguments and options
E.g. ls -a /tmp/
Options and arguments are basically argv[] of the
main() of that program
int main(int argc, char *argv[]) {
int i;
for(i = 0; i < argc; i++)
Printf(“%s\n”, argv[i]);
}
$ ./a.out hi hello 123 /a/b/c.c ./m/a.c
Basic Navigation Commands
pwd
ls

ls -l Map these commands to


ls -l /tmp/ navigation using a
ls -l /home/student/Desktop graphical file browser
ls -l ./Desktop
ls -a
\ls -F
cd

cd /tmp/
cd
cd /home/student/Desktop
notation: ~
cd ~
Before the command line, the concept
of Shell and System calls
System Call
Applications often need to tasks involving hardware
Reading input, printing on screen, reading from
network, etc.
They are not permitted to do it directly and
compelled to do it using functionality given by OS
How is this done? We’ll learn in later few lectures.
This functionality is called “system calls”
Before the command line, the concept
of Shell and System calls
System Call
A function from OS code
Does specific operations with hardware (e.g. reading
from keyboard, writing to screen, opening a file from
disk, etc.)
Applications can't access hardware directly, they
have to request the OS using system calls
Examples
open(“/x/y”, ..)
The Shell
Shell = Cover
Covers some of the
Operating System's
“System Calls” (mainly
fork+exec) for the
Applications
Talks with Users and
Applications and does
some talk with OS
Not a very accurate diagram !
The Shell
Shell waits for user's input

Requests the OS to run a program which the user has


asked to run

Again waits for user's input

GUI is a Shell !
Let's Understand fork() and exec()
#include <unistd.h> #include <unistd.h>
int main() { int main() {
fork(); printf("hi\n");
printf("hi\n"); execl("/bin/ls", "ls",
NULL);
return 0;
printf("bye\n");
}
return 0;
}
A simple shell
#include <stdio.h>
#include <unistd.h>
int main() {
char string[128];
int pid;
while(1) {
printf("prompt>");
scanf("%s", string);
pid = fork();
if(pid == 0) {
execl(string, string, NULL);
} else {
wait(0);
}
}
}
Users on Linux
Root and others
root
superuser, can do (almost) everything
Uid = 0
Other users
Uid != 0
UID, GID understood by kernel
Groups
Set of users is a group
File Permissions on Linux
3 sets of 3 permission
Octal notation: Read = 4, Write = 2, Execute = 1
644 means
Read-Write for owner, Read for Group, Read for
others
chmod command, used to change permissions, uses
these notations
It calls the chmod() system call
Permissions are for processes started by the user,
File Permissions on Linux

-rw-r--r-- 1 abhijit abhijit 1183744 May 16 12:48 01_linux_basics.ppt


-rw-r--r-- 1 abhijit abhijit 341736 May 17 10:39 Debian Family Tree.svg
drwxr-xr-x 2 abhijit abhijit 4096 May 17 11:16 fork-exec
-rw-r--r-- 1 abhijit abhijit 7831341 May 11 12:13 foss.odp

Owner size name


3 sets of 3 permissions

3 sets = user (owner), group, last-modification


others
hard link count
3 permissions = read, write,
execute
File Permissions on Linux
r on a file : can read the file
open(.... O_RDONLY) works
w on a file: can modify the file
open(.... O_WRONLY) works
x on a file: can ask the os to run the file as an
executable program
exec(...) works
r on a directory: can do 'ls'
w on a directory: can add/remove files from that
Access rights examples
-rw-r--r--
Readable and writable for file owner (actually a process started by the
owner!), only readable for others
-rw-r-----
Readable and writable for file owner, only readable for users belonging
to the file group.
drwx------
Directory only accessible by its owner
-------r-x
File executable by others but neither by your friends nor by yourself. Nice
protections for a trap...

http//free-
Permissions: more !
Setuid/setgid bit
$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 68208 Nov 29 17:23 /usr/bin/passwd
How to set the s bit?
chmod u+s <filename>
What does this mean?
Any user can run this process, but the process itself runs as if run by the
the owner of the file
passwd runs as if run by “root” even if you run it
Man Pages (self study)

Manpage 4 Device drivers and


$ man ls network protocols
/dev/tty
$ man 2 mkdir
$ man man
 5 Standard file formats
/etc/hosts
$ man -k mkdir
 6 Games and demos
Manpage sections /usr/games/fortune
1 User-level cmds and  7 Misc. files and docs
apps
man 7 locale
/bin/mkdir
 8 System admin. Cmds
 2 System calls
/sbin/reboot
int mkdir(const char *, …);
 3 Library calls
int printf(const char *, …);

http://www.cs.ucr.edu/~weesan/cs183/
GNU / Linux filesystem structure
Not imposed by the system. Can vary from one system to the other,
even between two GNU/Linux installations!

/ Root directory
/bin/ Basic, essential system commands
/boot/ Kernel images, initrd and configuration
files
/dev/ Files representing devices
/dev/hda: first IDE hard disk
/etc/ System and application configuration files
/home/ User directories
/lib/ Basic system shared libraries

http//free-
GNU / Linux filesystem structure (self study)
/lost+found Corrupt files the system tried to recover
/media Mount points for removable media:
/media/usbdisk,
/media/cdrom
/mnt/ Mount points for temporarily mounted
filesystems
/opt/ Specific tools installed by the sysadmin
/usr/local/ often used
instead
/proc/ Access to system information
/proc/cpuinfo,
/proc/version ...
/root/ root user home directory
/sbin/ Administrator-only commands
/sys/ System and device controls
(cpu frequency, device
power, etc.) http//free-
GNU / Linux filesystem structure (self
study)
/tmp/ Temporary files
/usr/ Regular user tools (not essential to the
system)
/usr/bin/, /usr/lib/,
/usr/sbin...
/usr/local/ Specific software installed by the sysadmin
(often preferred to /opt/)
/var/ Data used by the system or system servers
/var/log/, /var/spool/mail
(incoming mail), /var/spool/lpd
(print jobs)...

http//free-
Files: cut, copy, paste, remove,
(self study)
cat <filenames> mv <source> <target>
mv a.c b.c
cat /etc/passwd
mv a.c /tmp/
cat fork.c
mv a.c /tmp/b.c
cat <filename1> rm <filename>
<filename2> rm a.c
cp <source> <target> rm a.c b.c c.c
cp a.c b.c rm -r /tmp/a
mkdir
cp a.c /tmp/
mkdir /tmp/a /tmp/b
cp a.c /tmp/b.c
rmdir
Useful Commands
(self study)
echo grep

echo hi grep bash /etc/passwd


grep -i display /etc/passwd
echo hi there
egrep -i 'a|b' /etc/passwd
echo “hi there”
less <filename>
j=5; echo $j head <filename>
sort head -5 <filename>
sort tail -10 <filename>

sort < /etc/passwd


firefox
Useful Commands
alias (self study)
alias ll='ls -l'
strings
tar
strings a.out
tar cvf folder.tar folder
adduser
gzip
sudo adduser test
gzip a.c
su
touch
su administrator
touch xy.txt
touch a.c
Useful Commands
df (self study)
df -h
du
du -hs .
bc
time
date
diff
wc
dd
Network Related Commands
(self study)
ifconfig ping
ssh w
scp last
telnet whoami
Unix job control
•Start a background process:
–gedit a.c &
–gedit
hit ctrl-z
bg
•Where did it go?
–jobs
–ps
•Terminate the job: kill it
–kill %jobid
–kill pid
•Bring it back into the foreground
–fg %1
Configuration Files
Most applications have configuration files in TEXT format
Most of them are in /etc
/etc/passwd and /etc/shadow
Text files containing user accounts
/etc/resolv.conf
DNS configuration
/etc/network/interfaces
Network configuration
/etc/hosts
Local database of Hostname-IP mappings
/etc/apache2/apache2.conf
Apache webserver configuration http//free-
~/.bashrc file
(self study)
~/.bashrc
Shell script read each time a bash shell is started
You can use this file to define
Your default environment variables (PATH, EDITOR...).
Your aliases.
Your prompt (see the bash manual for details).
A greeting message.
Also ~/.bash_history

http//free-
Mounting
Partition

What is C:\ , D:\, E:\ etc on your computer ?


“Drive” is the popular term
Typically one of them represents a CD/DVD
RW
What do the others represent ?
They are “partitions” of your “hard disk”
Partition

Your hard disk is one contiguous chunk of storage


Lot of times we need to “logically separate” our storage
Partition is a “logical division” of the storage
Every “drive” is a partition
A logical chunk of storage is partition
Hard disk partitions (C:, D:), CD-ROM, Pen drive, ...
Partitions
Managing partitions and hard drives
System → Administration → Disk Utility
Use gparted or fdisk to partition drives on Linux
Had drive partition names on Linux
/dev/sda → Entire hard drive
/dev/sda1, /dev/sda2, /dev/sda3, .... Different
partitions of the hard drive
Each partition has a type – ext4, ext3, ntfs, fat32, etc.
Formatting: creating an empty layout on disk, layout
capable of storing the tree of files/folders
Windows Namespace
c:\temp\songs\xyz.mp3 Root is C:\ or D:\ etc
Separator is also “\”
C:\

temp\
windows\

songs\ report.doc system32\ drivers\

x.dll
xyz.mp3 abc.mp3 write.exe notepad.exe
Windows Namespace
C:\ D:\ Are partitions of the disk drive
One D:
Typical convention: C: contains programs, “tree” per partition
contains data
Together they make a “forest”

C:\ D:\

temp\ xyz\ sandip\


windows\

songs\ x.doc y\ college\


songs\ report.doc system32\ drivers\

prog.c
x.dll file.jpg game.exe
xyz.mp3 abc.mp3 write.exe notepad.exe xyz.mp3 abc.mp3
Linux Namespace: On a partition
/usr/songs/xyz.mp3 On every partition:
/ Root is “/”
Separator is also “/”
usr/ home/

abhijit/
songs/ note.txt guest/

myfile
xyz.mp3 abc.mp3 midspark.odp a.out
Linux namespace: Mount
Linux namespace is a single “tree” and not a “forest” like
Windows
Combining of multiple
/ trees is done through “mount”
Linux Hard disk
usr/ home/

abhijit/
songs/ note.txt guest/

mydir/ A disk partition


xyz.mp3 abc.mp3 midspark.od a.out /
p
x/ y/

my.x b.cpp file.txt chess.flv


Linux namespace
Mounting a partition
Linux Hard disk Mounting another
/
partition on
usr/ home/ /home/guest/mydir/
directory
Mounted disk
abhijit/ partition
songs/ note.txt guest/

mydir/
xyz.mp3 abc.mp3 midspark.od a.out
p x/ y/

my.x b.cpp file.txt chess.flv

/home/guest/mydir/x/b.cpp → way to access


the file on the other disk partition
Mounting across network!
Using Network File System (NFS)
sudo apt install nfs-common

$ sudo mount 172.16.1.75:/mnt/data /myfolder

http//free-
Files that are not regular/directory
Special devices (1)
Device files with a special behavior or contents
/dev/null
The data sink! Discards all data written to this file.
Useful to get rid of unwanted output, typically log information:
mplayer black_adder_4th.avi &> /dev/null
/dev/zero
Reads from this file always return \0 characters
Useful to create a file filled with zeros:
dd if=/dev/zero of=disk.img bs=1k count=2048

See man null or man zero for details

http//free-
Special devices (2)
/dev/random
Returns random bytes when read. Mainly used by cryptographic
programs. Uses interrupts from some device drivers as sources of
true randomness (“entropy”).
Reads can be blocked until enough entropy is gathered.
/dev/urandom
For programs for which pseudo random numbers are fine.
Always generates random bytes, even if not enough entropy is
available (in which case it is possible, though still difficult, to predict
future byte sequences from past ones).
See man random for details.

http//free-
Files names and inodes

Hard Links Vs Soft Links

http//free-
Creating “links”
Hard link
$ touch m
$ ls -l m
-rw-rw-r-- 1 abhijit abhijit 0 Jan 5 16:18 m
$ ln m mm
$ ls -l m mm
-rw-rw-r-- 2 abhijit abhijit 0 Jan 5 16:18 m
-rw-rw-r-- 2 abhijit abhijit 0 Jan 5 16:18 mm
$ ln mm mmm

You might also like