embedded linux – course goals - bfh · embedded linux module, ... sun: solaris, dec: ultrix, hp:...

84
Bern University of Applied Sciences Engineering and Information Technology Linux Introduction By Franz Meyer and Martin Aebersold Version 2.8 September 2015

Upload: dinhtram

Post on 27-Jul-2018

269 views

Category:

Documents


1 download

TRANSCRIPT

Bern University of Applied SciencesEngineering and Information Technology

Linux Introduction

By Franz Meyer and Martin AebersoldVersion 2.8September 2015

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-2Embedded Linux Module, Introduction

You will be able to: handle a Linux-System on a PC (console, editor, tools)understand the principles of the UNIX file system export and mount a file system across the networkapply the most important commands of the command linegain information about new commands yourself

Linux Introduction: Objectives

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-3Embedded Linux Module, Introduction

A brief history of UNIX

UNIX philosophy and features

The main parts (layers) of UNIX

UNIX File System

Network File System

Basic UNIX-Commands

Linux Introduction: Topics

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-4Embedded Linux Module, Introduction

Mid-sixties @ Bell Labs, MIT, GE: General purpose „computer-tool“, handles hundreds of users simultaneously -> OS: MULTICS (Multiplexed Information and Computing Service)1969, Ken Thompson, Bell Labs: UNICS (Uniplexed...)1972-1974 Thompson & Ritchie: Completely rewritten in C for PDP-11, Multiprogramming, Renamed to UNIX1978 Port to VAX, virtual memory (Demand Paging)1980 First release of BSD UNIX (Berkeley Software Distribution)Paged Virtual Memory, TCP/IP1983 Richard Stallman starts GNU-Project1991 First release of LINUX (Based on Tanenbaums MINIX)1998 X_Window manager and KDE2002 Open Office Version 1.0

The History of UNIX, how it began

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-5Embedded Linux Module, Introduction

The most important Milestones

Posting 1:Sender: Richard M. StallmanDate: September 27th 1983Subject: new UNIX implementation  

Posting 2:Sender: Linus B. TorvaldsDate: August 25th 1991Subject: What would you like to see most in minix?

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-6Embedded Linux Module, Introduction

The 4 freedoms by Richard Stallman / GNU project

The freedom to run the program, for any purpose (freedom 0). The freedom to study how the program works, and adapt it to your needs (freedom 1). Access to the source code is a precondition for this. The freedom to redistribute copies so you can help your neighbor (freedom 2). The freedom to improve the program, and release your improvements to the public, so that the whole community benefits (freedom 3). Access to the source code is a precondition for this.

Ref: http://www.gnu.org/philosophy/free-sw.html

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-7Embedded Linux Module, Introduction

Ref: http://en.wikipedia.org/wiki/Unix_wars

UNIX genealogy

System III & V family

BSD (Berkeley Software Distribution)

FreeBSD

NetBSD

OpenBSD

SunOS

Solaris

NextStep

Xenix OS

GNU Linux

Commercial UNIX

HP-UX

AIX

UnixWare

IRIX

BSD family

1970 1980 1990 2000 Time

Microsoft/SCO

Richard Stallman

Darwin

GNU/Hurd K16

Linus Torvalds

Andrew S. Tanenbaum Minix

3.3

6.5.30

4.1.4

Research UNIX 10.5

Bell Labs: Ken Thompson, Dennis Ritchie, et al.

Bill Joy

AT&T

IBM SGI

Univel/SCO

2010

4.4

5.8

10.2

OS X 10.11

7.0

11.3

7.2

11i v 3Sun/Oracle

Apple

4.3

3.3

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-8Embedded Linux Module, Introduction

There are many different flavors of UNIX:Sun: Solaris, DEC: Ultrix, HP: hpux, Microsoft: XENIX, Apple AUX/OS-X, IBM: AIX, ...

There are many different flavors of the free variants of UNIX:FreeBSD, OpenBSD, NetBSD, Linux, ...

There are many different distributions of LINUX:Debian, Red Hat, SuSE, Mandrake, ...

What they have in common: Most of the commands, overall structure of the kernel, API, usage patterns

UNICES / LINUCES

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-9Embedded Linux Module, Introduction

Small is beautifulSmall functional programs are readily understood, applied, and reused, and there is not as much complexity amongst which bugs can hide.

Make each program do one thing wellMultiple useful programs can then be composed together to perform more complex functions.

Prototype as soon as possible Early affirmation/denial of the validity of assumptions is really valuable.

Choose portability over efficiencyEffectiveness beats efficiency. If software works adequately, it will work even better on next year's hardware. And the real performance wins come out of the selection better algorithms, which has little to do with portability.

Store numerical data in flat ASCII filesPortable data is as important as portable code... Using text data files makes it easy to use powerful text-processing tools to manipulate the data.

Use software leverage to your advantageDon't force yourself or others to reimplement functionality; make it easy to reuse functionality.

UNIX Philosophy (1) ref: Mike Gancarz: The UNIX Philosophy

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-10Embedded Linux Module, Introduction

Use shell scripts to increase leverage and portabilityShell scripts don't need to be compiled or recompiled, and run faster and better any time:

Hardware improves Software improves (e.g. - someone optimizes the shell code)

Avoid captive user interfaces"Hidden" interfaces are something people will have to work around. Languages like Expect can work

around this, but represent kludges nonetheless. Make every program a filter

This makes it easy to build a set of filter components into "one big filter." Programs can communicate by explicitly reading and writing files; that is much more complex to manage, and requires a lot of disk I/O for interim output. A set of filters may pass data straight from memory buffer to memory buffer, avoiding disk altogether.

System abstractionKernel: hides hardwareShell

Interface between user and kernelText mode (command line)

X Window System: The GUI interface

UNIX Philosophy (2) ref: Mike Gancarz: The UNIX Philosophy

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-11Embedded Linux Module, Introduction

The most important philosophy

KISKeep it simple

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-12Embedded Linux Module, Introduction

Extremely flexible and portableFrom micro computer to mainframe

Multi-userProtection: A user cannot mess up with other user's resources like files, memory, system settings, environment, etc.

