Topic1
A QUICK INTRODUCTION TO LINUX
2/26/2021
Quick Introduction to Linux
This topic gives a quick overview of the Linux operating system: • What is Linux
• Who Created Linux
• Linux usage in everyday life
• What is Linux Kernel
• What is Linux Shell
• Unix philosophy
• But how to you use the shell
• What is a Shell Script or shell scripting • Why shell scripting
TOPIC 1 – AN INTRODUCTION TO LINUX 2
1
What is Linux
Linux is a free open-source operating system based on Unix, originally developed by Linus Torvalds with the assistance of developers from around the world. Linux is
• Free
• Open Source
• A network operating system
• Unix Like
Linux is a kernel. A kernel provides access to the computer hardware and control access to resources such as:
• Files and data.
• Running programs.
• Loading programs into memory.
• Networks
• Security and firewall.
• Other resources
2/26/2021
TOPIC 1 – AN INTRODUCTION TO LINUX 3
Linux Distribution
The kernel decides who will use a resource, for how long and when. A Linux kernel can be downloaded from the official website. However, the Linux kernel itself is useless unless you get all the applications such as text editors, email clients, browsers, office applications, etc. Therefore, someone came up with the idea of a Linux distribution, which typically includes:
◦ A Linux kernel
◦ GNU application utilities such as text editors, browsers etc
◦ Collection of various GUI (X windows) applications and utilities
◦ Office application software
◦ Software development tools and compilers.
◦ Thousands of ready to use application software packages.
◦ Linux Installation programs/scripts.
◦ Linux post installation management tools for daily work such as adding users, installing applications, etc.
◦ And, a Shell to glue everything together.
TOPIC 1 – AN INTRODUCTION TO LINUX 4
2
Companies such as Red Hat or Norvell provide Linux tech-support and sell their own Linux distributions to Corporate and Small Business Users.
Community driven Linux distributions also exist such as Debian, Gentoo, and Ubuntu(based on Debian).
TOPIC 1 – AN INTRODUCTION TO LINUX 5
Who created Linux
In 1991, Linus Torvalds was studying UNIX at university, where he was using a special educational experimental purpose operating system called Minx ( small version of UNIX to be used in the academic environment). However, Minix had its limitations and Linus felt he could do better, so he developed his own version of Minix, known as Linux.
Linux is a kernel developed by Linus. The kernel was bundled with system utilities and libraries from the GNU project to create a usable operating system. Some people refer to Linux as GNU/Linux because it has system utilities and libraries from the GNU project. Linus Torvalds is credited for creating the Linux Kernel, not the entire Linux operating system.
Linux distribution = Linux kernel + GNU system utilities and libraries + Installation scripts + Management utilities etc.
2/26/2021
TOPIC 1 – AN INTRODUCTION TO LINUX 6
3
Linux is now packaged for different uses in Linux distributions, which contain the sometimes modified kernel along with a variety of other software packages tailored to different requirements such as:
◦ Servers
◦ Desktop
◦ Workstation
◦ Routers
◦ Various embedded devices ◦ Mobile phones
TOPIC 1 – AN INTRODUCTION TO LINUX 7
What is the Linux Kernel
The kernel is the heart of the Linux operating system. It manages the resources of Linux such as:
◦ File Management
◦ Multitasking
◦ Memory management
◦ I/O management
◦ Process management
◦ Device management
◦ Networking support including IPv4 and IPv6
◦ Advanced features such as virtual memory, shared libraries, demand loading, shared copy-on-write executables, etc
The Kernel decides who will use these resources and for how long and when. It runs your programs or set up to execute binary files. The kernel acts as an intermediary between the computer hardware and various applications.
2/26/2021
TOPIC 1 – AN INTRODUCTION TO LINUX 8
4
What is the Linux Shell
Computers understand the language of zeros and ones (binary language). In the early days of computing, instructions were provided using binary language, which is difficult for us to read and write. Therefore, in an operating system there is a special program called the shell. The shell accepts human readable commands and translates them into something the kernel can read and process.
2/26/2021
TOPIC 1 – AN INTRODUCTION TO LINUX 9
What is a Shell?
◦ The shell is a user program or it is an environment provided for user interaction.
◦ It is a command language interpreter that executes commands read from the standard input device
such as keyboard or from a file.
◦ The shell gets started when you log in or open a console (terminal).
◦ Quick and dirty way to execute utilities.
◦ The shell is not part of system kernel, but uses the system kernel to execute programs, create files tec.
◦ Several shells are available for linux including:
◦ BASH (Bourne-Again SHell) – Most common shell in Linux. It’s Open Source.
◦ CSC (C Shell) – The C shell’s syntax and usage are very similar to the C programming language.
◦ KSH (Korn Shell) – Created by David Korn at AT & T Bell Labs. The Korn Shell also was the base for the POSIX Shell standard specifications.
◦ TCSH – It is an enhanced but completely compatible version of the Berkely UNIX C shell (CSH).
Note: each shell does the same job, but each understands different command syntax and provides different built-in functions. Under MS-DOS, the shell name is COMMAND.COM which is used for the same purpose, but is by far not as powerful as our Linux Shells are!
TOPIC 1 – AN INTRODUCTION TO LINUX 10
5
Shell Prompt
Ways to get shell access:
◦ Terminal – Linux desktop provide a GUI based login system> Once logged in you can gain access to a shell by running X Terminal (XTerm), Gnome Terminal (GTerm), or KDE Terminal (KTerm) application.
◦ Connect via secure shell (SSH) – You will get a shell prompt as soon as you log in to a remote server or workstation.
◦ Use the console – A few Linux system also proved a text-based login system. Generally you get a shell prompt as soon as you log in to the system.
2/26/2021
TOPIC 1 – AN INTRODUCTION TO LINUX 11
To find all of the available shells in your system, type the following command:
cat /etc/shells
TOPIC 1 – AN INTRODUCTION TO LINUX 12
6
2/26/2021
Command Line Interface (CLI)
The shell provides an interface to Linux where you can type or enter commands using the keyboard. It is known as the command line interface (CLI). To find out your current shell following command
Basic Command Line Editing
You can use the following key combinations to edit and recall commands: ◦ CTRL + L: Clear the screen.
◦ CTRL + W: Delete the word starting at cursor.
◦ CTRL + U: Clear the line i.e. Delete all words from command line.
◦ Up and Down arrow keys: Recall commands (see command history)
◦ Tab: Auto-complete files, directory, command names and much more.
◦ CTRL + R: Search through previously used commands (see command history) ◦ CTRL + C: Cancel currently running commands.
◦ CTRL + T: Swap the last two characters before the cursor.
◦ ESC + T: Swap the last two words before the cursor.
◦ CTRL + H: Delete the letter starting at cursor.
7
Executing A Command
Type your command, and press enter key. Try the date command which will display current date and time:
date
Command And File Completion
The Bash shell will auto complete file and command names, when possible and/or when you tell them to.
2/26/2021
Getting Help In Linux
man Command
man date
info Command
info ls
–help or -h command line option date –help
TOPIC 1 – AN INTRODUCTION TO LINUX 11-15
TOPIC 1 – AN INTRODUCTION TO LINUX 16
8
Basic Bash Commands
◦ ls – List directory contents. Example: ls /applications will display all the files and folders(directories) stored in the applications folder.
◦ cd – Change directory. Example: cd /usr/local will change from the current directory to /usr/local
◦ mv – Rename or move file(s) or directories. Example1: mv todo.txt /home/jleon/Documents would move “todo.txt” to the “Documents” directory, Example2: mv todo.txt todolist.txt would rename todo.txt to todolist.txt
◦ mkdir – Create a new directory(folder). Example: mkdir myFiles will make a directory named “myFiles”
◦ rmdir – Delete empty directory. Example: rmdir myFiles will delete the empty “myFiles” directory.
◦ rm – Remove file(s) and or directories. Example: rm todolist.txt will delete the “todolist.txt” file.
◦ clear – Clear a command line screen/window for a fresh start.
◦ cp – Copy files and directories. Example: cp todo.txt /home/jleon/Documents would create a copy of “todo.txt” to the “Documents” directory
◦ cat – Display the contents of a file on the screen. Example: cat todo.txt will show the text of “todo.txt” on the screen.
2/26/2021
TOPIC 1 – AN INTRODUCTION TO LINUX 17
◦ chmod – Change a file’s permissions. Example: chmod 777 todo.txt will make “todo.txt” readable, writable, and executable by everyone. The digits in “777” specify the permissions for user, group, and others, in that order.
◦ sudo – Perform tasks that require administrative or root permissions. Example: Use sudo passwd mgramirez to change the password of user “mgramirez”
◦ grep – Search files or output for a particular string or expression. This command searches for lines containing a specified pattern and, by default, writes them to the standard output. Example: grep run todo.txt will search for the “run” in the “todo.txt” file. Lines that contain “run” will be displayed.
◦ date – Display or set the system date and time.
◦ pwd – Display the pathname for the current directory. (Print Working Directory)
◦ ps – Displays a list of the currently running processes. This can be used to determine PIDs needed to kill processes.
◦ ssh – Remotely log in to another Linux machine, over the network. Example: ssh quincy@104.25.105.32 will login to 104.25.105.32 using the username “quincy”.
TOPIC 1 – AN INTRODUCTION TO LINUX 18
9
◦ tail – Display the last 10 lines of a file. See fewer or more lines by using the –n (number) option. Example: tail –n 5 todo.txt will display the last 5 lines of “todo.txt”
◦ head – Display the first 10 lines of a file. See fewer or more lines by using the –n (number) option. Example: head –n 5 todo.txt will display the first 5 lines of “todo.txt”
◦ top – Displays the resources being used on your system, similar to the task manager in Windows.
◦ kill – stop a process. Example: kill 485 will stop a process with a PID of 485 using the command kill 485. Can use the ps
command to determine the PID of a process.
TOPIC 1 – AN INTRODUCTION TO LINUX 19
What is a Shell Script or shell scripting
Normally shells are interactive, which means the shell will accept commands from you (via the keyboard) and execute them. However, if you store a sequence of commands in a text file and tell the shell to execute the text file, that is known as a shell program or shell script.
A shell script can be defined as – “a series of command(s) stored in a plain text file”. A shell script is similar to a batch file in MS-DOS, but it’s much more powerful compared to a batch file.
2/26/2021
TOPIC 1 – AN INTRODUCTION TO LINUX 20
10
Why shell scripting?
◦ Shell scripts can take input from a user or file and output them to the screen.
◦ Whenever you find yourself doing the same task over and over again you should use shell scripting, i.e.,
repetitive task automation.
◦ Creating your own power tools/utilities.
◦ Automating command input or entry.
◦ Customizing administrative tasks.
◦ Creating simple applications.
◦ Since scripts are well tested, the chances of errors are reduced while configuring services or system administration tasks such as adding new users
2/26/2021
TOPIC 1 – AN INTRODUCTION TO LINUX 21
Shell commands
Bash sell comes with two types of commands:
◦ Internal commands (builtins) – part of the shell itself
◦ External commands – separate binaries stored in /sbin, /usr/sbin, /user/bin, /bin, or /usr/local/bin
directories.
Bash and Command Types
The bash shell understands the following types of commands: ◦ Aliases such as ll
◦ Keywords such as if
◦ Functions (user defined functions)
◦ Built in such as pwd
◦ Files such as /bin/date
TOPIC 1 – AN INTRODUCTION TO LINUX 22
11
Use the type command to find out if a command is built in or external Ex. type –a ls
Sample output: ls is /bin/ls
Ex. type –a history
Sample output: history is a shell built in Ex. type –a echo
Sample outputs:
echo is a shell built in
echo is /bin/echo
TOPIC 1 – AN INTRODUCTION TO LINUX 23
Bash initialization and startup files
◦ /etc/profile – The systemwide initialization file, executed for login shells.
◦ /etc/bash/bashrc – The systemwide per-interactive-shell startup file. Tis is a non-standard file which may or may not exist on your distribution. Even if it exists, it will not be sources unless it is done explicitly in another start-up file.
◦ /etc/bash.logout – The systemwide login shell cleanup file, executed when a login shell exits.
◦ $HOME/.bash_profile – The personal initialization file, executed for login shells.
◦ $HOME/.bashrc – The individual per-interactive-shell startup file.
◦ $HOME/.bash_logout – The individual login shell cleanup file, executed when a login shell exits.
◦ $HOME/.inputrc – Individual readline initialization file.
2/26/2021
TOPIC 1 – AN INTRODUCTION TO LINUX 24
12
Shell initialization files
SYSTEM-WIDE CONFIGURATION FILES
2/26/2021
TOPIC 3 – THE BASH ENVIRONMENT 25
/etc/profile
When invoked interactively with the –login option or when invoked as sh, Bash reads the /etc/profile instructions. These instructions usually set the shell variables PATH, USER, MAIL, HOSTNAME and HISTSIZE
◦ values that can be stored
◦ operations that can be done on the values
All settings that you want to apply to all your users’ environments should be in this file.
TOPIC 3 – THE BASH ENVIRONMENT 26
13
# /etc/profile
# System wide environment and startup programs, for # login setup
PATH=$PATH:/usr/X11R6/bin
# No core files by default ulimit -S -c 0 > /dev/null 2>&1
USER=”`id -un`” LOGNAME=$USER MAIL=”/var/spool/mail/$USER”
HOSTNAME=`/bin/hostname` HISTSIZE=1000
# Keyboard, bell, display style: the readline config # file:
if [ -z “$INPUTRC” -a ! -f “$HOME/.inputrc” ]; then
INPUTRC=/etc/inputrc
PS1=”\u@\h \W”
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC PS1
# Source initialization files for specific programs (ls, vim, less, …)
for i in /etc/profile.d/*.sh ; do
if [ -r “$i” ]; then . $i
fi done
# Settings for program initialization
source /etc/java.conf
export NPX_PLUGIN_PATH=”$JRE_HOME/plugin/ns4plugin/:/usr/li b/netscape/plugins”
PAGER=”/usr/bin/less” unset i
fi
TOPIC 3 – THE BASH ENVIRONMENT 27
Shell initialization files
INDIVIDUAL USER CONFIGURATION FILES
2/26/2021
TOPIC 3 – THE BASH ENVIRONMENT 28
14
~/.bash_profile
Note: These files might be in your home directory by default. If not, go ahead and create them.
The .bash_profile file is the preferred configuration file for configuring user environments individually. In this file, users can add extra configuration options or change default settings:
2/26/2021
TOPIC 3 – THE BASH ENVIRONMENT 29
################################################################# ##
# .bash_profile file # ##
# Executed from the bash shell when you log in. ## #################################################################
source ~/.bashrc source ~/.bash_login
case “$OS” in
IRIX)
stty sane dec
stty erase
;;
# SunOS)
# stty erase # ;;
*)
stty sane
;; esac
This user configures the backspace character for login on different operating systems. It also read’s the user’s .bashrc and .bash_login files
#
TOPIC 3 – THE BASH ENVIRONMENT 30
15
Hello World! Tutorial
To create a shell script:
◦ Use a text editor such as vim. Write required Linux commands and logic in the file. ◦ Save close the file (exit from vim).
◦ Make the script executable (chmod +x)
◦ Test your script and once satisfied move to the production environment.
#!/bin/bash
echo “Hello, World!” echo “Welcome to CS018”
2/26/2021
TOPIC 1 – AN INTRODUCTION TO LINUX 31
16