Linux Systems Programming: Ubuntu Installation and Configuration

RashidFaridChishti 49 views 45 slides May 07, 2024
Slide 1
Slide 1 of 45
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45

About This Presentation

Linux Systems Programming: Linux Basics


Slide Content

Linux System Programming Getting Started Engr. Rashid Farid Chishti [email protected] https://youtube.com/rfchishti https://sites.google.com/site/chishti International Islamic University H-10, Islamabad, Pakistan http://www.iiu.edu.pk

Prerequisites CS213 Data Structure & Algorithms CS231 Operating Systems Linux Basics Why You should study this course? To become a better C Programmer To become a Linux Developer To become a System Level Programmer Recommendations

Recommended Text Books

Recommended Virtual Machine Oracle VM Virtual Box

Recommended Virtual Machine

Recommended Operating System ubuntu-22.04.3 desktop-amd64

Linux Basics: Installation , Commands gcc compiler usage: Header files, libraries, exes Linux Files structure: Files and Directory Access. Process and Signals: Process Structure, Starting new Process Threads: Threads creation, Synchronization, Scheduling Inter Process Communication ( IPC): Pipes , Semaphore, Shared memory, Message Queues Sockets: (TCP/UDP ) Sockets Programming Device Drivers: Character/Block Devices Programming Kernel: Linux Kernel Management, Recompilation Course Outline

Popular Desktop Operating Systems in Year 2023

The UNIX operating system was developed in the 1960s and 1970s at Bell Labs, primarily by Ken Thompson, Dennis Ritchie, and others . It was a very popular multi-user (many users can simultaneously use a single machine), multi-tasking (it can run multiple programs simultaneously) operating system for different platforms, from PC workstations to multiprocessor servers and supercomputers. Over time, various versions of UNIX have been developed, and it has influenced the design of many other operating systems, including Linux and BSD (Berkeley Software Distribution ). UNIX

Linux was developed by Linus Torvalds (Finland) He started working on the project in 1991 while he was a student at the University of Helsinki . Linus Torvalds initially created Linux as a hobby and released the first version of the Linux kernel (the core of the operating system) to the public on September 17, 1991. The development of Linux has since evolved into a collaborative effort with contributions from a global community of developers. The Linux operating system is now widely used in various forms, powering servers, embedded systems, and serving as the basis for many distributions like Ubuntu , Fedora , Debian , CentOS, Knoppix , Kali Linux and many others. The name LINUX stands for the recursive LINUX Is Not Unix . LINUX

The name GNU stands for the recursive GNU’s Not Unix . The Free Software Foundation GNU was set up by Richard Stallman . Free Software means “there may be a cost involved in obtaining the software, it can thereafter be used in any way desired and is usually distributed in source form” Some of the free software from the GNU Project: LINUX: a free UNIX-like system. (KDE, GNOME Desktop) GCC: The GNU Compiler Collection, containing the GNU C compiler G++: A C++ compiler, included as part of GCC GDB: A source code–level debugger GNU make: A version of UNIX make Gimp: A graphical image manipulation tool bash: A command shell GNU

There are two special types of files: executables and scripts Executable files are programs that can be run directly by the computer ; they correspond to Windows .exe files. Scripts are collections of instructions for another program or an interpreter, to follow. These correspond to Windows .bat or . cmd files. In Linux executables or scripts to do not require to have a specific filename or any extension whatsoever. File system attributes , (discuss later), are used to indicate that a file is a program that may be run. A shell program (often bash ) like command prompt in windows is used to run our programs or commands. Linux uses Environment Variable , PATH to search command on different paths. e.g. In Linux run this command and see the output echo $ PATH Linux Programs

The purpose of different paths / usr /local/bin : programs added by administrators for a specific host computer or local network are found here / usr /bin : programs supplied by the system for general use /bin : Binaries, programs used in booting the system / sbin : for system administrator root / usr / sbin : for system administrator root Use env command to show all the environment variables. e nv e nv | grep USER Linux Programs

Linux Commands

Linux Commands

ls : List files and directories. ls, ll , ls –al, man ls cd : change directory command cd, cd .., cd folder, relative and absolute path, pwd cp : Copy files or directories. man cp , cp file1 fl2, cp – rv folder1 fld2 mv : Move or rename files and directories . man mv, mv file1 file2, mv folder1 folder2 rm : Remove/delete files or directories . rm file, rm –r dir , rm –f file, rm – rfv dir , rmdir folder_name mkdir : Create a new directory. mkdir folder_name touch : Create an empty file or update modification times of a file . touch file_name , touch folder_name clear : clear screen like cls command in windows File and Directory Commands