Multi-taskingMulti-processor supportNetworking support (Ethernet, TCP/IP)Network file system support

Main UNIX Features (1)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-13Embedded Linux Module, Introduction

Rich variety of development tools„Toolbox“ structureConfiguration of services is done in ASCII-FilesMany different „look and feels“Stable (Makes it ideal for server tasks AND Embedded Systems)Some implementations are Open SourceScalable, configurable

Main UNIX Features (2)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-14Embedded Linux Module, Introduction

The OS / Kernel encapsulates theHardware and manages the access of users and applications to these resources

Typical tasks of amultiuser/multitasking OS:

User managementProcess/CPU-managementMemory managementFile managementDevice management

Layered model (onion m.)

Kernel

HW

Applikationen

Tools + Utilities

Shell

UNIX Layering

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-15Embedded Linux Module, Introduction

HDHD-PartitionCDROMFloppyNetwork DriveFlash...

The solution: mount individual storage devices into a single unified file system tree

UNIX/Linux Filesystem Hierarchy (1)

tmp

include

include

/

local

usr

bin lib

libbin doc

opc

X11 net

root

bin home

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-16Embedded Linux Module, Introduction

Almost everything is a file in UNIXDirectories are files too: They contain files and perhaps other directories --> forms the hierarchyFile names are case sensitiveA path is a sequence of nested directories with a file or a directory at the end, separated by /

Relative path: lib/opc Working directory is: /usr/localAbsolute path: /usr/local/lib/opcIndependent of working directory

UNIX/Linux Filesystem Hierarchy (2)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-17Embedded Linux Module, Introduction

The file system is not imposed by the Linux system. It can vary slightly from system to system. In general a Linux file system uses the following top level structure:

/ the root directory/bin basic, essential system commands/sbin sysadmin-only commands/boot kernel images, files of the bootloader, etc./dev device files, e.g. /dev/hda: first IDE hard disk/etc system configuration files/home user directories/lib basic system and shared libraries

UNIX/Linux Filesystem Hierarchy (3)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-18Embedded Linux Module, Introduction

/lost+found parts of corrupted files --> for recovery/media mount point for removable media/mnt mount points for other file systems/opt specific software installed by the sysadmin/proc system information (pseudo file system)/root super user's (root) home directory/srv data for services provided by this system/sys system and device control (pseudo file system)/tmp temporary files (may be cleared at boot time)/usr tools coming with the distribution/usr/local specific software (alternative: /opt)/var data used by system or servers/var/log log files

UNIX/Linux Filesystem Hierarchy (4)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-19Embedded Linux Module, Introduction

Exported File Systems controlled by file/etc/exports

Imported File Systems controlled by file/etc/fstabor by themount command

Network File System Principles (NFS) (1)

NFS Client (Paul's machine)NFS Server

Network

/

tmp home usr

jeff ralph paul

prog doc img

/

tmp home usr

bin lib

x11 net

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-20Embedded Linux Module, Introduction

NFS Server on machine nfs­srvr: Exported File Systems

#/etc/exports# exported file system     allowed clients# paul's home directory all machines in the given#                             IP subnet may read and write

/home/paul 147.87.70.0/24(rw)

NFS client on machine 147.87.70.10: Imported File Systems - by mount command (as root)

#mount ­t nfs nfs­srvr:/home/paul  /home/paul 

- by file /etc/fstab     (controls file system mount at startup time)

# file system        mount­point fs­type flags  dump  fschk nfs­srvr:/home/paul  /home/paul  nfs     rw     0     0 

Network File System Principles (NFS) (2)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-21Embedded Linux Module, Introduction

Network File System: Host - BBB

BBBHost

/

bin boot

cross eclipse

microwin openocd rootfs

opt

bbb

bin dev etc

/

bin dev etc

Network

Berne University of Applied SciencesSchool of Information Technology HTI

V1.2 © 2008 by [email protected] Slide-22

Module 7063Operating Systems

Introduction

Login into a Linux System using a Graphical Desktop

When you boot Linux from your USB disk you will perhaps see boot messages, then after a while the graphical display manager will be started. It welcomes you on a welcome page.Log in with the user id “student”. Then give the password.If the log in is successful (you gave the correct password) then the graphical desktop (GNOME or KDE) will be started. When it is ready you may use a lot of applications.At the beginning you only run a console application. This is a command window that runs an interactive shell. The key combination Ctrl+Alt+F1..F6 allow you to log in a text console.The key combination Alt+F7 brings you back to the graphical desktop

Berne University of Applied SciencesSchool of Information Technology HTI

V1.2 © 2008 by [email protected] Slide-23

Module 7063Operating Systems

Introduction

Login into a Linux System using a Graphical Desktop

When you boot Linux from your USB disk you will perhaps see boot messages, then after a while the graphical display manager will be started. It welcomes you on a welcome page.Log in with the user id “student”. Then give the password.If the log in is successful (you gave the correct password) then the graphical desktop (GNOME or KDE) will be started. When it is ready you may use a lot of applications.At the beginning you only run a console application. This is a command window that runs an interactive shell. The key combination Ctrl+Alt+F1..F6 allow you to log in a text console.The key combination Alt+F7 brings you back to the graphical desktop

Berne University of Applied SciencesSchool of Information Technology HTI

V1.2 © 2008 by [email protected] Slide-24

Module 7063Operating Systems

Introduction

Login into a Linux System using a Text Console

init

create process

getty

become

Login: marvin

Password: eeee

$ ...

login =?=

show promptread commandexecute command

shell

becomeyn

userId'sencryptedpasswords

[system boot]create

startup files/etc/profile~/.bash_profile

Type: Ctrl+Alt+F1..F6

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-25Embedded Linux Module, Introduction

the main user interface to the UNIX-Systemreads user input and creates subprocessescan also be used as a simple programming languagethere are many different flavors of shells (sh, ksh, csh, tcsh, bash, ...)compare to command.com on Windows-Systems (CLI)

The Shell

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-26Embedded Linux Module, Introduction

case sensitive↑↓: command line historytab: completes path$ history: show command history $ !N: run command on line N

The Command Line

$ ls -ltr /tmpshell prompt,do notenter this

command (built in or in $PATH)

switches, optionsmodify behaviorof command

argument to the command

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-27Embedded Linux Module, Introduction

Standard I/O and RedirectionA command uses three standard data channels, all associated with the terminal:

standard input (channel #0)standard output (channel #1)standard error output (channel #2)

Redirection operators (< and >) allow you to redirect standard channels from/to files.

Examples: $ ls -al > outfile re-writes the file outfile with the output of command ls.$ ls -al >> outfile appends the output of command ls to the file outfile$ ls -al > /dev/null the output of command ls goes to the bit bucket$ ls -al 2> error the error messages of command ls goes to file error $ mail john < message the text in file message is the input for the mail program

com-mand

stderr

stdout

stdin

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-28Embedded Linux Module, Introduction

Internal, online help system, explains usage of commands and more

Divided into sections:

1 Executable programs or shell commands 2 System calls (functions provided by the kernel) 3 Library calls (functions within system libraries) 4 Special files (usually found in /dev) 5 File formats and conventions eg /etc/passwd 6 Games 7 Macro packages and conventions eg man(7), groff(7). 8 System administration commands (usually only for root) 9 Kernel routines [Non standard]

NAME Name an purpose SYNOPSIS Syntax of command DESCRIPTION Full description, sometimes long OPTIONS List of possible options FILES Specific files for this command SEE ALSO Cross-reference for related info BUGS Shortcomings, warnings AUTHOR author

$ man man #manual page of man$ man -k password #search for pages containing password$ man 2 write #manual page of write system call

General Page structure

Examples:

Start point and last resort – The manual pages

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-29Embedded Linux Module, Introduction

Manual Pages : Example

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-30Embedded Linux Module, Introduction

<Space> or <PgDn>: one page down„b“ or <PgUp>: one page up<CR> or <↓>: one line down„k“ oder <↑>: one line up„h“: Help „q“: quit manual pages

Navigating the Manual Pages

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-31Embedded Linux Module, Introduction

More Commands – list files, file permissionsThe command to list files is 'ls' (Output of: ls -al)

drwx--x--x 57 myf1 staff 4608 Feb 21 14:04 .drwxr-xr-x 787 root other 15360 Jan 13 12:40 ..-rw------- 1 myf1 staff 1701 Aug 3 2000 .Xdefaults-rw------- 1 myf1 staff 120 Mar 14 13:12 .logindrwx------ 4 myf1 staff 512 Feb 21 14:04 .mozilladrwx------ 29 myf1 staff 1024 Feb 17 08:58 PLAYGROUNDdrwx------ 42 myf1 staff 1536 Feb 21 11:05 SANDBOXdrwx------ 16 myf1 staff 512 Mar 27 2001 UNTERRICHTdrwxrwx--- 2 myf1 webteam 512 Feb 21 14:43 WebenabledDevices-rw------- 1 myf1 staff 786 Oct 10 14:46 callog.sep03.tgzlrwxrwxrwx 1 myf1 staff 20 Feb 21 14:41 cluster -> /home/P202-rwx------ 1 myf1 staff 314 Dec 24 10:44 doitdrwxr-xr-x 12 myf1 staff 2048 Jan 17 12:14 public_html

Permissions Links Owner Group Size Date/Time Name

world (others)groupuser (owner)file type

hiddenfiles

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-32Embedded Linux Module, Introduction

File Attribute (1)

Every file has a 16 bit wide attribute called mode field: It contains three parts: file type, special bits, file permissions

<type> <spc> <permissions> r w x r w x r w x

u g s user group others- regular (owner) (world) d directory c character device file <spc>:special bitsb block device file u: setuid bits Unix domain socket g: setgid bit p named pipe s: sticky bitl symbolic link

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-33Embedded Linux Module, Introduction

File Attribute (2)

Interpretation of file permissions

Permission Regular File Directory  

 r:(read) read is allowed ls is allowed

 w:(write)   write is allowed new files can becreated

 x:(execute) file may be directory may beexecuted searched but NOT

listed

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-34Embedded Linux Module, Introduction

chmod <level>+|­|= <permissions> <filename>chown <username> <filename>chgrp <groupname> <filename>

Commands:

drwxr-xr-x 2 student student 1024 2007-10-03 16:00 adir-rw-r--r-- 1 student student 9174 2007-10-03 16:00 afile.txt-rw-r--r-- 1 student student 4096 2007-10-03 16:00 aprog

chmod g+w adir levels u: user ( owner)chmod og-r afile.txt g: groupchmod a=---,u=rwx aprog o: other users (world)

a: all

drwxrwxr-x 2 student student 1024 2007-10-03 16:00 adir-rw------- 1 student student 9174 2007-10-03 16:00 afile.txt-rwx------ 1 student student 4096 2007-10-03 16:00 aprog

More Commands - Change file permission / ownership

Example: (1) Permissions before giving chmod commands

(3) Permissions after you gave chmod commands (2)

(2) Giving chmod commands

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-35Embedded Linux Module, Introduction

Use the 'ps' command to gather information about running processes

student@student-desktop:~$ ps aux

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.3 2912 1844 ? Ss 11:20 0:02 /sbin/initroot 2 0.0 0.0 0 0 ? S 11:20 0:00 [migration/0]root 3 0.0 0.0 0 0 ? SN 11:20 0:00 [ksoftirqd/0]root 4 0.0 0.0 0 0 ? S 11:20 0:00 [watchdog/0]root 5 0.0 0.0 0 0 ? S< 11:20 0:00 [events/0]root 6 0.0 0.0 0 0 ? S< 11:20 0:00 [khelper]root 7 0.0 0.0 0 0 ? S< 11:20 0:00 [kthread]root 30 0.0 0.0 0 0 ? S< 11:20 0:00 [kblockd/0]root 31 0.0 0.0 0 0 ? S< 11:20 0:00 [kacpid]root 32 0.0 0.0 0 0 ? S< 11:20 0:00 [kacpi_notify]root 90 0.0 0.0 0 0 ? S< 11:20 0:00 [kseriod]root 111 0.0 0.0 0 0 ? S 11:20 0:00 [pdflush]

...

More Commands – Processes

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-36Embedded Linux Module, Introduction

Signal Value Default Comment ------------------------------------------------------------------------- SIGHUP 1 Exit Hangup detected on controlling terminal SIGINT 2 Exit Interrupt from keyboard SIGQUIT 3 Core Quit from keyboard SIGILL 4 Core Illegal Instruction SIGABRT 6 Core Abort signal from abort(3) SIGFPE 8 Core Floating point exception SIGKILL 9 Exit Kill signal (Non maskable) SIGSEGV 11 Core Invalid memory reference SIGPIPE 13 Exit Broken pipe: write to pipe with no readers SIGALRM 14 Exit Timer signal from alarm(2) SIGTERM 15 Exit Termination signal (default signal)... and more (see kill -l)

Kill can be be used to send signals (Kind of „software interrupts”) to processes. It is often used to force processes to end execution (Signal # 9)

student@student-desktop:~$ ps PID TTY TIME CMD 5172 pts/1 00:00:00 bash 5190 pts/1 00:45:17 illBehavingProc 5198 pts/1 00:00:00 ps

$ kill 5190 (if it does not help: $kill -9 5190)[1]+ Terminated illBehavingProc

More Commands – kill, send signals to processes

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-37Embedded Linux Module, Introduction

A pipeline is a convenient way to channel the output of one command into the input of another without creating an intermediate file. Pipes allow you to implement the idea of combining many commands to obtain complexer commands.

Consider the following command sequence:$ ps -aux > tempfile$ grep bash tempfile

This sequence can be combined by using a pipe:$ ps -aux | grep bash

grepps -aux stdout stdoutstdin

More Commands – pipes

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-38Embedded Linux Module, Introduction

Access variable var$varExdecute multiple commands in a subshell(cmd_1;cmd_2)Execute multiple commands, command groupscmd_1;cmd_2Execute command cmd in the backgroundcmd &Pipeing commands (output of cmd_1 becomes input of cmd_2)cmd_1 | cmd_2Mask (escape) next character\Redirection standard out and error out to the same filecmd > file 2>&1Redirection standard error output of cmd to filecmd 2> fileRedirection standard input for cmd from file (rarely used)cmd < fileAppend standard output of cmd to filecmd >> fileRedirection of standard output of cmd into filecmd > fileExclusion of a character (example: character without number)[!0-9]Selection of a character in a file name ( example: a, b or c)[abc]Any single character in a file name?Any number of characters in a filename, including none*Home-Directory

Shell meta characters

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-39Embedded Linux Module, Introduction

Lab #1 – A learning exercise (1)

(1) Plug-in your USB Lab work disk and start your Ubuntu-14.04(2) Log into the graphical Desktop.(3) Start one or more console windows (using the icon with the shell at

the bottom of the screen). Now you have a command line interpreter and you can begin to explore UNIX by entering the commands below and observing the results.

(4) Work through the Labs on the moodle page for session-1Lab1-1:Basic Terminal ManipulationsLab1-2: Basic File Manipulations

(5) Don't forget to take notes: Assemble your own “Quick guide to Unix” with the most frequently used commands with their most important options.

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-40Embedded Linux Module, Introduction

Lab #1 Reflections

Now you've learned the most basic UNIX-Commands and made some experiences with Knoppix

pwd, cp, ls, mkdir, rmdir, touch, mv, rm, more, tail, >, >>Hint: Command line history: <Arrow-Key>Hint: Last Argument from previous command: <Esc> .

Hint: Use meta characters in shell: * Any number of characters? Exactly one (but arbitrary) characterHint: Use ; to separate several command on one lineHint: The directory you're in after the login is your home directory (use 'cd' to change to)$HOME (Environment variable) or ~ can be used to reference the home directory

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-41Embedded Linux Module, Introduction

More Commands – findfind is a very useful command for finding particular groups of files. find descends

the directory tree beginning at the pathname(s) given as first parameter(s) and locates all files that meet the specified condition. All matching files are treated according to the command given on the command line. Look up the manual page to find more about this useful tool.

List all files named “chapter1” in the /work directory$ find /work -name chapter1 -print

List all files named “memo” and belong to user “ann” in the directories /work and /home$ find /work /home -name memo -user ann -print

Remove all empty files on the system (prompting first)$ find / -size 0 -ok rm {} \;

List all files which were modified within the last two days$ find / -mtime -2 -print

Examples:

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-42Embedded Linux Module, Introduction

More Commands – xargsxargs reads items from the standard input, delimited by blanks or newlines, and

executes the command (default is /bin/echo) one or more times with any initial­arguments followed by items read from standard input. Blank lines on the standard input are ignored.

Search all files *.txt in the current directory (and below) that contain the word (-w) login and display their file names (-l)

$ find . ­name \*.txt | xargs grep ­w ­l login

Find all files *.conf in the current directory (and below) and display a long directory listing

$ find . ­name “*.conf” | xargs ls ­l

Remove all empty files on the system using xargs (do not ask)

$ find / ­size 0 | xargs rm

Examples:

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-43Embedded Linux Module, Introduction

More Commands – ln

The Linux file system allows you to link two files in one of two ways: hard links (in the same file system), soft or symbolic links.Usage

$ ln [­s] target [link­name]where

target is the path to the existing filelink­name is the path of the link­s a symbolic link is created

Example$ls ­li1109926 ­rw­r­­r­­ 1 myf1 myf1 2006­08­16 summer1109927 ­rw­r­­r­­ 1 myf1 myf1 2006­08­16 winter$ln summer season­hard; ln ­s summer season­soft; ls ­li1109926 ­rw­r­­r­­ 2 myf1 myf1 2006­08­16 season­hard1109929 lrwxrwxrwx 1 myf1 myf1 2006­08­16 season­soft ­> summer

inode

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-44Embedded Linux Module, Introduction

More Commands – grep (1)

grep is a handy utility to scan a file and filter out all of the lines that do contain a specified pattern. There are a few variants like egrep (grep -E), fgrep (grep -F), rgrep (grep -r).

Usage: $grep [options] pattern [file­name­list]$grep [options] [­e pattern |­f file­name][file­name­

list]Most frequently used options

e: useful if pattern begins with -E: extended regular expression (same as egrep)f file: obtain patterns from FILE, one per line.

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-45Embedded Linux Module, Introduction

More Commands – grep (2)

Usage: $grep [options] pattern [file­name­list]$grep [options] [­e pattern |­f file­name][file­name­

list]

Most frequently used options, cont.F: interpret pattern as a list of fixed strings, separated by new lines, any of which is to be matchedi: ignore case distinctions in both the pattern and the input files.n: prefix each line of output with the line number within its input filer,R: read all files under each directory, recursivelyv: invert the sense of matching, to select non-matching linesw: select only those lines containing matches that form whole wordsx: select only those matches that exactly match the whole line

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-46Embedded Linux Module, Introduction

Examples – grep (3)

$ cat grepfileWell you know it's your bedtime,So turn off the lightSay all your prayers and then,Sleep well, dream of wonderful things,Mermaids will swim through the sea,And you will be swimming there too.

Search for the pattern “the”

$ grep the grepfileSo turn off the lightSay all your prayers and then,Mermaids will swim through the sea,And you will be swimming there too.

Search for the word pattern “the”display line numbers

$ grep -wn the grepfile2:So turn off the light5:Mermaids will swim through the sea,

grep Examples: grep Examples, cont

Search for the word pattern “well”

$ grep -wn well grepfile4:Sleep well, dream of wonderful things,

Search for the word pattern “well”ignore case

$ grep -wn -i well grepfile1:Well you know it's your bedtime,4:Sleep well and dream of wonderful things,

Search for the pattern “the”ignore case and inverse the filter

$ grep -nv -i the grepfile1:Well you know it's your bedtime,4:Sleep well and dream of wonderful things,

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-47Embedded Linux Module, Introduction

More Commands – tar tar is used to copy files to or restore from an archive (similar to a zip-File in the

Windows world). The archive can be a tape or a file. Entire subtrees can be archived.

Examples:

Create an uncompressed archive of the work directory (recursively)

$ tar cvf myarchive.tar work (c=create, v=verbose, f=file)

Show the contents of the archive

$ tar tvf myarchive.tar (t=table of content)

Extract the archive (Directory tree is reconstructed)

$ tar xvf myarchive.tar (x=extract)

Create a compressed archive (gzip) of the work directory

$ tar cvzf myarchive.tar.gz work (z=zipped)$ tar cvjf myarchive.tar.bz2 work (j=bz2 zipped, more compressed)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-48Embedded Linux Module, Introduction

Very Simple Shell Scripting (1)The shell can also be used as a simple scripting language to automate repetitive tasks (Similar to *.bat files in MS-DOS). The first line of a shell script determines the program which is used to execute the commands.

$ cat simple.sh

#!/bin/sh#a very simple shell scriptecho “this script executes the command: pwd”pwd

Example:

#! is called “sha-bang”, indicates that this file contains a shell script and specifies the interpreter

This line is a comment

This script can be executed in a shell in several ways

(1) Executing the script simple.sh in the same shell

myf1@sydney:$ source simple.shthis script simply executes: pwd/home/myf1/shell­scripts

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-49Embedded Linux Module, Introduction

Very Simple Shell Scripting (2)

myf1@sydney:$ sh simple.shthis script simply executes: pwd/home/myf1/shell­scripts

(3) Executing the script simple.sh in a sub-shell, if the x permission is set: (-rwxr­xr­x)

myf1@sydney:$ ./simple.shthis script simply executes: pwd/home/myf1/shell­scripts

(4) Executing the script simple.sh in a sub-shell, and in the background

myf1@sydney:$ ./simple.sh & this script simply executes: pwd/home/myf1/shell­scripts

(2) Executing the script simple.sh in a sub-shell, if the x permission is not set: (-rw­r­­r­­)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-50Embedded Linux Module, Introduction

Position Parameters in Shell Scripts

You can call a shell scripts with arguments. They are assigned to the positional parameters, starting with $0 (the name of the script), $1 to the first argument, $2 to the second, etc. A parameter stores values that can be a name, a number, a string, etc. $# gives the number of arguments (except $0). $* expands to a string of all arguments. $@ expands to a list of separate arguments withoutinterpretation or expansion

File: parameter.sh         (permissions: ­rwxr­xr­x)

#!/usr/bin/shecho command is    : $0echo parameters are: $1 $2 $3 $4 $5 $6 $7 $8 $9

$ ./parameters.sh hello good morninggivescommand is: ./parameter.shparameters are: hello good morning

Example:

$0 $1 $3$2

$4..$9 are empty stringsin this example

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-51Embedded Linux Module, Introduction

Interactive Input in Shell ScriptsThe read command allows your shell script to accept input from the user via standard input and to store the input line to one or more variables. read variable [variable ...]

Example:

File: read1.sh         (permissions: ­rwxr­xr­x)myf1@sydney:$ cat read1.sh #!/bin/bash# simple read exampleIFS=','echo “enter three parameters, comma separated:”read p1 p2 p3echo "you entered: $p1 $p2 $p3"

$myf1@sydney:$ ./read1.shenter three parameters, comma separated:first par,par2,third paryou entered: first par par2 third par

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-52Embedded Linux Module, Introduction

More Commands on a Single Line

The shell allows you to enter several commands on a single line in different ways– Sequence: cmd1 ; cmd2 ; cmd3; and so on– Sequential AND: cmd1 && cmd2 cmd2 is executed only if cmd1

was successfully executed before– Sequential OR: cmd1 || cmd2

cmd2 is executed only if cmd1was NOT successfully executed

Example:

myf1@sydney:$ make && make install 

If the make run fails nothing is installed

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-53Embedded Linux Module, Introduction

Control Structures: test command (1)The test command returns a true status if the condition holds and a false status otherwise.

Syntax $ test <condition> or $ [ <condition> ] Conditions for tests are:

-e <file> : true if file exists-r <file> : true if file exists and is readable-w <file> : true if file exists and is writable-x <file> : true if file exists and is executable-f <file> : true if file exists and is a regular file-d <file> : true if file exists and is a directory-L <file> : true if file exists and is a symbolic link-s <file> : true if file exists and is has a length greater than zero-n <string> : true if the string is not empty-z <string> : true if the string is empty<string1> = <string2> : true if strings are equal (spaces around =)<string1> != <string2> : true if strings are different

Spaces needed

resolves to /usr/bin/[ 

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-54Embedded Linux Module, Introduction

Control Structures: test cmd, cont. (2)

The test command ($test condition) returns a true status if the condition holds and a false status otherwise. Conditions for tests are:

-eq : equal Example: $# ­eq 0

-ne : equal Example: $# ­ne 0

-ge : greater or equal Example: $x1 ­ge $x2

-gt : greater than Example: $x1 ­gt $x2

-le : less or equal Example: $x1 ­le $x2

-lt : less than Example: $x1 ­lt $x2

AND : <cond1> -a <cond2>

OR : <cond1> -o <cond2>

NOT : !<expression>

Parantheses : \( <expresion> \)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-55Embedded Linux Module, Introduction

Control Structures: if ... then (3)The syntax of the if .. then control structure is

if test-command 

then

commands

fi

Example:

myf1@sydney:$ cat iftest.shecho ­n “word 1:” ; read word1echo ­n “word 2:” ; read word2if test $word1 = $word2

thenecho “Match”

fi

myf1@sydney:$ ./iftest.sh word 1:appleword 2:peachmyf1@sydney:$

myf1@sydney:$ ./iftest.sh word 1:appleword 2:appleMatchmyf1@sydney:$

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-56Embedded Linux Module, Introduction

Control Structures: if ... then ... else (4)The syntax of the if ... then ... else control structure is

if test-command; then

commands

else

commands

fi

Example:myf1@sydney:$ cat out.sh#!/bin/bashif test $# ­eq 0; then   echo "usage: out [­v] files..." 1>&2

exit 1fiif [ "$1" = "­v" ]; then

shiftless ­­ "$@"

    elsecat ­­ "$@" 

fi

myf1@sydney:$ ./out.sh file1 file2 first line

second linemyf1@sydney:$

myf1@sydney:$ ./out.shusage: out [­v] files...

myf1@sydney:$ cat file1first linemyf1@sydney:$ cat file2second line

Result of the Example:

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-57Embedded Linux Module, Introduction

Control Structures: case (5)

The syntax of the case control structure iscase test-string in

pattern1)

commands1

;;

pattern2)

commands2;;

...

*)

default-commands

;;

esac

myf1@sydney:$ cat case1.sh#!/bin/bash# case testecho ­n "Enter a,b,c:"read valcase $val in    a)

echo "you entered a";;

    b)echo "you entered b";;

    c)echo "you entered c";;

    *)echo "you entered something else";;