c at: Displays the entire content of a file. cat myfile.txt more: Displays the content of a file one screen at a time. You can navigate using the spacebar. more myfile.txt less: Similar to more, but allows both forward and backward navigation through the file. less myfile.txt head: Displays the first few lines of a file. By default, it shows the first 10 lines . head –n 10 myfile.txt tail : Displays the last few lines of a file. By default, it shows the last 10 lines. tail –n 20 myfile.txt File View Commands

nano : A simple and user-friendly text editor. nano myfile.txt micro : A modern terminal based text editor . micro myfile.txt vi: A powerful and versatile text editor with modes for navigation and editing. vim myfile.txt emacs : Another powerful text editor with extensive features. emacs filename gedit (GNOME Text Editor): A graphical text editor for systems using the GNOME desktop environment . gedit myfile.txt File Editing Commands

df : Displays information about disk space usage on mounted file systems. d f -h du: Shows the disk usage of files and directories . du –h file_name fdisk : Displays information about disk partitions on a system. sudo f disk –l, sudo fdisk /dev/ sdb mount : Displays information about currently mounted file systems. mount | grep sda , sudo mount /dev/ cdrom / cdrom p wd : Prints the current working directory. pwd File Information Commands

uname : Displays system information such as kernel name, network node hostname, kernel release, kernel version, and machine hardware . uname -a hostname : Shows or sets the name of the current host. hostname uptime : Shows how long the system has been running, as well as load averages. free : Displays information about system memory usage . free -h lscpu : Displays information about the CPU architecture and cores . lscpu lsusb : Displays information about the CPU architecture and cores. lscpu date : Displays the current date and time . date cal : Displays a calendar for the current month . cal w : Shows who is logged in and what they are doing . w System Information Commands

ps : Shows information about currently running processes. ps aux top : Gives a dynamic view of system processes and resource usage . top pstree : Displays a tree diagram of processes. pstree -p | less kill : Sends a signal to terminate a process. The -9 option forcefully kills the process kill -9 1234, killall -9 firefox killall : Sends a signal to terminate all processes with the specified name . Command_name & : Runs a command in background sleep 10 &, (sleep 5; echo "Done") & job : Displays the status of jobs running in the background. job bg : Puts a job in the background. bg fg : Brings a job to the foreground. fg nice : Runs a command with a specified priority. nice -n 10 my_process renice : Changes the priority of an already running process. renice +5 1234 Process Management Commands

In Linux, runlevels are predefined operating states in which a system can operate. Each runlevel is associated with a specific set of services and configurations . Here are the commonly used runlevels Runlevel 0: Halt/Shut down the system. Runlevel 1: Single-user mode, also known as rescue mode, used for system maintenance tasks. Typically, only a minimal set of services are started. Runlevel 2: Multi-user mode with networking, but without graphical user interface (GUI). It's similar to runlevel 3 but may have fewer services enabled. Runlevel 3: Multi-user mode with networking and a text-based user interface. This runlevel is often used for servers and systems where a graphical interface is not required. Runlevel 4: Not used by default, can be defined by the system administrator. Runlevel 5: Multi-user mode with networking and a graphical user interface (GUI). This runlevel is typically used for desktop systems. Runlevel 6: Reboot the system . Linux Run Levels

To switch to single-user mode (equivalent to runlevel 1), you can use: sudo systemctl isolate rescue.target To switch to multi-user text mode (equivalent to runlevel 3), you can use: sudo systemctl isolate multi- user.target To switch to graphical mode (equivalent to Run Level 5 ), you can use: sudo systemctl isolate graphical.target Use the following command to set the default target to multi- user.target : sudo systemctl set-default multi- user.target Linux Run Levels

useradd : Creates a user sudo useradd -m -s /bin/bash -c "Rashid Farid Chishti" Chishti passwd : Changes password of a user sudo passwd Chishti whoami : print effective userid whoami w : shows who is logged in and what they are doing w finger : displays information about the user finger Chishti userdel : Delete a user account and related files sudo userdel -r Chishti groupadd : Create a new group. sudo groupadd Teachers usermod : Modify a user account. sudo usermod - aG Teachers Chishti i d: Shows user id and group id id Chishti Linux Files: / etc / passwd / etc /groups / etc /shadow chown : Change ownership sudo chown -r newowner:newgroup directory c hmod : Change file permissions chmod 755 filename, chmod -R 644 directory User Management Commands

Assumptions : At the end of commands, if nothing happens press the ENTER key. Linux is case sensitive (\ a" is not the same as \A "). Entering vi vimtutor Invokes a tutorial for vi vi filename Normal way of entering vi Saving and Exiting ZZ Quit and Save : wq Quit and Save :q ! Force quit and ignore changes :w Save changes and stay in file :f file Change current filename to file Using Vi Editor (1/3) Positioning the Cursor ZZ Quit and Save G Go to last line in file n G Go to line number n : n Go to line number n b Back one word w Start of next word ( Beginning of previous sentence ) Beginning of next sentence f Beginning of previous paragraph g Beginning of next paragraph H Home-top line on screen L Last line on screen