esac

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-58Embedded Linux Module, Introduction

Control Structures: for ... in (6)The syntax of the for ... in control structure is

for loopindex in argument-list

do

commands

done

myf1@sydney:$ cat for1.sh#!/bin/bash# for testecho ­n "enter fruits:"read fruitsfor fruit in $fruitsdo

echo “$fruit”doneecho “done”

myf1@sydney:$ ./for1.sh enter fruits:apple banana oranges“apple”“banana”“oranges”“done”myf1@sydney:$

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-59Embedded Linux Module, Introduction

Control Structures: for ... in (7)#!/bin/sh#simple backup script:# ­ rename all .bk files to .bk­1 files# ­ copy all .c files to .bk files# ­ show all *.c files# all errors are suppressed

for file in *.bkdo

mv $file $file­1 2>/dev/nulldone

for file in *.cdo

cp $file $file.bk 2>/dev/nulldone

ls *.c 2>/dev/null

Example-2

ignore output

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-60Embedded Linux Module, Introduction

Control Structures: while (8)The syntax of the while control structure is

while test-command

do

commands

done

myf1@sydney:~$ cat while1.sh#!/bin/bash# while testecho ­n "enter number:" ; read valsum=0while [ $val ­gt 0 ]do

echo ­n "$val "let sum=$sum+$vallet val=$val­1

doneecho “the sum is: $sum”

myf1@sydney:~$ ./while1.sh enter number:1010 9 8 7 6 5 4 3 2 1 “the sum is: 55”myf1@sydney:$

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-61Embedded Linux Module, Introduction

Shell Builtin Commands (1)

exit [n]Cause the shell to exit with a status of n. If n is omitted, the exit status is that of the last command executed. A trap on EXIT is executed before the shell terminates.

exec [­cl] [command [arguments]]If command is specified, it replaces the shell process. No new process is created. The arguments become the arguments to command. If you use exec in a foreground shell then this shell is terminated after having executed the command. Thus, exec is useful in shell scripts.

If command is not specified then the exec builtin is used to redirect standard input, output, error from within a shell script.exec < infile : from now on, all the input comes from file infile.

exec > outfile 2> errfile : from now on, all the output goes to file outfile, and the error messages go to file errfile.

­c causes command to be executed with an empty environment.

­l the shell places a dash at the beginning of the zeroth arg passed to command.

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-62Embedded Linux Module, Introduction

Shell Builtin Commands (2)

export [name[=word]] ...The supplied names are marked for automatic export to the environment of subsequently executed commands. If a variable name is followed by =word, the value of the variable is set to word.

eval [arg...]The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0.

trap [[arg] sigspec ...]The command arg is to be read and executed when the shell receives signal(s) sigspec. If arg is absent each specified signal is reset to its original disposition (the value it had upon entrance to the shell). If arg is the null string the signal specified by each sigspec is ignored by the shell and by the commands it invokes.

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-63Embedded Linux Module, Introduction