Inserting New Text i Insert before cursor I Insert at beginning of line a Insert after line A Append to end of line o Insert one line below cursor O Insert one line above cursor Esc Terminate insert mode Searching / string Search forward for string ? string Search backward for string :s/ old / new / n Search and replace n times Using Vi Editor (2/3) Deleting and Copying Text x Delete character at current cursor position dd Delete current line dw Delete a word n dd Delete n lines n dw Delete n words D Delete remainder of line Y or yy Copy current line to new bffer p Place buffer contents after cursor or Place last deleted text after cursor P Place buffer contents before cursor or Place last deleted text before cursor

Changing Text r x Replace character with x R Replace beginning at cursor cw Change word cc Change line C Change to end of line Miscellaneous Commands :! command Issue LINUX command J Join two lines :r file Append file into vi Z Redraw the screen . Repeat last command u Undo last command Using Vi Editor (3/3) Positioning the Cursor k Move up one line j Move down one line l Move right one character h Move left one character h (You can use arrow keys for above functions ) ^ B Scroll back one screen ^F Scroll forward one screen ^U Scroll back half screen ^D Scroll forward half screen Go to first position on current line $ Go to last position on current line

Mount CDROM sudo mount /dev/ cdrom / cdrom Install gcc , g++ and make sudo apt-get update sudo apt install build-essential Install Vim Editor sudo apt install vim sudo apt install vim- gtk Problem: in Vim Editor Arrow keys show characters: If you don't already have a . vimrc file in your home directory, create one using this: vim ~/. vimrc Add this line to the top of the file: set nocompatible Press i for Editing Mode and Esc for Command Mode, Press : wq for save and quit Add indentation in C/C++ program sudo apt install indent indent program.c change the appearance of a C program by inserting or deleting whitespace search indent related commands apropos indent Problems and Solution

Configure Proxy sudo vi / etc /apt/ apt.conf.d / proxy.conf add the following lines. Acquire { HTTP ::proxy "http://192.168.10.9:8080 "; HTTPS :: proxy "http ://192.168.10.9:8080 "; } To use wget behind proxy sudo vi / etc / wgetrc https_proxy   = http:// username:password@proxy:port http_proxy = http:// username:password@proxy:port ftp_proxy = http:// username:password@proxy:port Uncomment ' use_proxy = on ' Proxy Settings

First update the system using the  apt command  or  apt-get command : $ sudo apt update $ sudo apt upgrade Install ifconfig command sudo apt install net-tools install the manual pages about using GNU/Linux for development: sudo apt install manpages -dev To validate that the GCC compiler is successfully installed use the   gcc - - version $ sudo apt install g++ Search for pthread library sudo apt search pthread Using apt command

whoami Who you are logged in as w Display who is online prevent  user from changing his password for almost 274 years. passwd -n 9999 user make a file data.txt of size 5MB truncate -s 5M data.txt make default shell as bash for user sudo vi / etc / passwd change the appearance of a C program by inserting or deleting whitespace replace /bin/ sh with /bin/bash wall this is msg to all Send msg to all Linux user mesg n Block messages mesg y Enable lock broadcast messages write user_name your_message Ctrl+C Send msg to a specific user User Management

date Show current date and time Show Applications > Language Support > Regional Formats > English (United States) > Close reboot Restart Computer sudo apt install ncal cal Show this month’s calander uptime Show current uptime sudo date -s "16 OCT 2019 13:16:00" Set software clock sudo date --set="2019-10-18 10:25:30" Set software clock sudo hwclock -- systohc Sync with hardware clock Setting Date and Time

To install openssh -server package, run: $ sudo apt search openssh -server $ sudo apt install openssh -server Verify that ssh service is running: $ sudo systemctl status ssh Configure firewall and open port 22 You must configure the Ubuntu Linux firewall called ufw . $ sudo ufw allow ssh $ sudo ufw enable $ sudo ufw status $ sudo systemctl restart ssh Testing it : Y ou can login from another computer using MS-Windows (putty client) or Unix-like system using the ssh command : $ ssh [email protected] Installing SSH Server

Install ftp server ­ ­ sudo apt install vsftpd Show version of installed ftp server vsftpd – v Enable ftp server when system starts sudo systemctl enable vsftpd Make backup of configuration file sudo cp / etc / vsftpd.conf / etc / vsftpd.conf.orig Open configuration file if you need it sudo vi / etc / vsftpd.conf write_enable =YES Uncomment this line to allow uploading data ssl_enable =YES Enable ftp over ssh (port 22) Restart ftp server sudo systemctl restart vsftpd Installing FTP Server