Shell FunctionsThe syntax of a shell function is

function­name()

{

commands}

Example: search for all commands containing the pattern svn in the command history

myf1@sydney:~$ cat func1.sh#!/bin/bash# shell functioncmd(){    echo “in shell fct cmd()”    history | grep ­i $1}cmd $1

myf1@sydney:~$ source func1.sh svnin shell fct cmd()  104  svn status  105  svn add www/slides/Prog.pdf   108  svn commit ­m ""

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-64Embedded Linux Module, Introduction

Log out and Shutdown

When you are using a graphical desktop like KDE or GnomeKlick on the K-Menu button (KDE) and select “ Log Out”The KDE System shows you the “End Session Menu”. It allows you

to end your current sessionto turn off your computerto restart your computerto cancel your log out selection

When you are using a terminal (on the command line)to end your session (shell), type

$ exitto shut down your computer, type (as root)

# shutdown ­h nowto reboot your computer, type (as root)

# reboot   or   # shutdown ­r now

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-65Embedded Linux Module, Introduction

Networking Environment of a Single Homed Host

Networking Options

● Network Address● Subnet Mask ● Broadcast Address ● Domain Name● Host Name● Lease Duration

Networking Options

● Network Address● Subnet Mask ● Broadcast Address ● Domain Name● Host Name● Lease Duration

Networking Options

● Network Address● Subnet Mask ● Broadcast Address ● Domain Name● Host Name● Lease Duration

Networking Options

● Network Address● Subnet Mask ● Broadcast Address ● Domain Name● Host Name● Lease Duration

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-66Embedded Linux Module, Introduction

Network Access

Linux uses device driver software to enable access between the kernel and peripheral devices such as network interfaces.Drivers are either compiled into the kernel software or are available in the form of kernel loadable modules.Your system is configured this way: When you boot, the DHCP supplies your computer with useful information to configure the network interface(s).You may find network characteristic information in the boot log by using the dmesg utility.

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-67Embedded Linux Module, Introduction

Configuration of Network Interfaces (1)

Use the ifconfig program (in directory /sbin). Notice that configurations will be lost when you re-boot your machine. The program can be used in two ways

ifconfig [­a] interfaceifconfig interface [aftype] options | address

The first case is used to display the current configuration (you need not be root)

Example1: An unconfigured interface$/sbin/ifconfig eth0      Link encap:Ethernet  Hwaddr 00:08:74:E2:F4:96          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:0 errors:0 dropped:0 overruns:0 frame:0          TX packets:2 errors:0 dropped:0 overruns:0 carrier:2          collisions:0 txqueuelen:100          RX bytes:0 (0.0 b)  TX bytes:684 (684.0 b)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-68Embedded Linux Module, Introduction

Configuration of Network Interfaces (2)

Example2: The loopback interface$ /sbin/ifconfig lolo       Link encap:Local Loopback         inet addr:127.0.0.1  Mask:255.0.0.0         UP LOOPBACK RUNNING  MTU:16436  Metric:1         RX packets:58 errors:0 dropped:0 overruns:0 frame:0         TX packets:58 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0          RX bytes:4020 (3.9 KiB)  TX bytes:4020 (3.9 KiB)

● It uses the address 127.0.0.1● Traffic sent to the loopback address never appears on any

attached network.● Can be used to test client/server based application in absence

of a physical network.

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-69Embedded Linux Module, Introduction

Configuration of Network Interfaces (3)

Example 3: Display configuration of all interfaces  (lo omitted)$ /sbin/ifconfig ­a

eth0 Link encap:Ethernet Hwaddr 00:08:74:e2:f4:96inet addr:147.87.70.112 Bcast:147.87.70.255 Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets: 34565  errors:0 dropped:0 overruns:0 frame:0TX packets: 123543 errors:0 dropped:0 overruns:0 carrier:0collisions:342 txqueuelen:100Interrupt:11 Base address:0xec80

ppp0   Link encap:Point­to­Point Protocol       inet addr:147.87.69.7  P­t­P:147.87.66.249 Mask:255.255.255.255       UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1       RX packets:11 errors:0 dropped:0 overruns:0 frame:       TX packets:9 errors:0 dropped:0 overruns:0 carrier:0       collisions:0 txqueuelen:3       RX bytes:113 (113.0 b)  TX bytes:97 (97.0 b)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-70Embedded Linux Module, Introduction

Configuration of Network Interfaces (4)#ifconfig interface [aftype] options |address  (as root)

frequently used optionsup  causes the interface to be activateddown  causes the interface to shut down [­]arp enable/disable the ARP protocol[­]promisc enable/disable promiscuous mode

(make interface accept ALL packets)

mtu N set the Maximum Transfer Unit to N[inet] addr set the interface address to addrnetmask mask set the netmask to mask [­]broadcast [addr] enable/disable broadcast. If 

  broadcast is on, addr is used. 

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-71Embedded Linux Module, Introduction

Ping - A Network Diagnostic Utility (1)

One of the most popular diagnostic utilities: the ping program

sydney$ ping www.whitehouse.govPING a1289.g.akamai.net (195.176.255.135): 56 data bytes64 bytes from 195.176.255.135: icmp_seq=0 ttl=247 time=10.0 ms64 bytes from 195.176.255.135: icmp_seq=1 ttl=247 time=11.4 ms64 bytes from 195.176.255.135: icmp_seq=2 ttl=247 time=60.9 ms

­­­ a1289.g.akamai.net ping statistics ­­­3 packets transmitted, 3 packets received, 0% packet lossround­trip min/avg/max = 10.0/27.4/60.9 ms

The output of the ping program has three section● Beginning with PING: overview of the command● Beginning with 64 bytes: the result of a ping response showing

a sequence number, the time to live, and the round trip time● The ping statistics

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-72Embedded Linux Module, Introduction

Ping - A Network Diagnostic Utility (2)

Syntax of the ping command

ping  [­dfnqrvR] [­c count] [­i wait] [­l preload] [­p pattern][­s packetsize]

­c count stop after sending and receiving count packets­f flood: sends packets as fast as possible (must be root)­i wait sets an interval of wait seconds between two packets­I device sets the output interface on multi­homed host­n  numeric only, no host name resolution­R  sets the record route option­s pktsize sets the number of data bytes to pktsize (must be root)

Example:  #ping ­n ­c 5 ­s 1200 147.87.64.15

Typical options of the ping command

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-73Embedded Linux Module, Introduction

Hostname and more Sophisticated Interface ConfigHostname