To install apache2 package, run: update the local package index $ sudo apt update install the apache2 package $ sudo apt install apache2 List the ufw application profiles $ sudo ufw app list Verify that apache is running: $ sudo systemctl status apache2 Configure firewall and open port 22 You must configure the Ubuntu Linux firewall called ufw . $ sudo ufw allow Apache $ sudo ufw enable $ sudo ufw status $ sudo systemctl restart apache2 Testing it : Write down ip address of Linux server in a web browser. Installing Apache Web Server

Add DNS Server IP Address in / etc / resolv.conf sudo vi / etc / resolv.conf Add these two lines n ameserver 1.1.1.1 n ameserver 8.8.8.8 Apt-get Issues

Type vi hello.c on shell, press i Type following code in hello.c file. Press Esc key To save and exit write : wq and then press Enter Button Now compile the code using command gcc hello.c Run the Program using command ./ a.out Your First Linux C Program #include < stdio.h > int main() { printf ( "Hello World\n" ); exit( ); } hello.c

Type this command ll to see list for files Now compile in this way gcc –o hello hello.c Run the Program ./ hello Header Files are placed at / usr /include To see which of the header files contains EXIT_ word type following commands. cd / usr /include grep EXIT_ *. h Library Files are usually placed in /lib and / usr /lib directory A library filename always starts with lib Then follows the part indicating what library is this (like c for the C library , or m for the mathematical library ). Using gcc Compiler

The last part of the name starts with a dot ( . ) and specifies the type of the library .a for static libraries .so for shared libraries Examples / usr /lib/ lib c . so . 6 is a standard C shared library in version 6 / usr /lib/ lib m . a is a standard C static library for math functions When a shared library is linked into a program, the final executable does not actually contain the code that is present in the shared library. The executable merely contains a reference to the shared library. If several programs on the system are linked against the same shared library, they will all reference that library, but none will actually be included. Thus, the library is “ shared ” among all the programs that link with it. So the size of Library Files

program is reduced and the space is saved. Type following command ldd a.out and see the output When a code is compiled against a statically linked library , the code for any referenced library routine is included directly in the resulting program binary. This results in very large application executables because each one contains a duplicate of standard routines. Analogies between Windows and Linux Item Linux Windows object module func.o func.obj static library lib.a lib.lib shared library lib.so lib.dll program program program.exe Library Files

In this example, you create your own small library containing two functions and then use one of them in an example program. The functions are called fred and bill and just print greetings. First , create separate source files (imaginatively called fred.c and bill.c ) for each function . Try It Out Static Libraries 1/3 // this is fred.c # include < stdio.h > void fred ( int arg ){ printf ( " fred : we passed % d\n" , arg ); } fred.c // this is bill.c # include < stdio.h > void bill( char * arg ){ printf ( "Bill: we passed %s\n" , arg ); } bill.c

Produce object files of both fred.c and bill.c gcc –c fred.c bill.c ll *.o Create a header file for our library. This will declare the functions fred () and bill() present in our library Include the library header file in our calling program and calls one of the functions from the library Try It Out Static Libraries 2/3 void fred ( int arg ); void bill( char * arg ); lib.h // this is program.c # include < stdlib.h > # include " lib.h " int main(){ bill( "Hello World" ); exit( ); } program.c

Compile our file program.c and link it with the previously compiled object module bill.o gcc -c program.c gcc -o program program.o bill.o ./program create our own library called libfoo.a ar crv libfoo.a bill.o fred.o create a table contents for the library, make the library ready to use ranlib libfoo.a use this library to link to program.o */ gcc -o program program.o libfoo.a ./ program OR You can use a shorthand notation gcc -o program program.o -L. - lfoo Try It Out Static Libraries 3/3

Section contents 1 user commands 2 system calls 3 C library functions 4 devices and network interfaces 5 file formats 6 games and demos 7 environments, tables and macros 8 system maintenance e.g. man 1 printf indent program.c man 3 printf man indent man for help

Make a static library with the name libYourName.a . This library will contain following functions void YourName_Get (double Mat[][3]) // Gets data for a 3x3 Matrix void YourName_Show (double Mat[][3]) // Shows a 3x3 Matrix // Adds two 3x3 matrices Mat_A and Mat_B and saves the result in Mat_C void YourName_Add (double Mat_A [][3 ], double Mat_B [][3 ], double Mat_C [][3 ]) // Multiplies two 3x3 matrices Mat_A and Mat_B and saves the result in Mat_C void YourName_Mul (double Mat_A [][3 ], double Mat_B [][ 3], double Mat_C [][3 ]) // Takes inverse of a 3x3 Mat_A and save the inverse matrix in Mat_B . void YourName_Inv (double Mat_A [][3 ], double Mat_B [][3 ]) // Calculates and returns Mod of a 3x3 matrix Mat_A . double YourName_Mod (double Mat_A [][3 ])   Write all the steps to compile your library Call these library functions in your main program to show the functionality Assignment #1