File: /etc/hostname contains the host nameScript: /etc/init.d/hostname.sh sets the host name

Automatic Network Configuration during StartupFile: /etc/network/interfaces consists of zero or more auto and iface stanzas, each of which defines

one network interface. Examples:

Script: /etc/init.d/networking

auto lo eth0iface lo inet loopbackiface eth0 inet dhcpiface eth1 inet static

address 192.168.1.1   netmask 255.255.255.0                  

                  

Interface eth1 is not configured automatically at startup by

network script.Use the ifup/ifdown

commands to configure/de-configure eth1 manually

Interfaces configured

automaticallyat startup

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-74Embedded Linux Module, Introduction

Netstat – Getting network statistics (1/2)

The netstat command allows you to obtain network informationTypical usage scenarios are:

Inspecting NW interface configurationMonitoring connectionsListing services that are listening for network trafficLooking at routing table Viewing statistical information about protocols

In the following we look at each of these usage scenarios

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-75Embedded Linux Module, Introduction

Netstat – Getting network statistics (2/2)

Inspecting NW interface configurationnetstat ­i (netstat ­ie is the same as ifconfig)

Monitoring connections (t: TCP u: UDP)netstat ­t, netstat ­u, netstat ­ta, etc.

Listing services that are listening for network traffic: netstat ­p

Looking at routing table: netstat ­r (same as route ­n)

Viewing statistical information about protocols: netstat ­s

Remark: Use ­n option to switch off name resolution in the output – especially useful, when DNS is not working.

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-76Embedded Linux Module, Introduction

Configure Client Name Resolution using DNS

Situation: Assume that there is a DNS server running

DNS clientDNS server1

ResolverProcess

DNSServer

Process

DNS server2

DNSServer

Process

Sample /etc/resolv.conf filesearch netlab.bfh.ch. bfh.ch.nameserver 147.87.250.20nameserver 84.244.3.201

Search List

Primary DNS Server

Secondary DNS Server

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-77Embedded Linux Module, Introduction

The Linux Network Manager (1)

The NetworkManager is a software to dynamic network control and configuration. It attempts to keep network devices and connections up and active when they are available.The NetworkManager can be used to configure the following types of connections: Ethernet, wireless, mobile broadband (such as cellular 3G), and DSL and PPPoE (Point-to-Point over Ethernet), etcThe NetworkManager tries to read and write your configuration files through plugins called "system settings plugins". These plugins allow you to use the file formats you're already comfortable with and the tools you already know how to use to manage your network configuration.These plugins are configured through the /etc/NetworkManager/NetworkManager.conf configuration file.

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-78Embedded Linux Module, Introduction

The Linux Network Manager (2)

You can read more details of these plugins in the NetworkManager.conf manual pages. Type:

man NetworkManager.conf

To enable/disable a plugin, add or remove it from the "plugins" line of /etc/NetworkManager/NetworkManager.conf

[main]plugins=ifupdown

This plugin is used on the Debian and Ubuntu distributions, and reads connections from /etc/network/interfaces.

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-79Embedded Linux Module, Introduction

The Linux Network Manager (3)

The NetworkManager does a good job for wireless connections. However, for wired test networks used to connect a target system the NetworkManager may take wrong decisions and shuts down a device unexpectedly.Unmanaged Devices is a solution.All ifupdown-specific options go in a "[ifupdown]" section. If the "managed" key is set to "false", then any device listed in /etc/network/interfaces will be completely ignored by NetworkManager. Thus, first make sure that the following lines are present in file /etc/NetworkManager/NetworkManager.conf:

[main]plugins=ifupdown   (maybe more plugins)[ifupdown]managed=false

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-80Embedded Linux Module, Introduction

The Linux Network Manager (4)

Second, the file /etc/network/interfaces needs the following lines:

auto loiface lo inet loopback# manual configuration for test network# that connects the BBB capeauto eth1iface eth1 inet static

address 192.168.2.1netmask 255.255.255.0

# enable IP forwarding and NAT (later)Third, restart the NetworkManager (as root)

# service NetworkManager restart

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-81Embedded Linux Module, Introduction

Components of an Embedded Linux Systems

Boot Loader (U-boot: Universal boot loader)Supports various embedded platforms such as

ARM (ARM7, ARM9, Xscale, Omap …)Power PC, MIPS (4Kc, 5Kc) ...

Boots Linux and passes Parameters to the kernelFirmware update via Serial Line or EthernetCan be controlled by scripts

KernelStill some 2.6 but mostly 3.x and now 4.x

File System (rootfs)with daemons (also known as 'services'), tools and applications.in Unix Systems, almost everything is a file.

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-82Embedded Linux Module, Introduction

The role of the Boot Loader (1)

The boot loader normally comes with the board as part of the board support package (BSP).After power-on/reset the internal boot-up sequence (first stage) is executed and loads the second boot loader MLO.MLO loads U-Boot and executes it.Next, the boot loader goes through the following hardware initialization steps:

Configure the CPU speedInitialize memory: RAM, RegistersTurn on cachesSet up a serial port to communicate with the console.Do some hardware diagnostics (Power On Self Test)

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-83Embedded Linux Module, Introduction

The role of the Boot Loader (2)

Boot loader steps, cont.Locates and Downloads the kernel image, normally from system flash or by TFTP. Decompresses the kernel.Downloads and initializes the RAM disk (if there)Set up arguments:

There is a generic way to pass arguments to the kernel like root (root fs), ethernet (Ethernet if), console (select serial port for the console) etc.

Jump to the kernel entry point

Bern University of Applied SciencesEngineering and Information Technology

V2.8 © 2015 by Franz Meyer and Martin Aebersold Slide-84Embedded Linux Module, Introduction

Kernel start

Turn on MMU, setup kernel stack and BSS segment Initialize interrupts (assign default handlers) Initialize timers, calibrate for busy waiting loopInitialization of subsystems like scheduler, memory manager, virtual file system and real file system softwareInitialization of basic block and char device drivers.Mounting the root file system (rootfs)Remove initial use-and-throw code (__init) and (__initdata).Create the first kernel thread and jump to the init process