+ All Categories
Home > Documents > LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU...

LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU...

Date post: 19-Jan-2020
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
86
LINUX EXERCISE Jo, Heeseung
Transcript
Page 1: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

LINUX EXERCISE

Jo, Heeseung

Page 2: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

2

Download Programs

Download VMWare player

• http://www.vmware.com > download > workstation player

Download Ubuntu iso file

• http://ubuntu.com

- server, 64bit version

• http://cslab.cbnu.ac.kr/_down/ubuntu-18.04.2-live-server-amd64.iso

• http://mirror.kakao.com/ubuntu-releases/18.04/ubuntu-18.04.3-live-server-amd64.iso

Page 3: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

3

실습: Vmware 설치

실습목표

• Vmware를 설치

결과 화면

Page 4: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

4

가상머신의 겉모양

가상머신이 생성된 화면

Page 5: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

5

실습: 가상머신 생성

실습목표

• 앞으로 계속 사용할 가상머신을 생성

가상머신이 생성된 결과화면

네트워크는 반드시"Vmnet8(NAT)"로 지정

Page 6: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

7

실습: Linux ISO 파일 다운로드

실습목표

• 인터넷에서 Linux 설치용 ISO 파일을 다운로드

다운로드 사이트 예

• http://www.kernel.org

• http://mirrors.kernel.org

• http://fedoraproject.org/

• http://www.centos.org/

• http://www.ubuntu.com/

Page 7: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

8

실습: ISO 파일 설치

실습목표

• 물리적인 CD/DVD 대신에 ISO파일을 사용

가상머신에서 CD/DVD 대신 ISO 파일 사용 화면

Page 8: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

9

실습: Ubuntu 설치

실습목표

• 기본적인 설치를 진행

• Ubuntu 18.04 live server amd64 버전 설치

• 로그인 후 인터넷이 되는지 확인

• 설치 시 한글로 설치하지 말 것 (영어 사용)

• 기본 설정

- Processors: 4

- Memory: 2GB

- Storage: 40GB (single file)

- Network Adapter: NAT

Page 9: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

EXTRA

Jo, Heeseung

Page 10: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

11

가상머신(Virtual Machine)의 소개

지금 쓰는 Windows를 그대로 사용하면서도 여러 대의 리눅스 서버를운영하는 효과를 내는 프로그램

1대의 PC에서 추가로 3개의 가상머신을 구동한 화면

Page 11: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

12

Virtual Machines

Host computer emulates guest operating system and machine resources

• Improved isolation of multiple guests

• Avoids security and reliability problems

• Aids sharing of resources

Virtualization has some performance impact

• Feasible with modern high-performance computers

Examples

• IBM VM/370 (1970s technology!)

• VMWare

• Xen

• Linux KVM

• Microsoft Hyper-V

Page 12: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

13

Consolidation using Virtualization

Server consolidation

• Multiple OSes in a physical machine

Hardware

Virtual Machine Monitor

Windows

App App

App

Linux

App App

App

Linux

App App

Page 13: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

14

Virtualization Benefits

High machine utilization

Low ownership cost

Live migration, Easy management

Hardware

Windows

App App

App

Hardware

Windows

App App

App

Hardware

Windows

App App

App

Hardware

Windows

App App

App

Hardware

Windows

App App

App

Hardware

Windows

App App

App

Hardware

Windows

App App

App

Hardware

Windows

App App

App

Hardware

Windows

App App

App

Hardware

Virtual Machine Monitor

Page 14: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

15

Software Architecture

OS

Hardware

Application

Application

Application

Application

Application

Application

Physical

CPU

Physical

Memory

Physical

Network

Physical

Block

Device

Application

Application

Application

Application

Application

Application

Page 15: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

16

Virtualization Architecture

VMMDomain0

Control

Interface

Hardware

Guest OS

Domain0

Control

Software

Guest OS

DomainU

Application

Guest OS

DomainU

Guest OS

DomainU

Virtual

CPU

Virtual

Memory

Virtual

Network

Virtual

Block

Device

Application

Application

Application

Application

Application

Physical

CPU

Physical

Memory

Physical

Network

Physical

Block

Device

Page 16: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

17

가상머신과 가상머신 프로그램의 개념 (1)

가상머신 모니터 (Virtual Machine Monitor, aka Hypervisor)

• 컴퓨터에 설치된 운영체제(호스트OS)안에 가상의 컴퓨터를 만들고, 그안에 또 다른 운영체제(게스트OS)를 설치/운영할 수 있도록 제작된프로그램

Host OS vs. 가상머신(VM) vs. Guest OS

• Host OS: PC에 이미 설치되어 있는 Windows

• 가상머신: Host OS위에서 동작하는 가상의 PC

• Guest OS: 가상머신에서 동작하는 운영체제

멀티부팅(Multi-Booting)과는 개념이 다름

Page 17: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

18

가상머신과 가상머신 프로그램의 개념 (2)

1대 PC에1개 OS

1대 PC에4개 OS

Page 18: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

19

Vmware 핫 키

호스트OS와 게스트 OS 사이의 초점이동 기본 키

• Ctrl + Alt

Page 20: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

21

GNU 프로젝트

Richard Stallman

• 1984년 GNU 프로젝트가 시작

• 목표는 '모두가 공유할 수 있는 소프트웨어'를만드는 것

• 1985년 자유 소프트웨어 재단(FSF, Free Software Foundation) 설립

FSF

• GNU 프로젝트에서 제작한 소프트웨어를 지원함으로써 컴퓨터 프로그램의복제, 변경, 소스 코드의 사용에 대한 제한을 철폐

• GPL(General Public License)을 따름

- GPL은 자유 소프트웨어(Free Software)의 수정과 공유의 자유를 보장

• 프리웨어(Freeware, 무료 소프트웨어)라는 개념을 뛰어넘어서 진정한자유(Freedom)에 대한 개념

• 자유 소프트웨어는 심지어 무료로 얻은 소프트웨어를 유상으로 판매할자유도 보장

Page 21: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

22

커널

http://www.kernel.org 에서 최신버전을 다운로드

커널 변천사

커널 버전의 의미 (예: linux-2.6.30.tar.bz2 )

• 2는 주 버전 (Major Version)

• 6은 부 버전(Minor Version)

• 30은 패치 버전(Patch Version)

• Minor version이 홀수: 개발용 테스트 버전

• Minor version이 짝수: 안정 버전

배포판에 포함된 기본 커널을 사용자가 직접 최신의 커널로업그레이드할 수 있음 (커널 업그레이드)

• https://www.kernel.org

Page 22: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

23

레드햇 리눅스와 페도라 리눅스 (1)

Red Hat

• 전 세계적으로 가장 유명한 배포판

• 레드햇(Red Hat)사에서 제작한 '레드햇 리눅스(Red Hat Linux)'

Red Hat Enterprise Linux vs. Fedora Linux

• 상용으로 판매되는 레드햇 엔터프라이즈 리눅스(Red Hat Enterprise Linux)레드햇사에서 지원하여 무료로 배포되는 페도라 리눅스(Fedora Linux)

• 무료인 페도라 리눅스는 상용인 레드햇 엔터프라이즈 리눅스에 포함될새로운 기술을 미리 시험하기 위한 용도로도 사용

Page 23: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

24

레드햇 리눅스와 페도라 리눅스 (2)

레드햇 리눅스와 페도라 리눅스의 발전

~~~ 중간 생략 ~~~

Page 24: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

25

Linux의 종류는 배포판의 차이

Linux 커널

• http://www.kernel.org

• http://mirrors.kernel.org

Linux 배포판

• http://www.ubuntu.com/

• http://fedoraproject.org/

• http://www.centos.org/

Page 25: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

BASIC COMMANDS

Page 26: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

27

Linux 서버 접속 Protocol

Shell

• telnet: TCP port 23

- Deprecated in most systems

• ssh: TCP port 22

File

• ftp: TCP port 21

• ssh(sftp, scp): TCP port 22

Page 27: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

28

필요 package 설치

apt-get update

• root 권한 필요

• sudo apt update

• sudo apt -y install vim

• sudo apt -y openssh-server

Page 28: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

29

Linux 접속

Client

• telnet, ssh, ftp, ...

• putty, secureCRT, mlterm, ...

• WinSCP, secureFX, ...

Page 29: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

30

Linux 접속

putty with ssh

• Host:192.168.XXX.XXX

• port: 22

• connection type: SSH

Linux IP addr. 확인

• ifconfig [엔터]

Page 30: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

31

Linux 접속

Login and shell

• ID: IDID

• PW: XXXX

• 항상 root 계정으로 변경해서 작업할 것

- 로그인후 sudo su -

Page 31: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

32

Linux 접속

Password 변경

• passwd 명령

Page 32: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

33

Shell

Shell?

• 운영체제 상에서 다양한 운영체제 기능과 서비스를 구현하는인터페이스를 제공하는 프로그램

BASH: GNU Bourne Again Shell

• 가장 많이 쓰이며, 가장 범용적으로 사용됨

• 거의 모든 리눅스 배포판의 기본 shell

CSH: C 스타일 문법을 가지는 쉘, BSD 시스템 표준

• Bourne sheell의 확장

• UC berkeley에서 처음 개발

KSH: 실제 AT&T 버전의 Korn shell

• 어휘의 스코프, 합성 변수, 결합 배열, 이름 참조 그리고 부동 소수점연산등이 새로운 기능

Page 33: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

34

기본 명령

shell commands의 규칙

• command -option xxx

• 빈칸이 구분자로 사용됨

- 빈칸을 명령에 포함하려면 " 또는 ' 를 사용

- ex.

ls file name

ls "file name"

• 도움말

- command --help

- ex. ls --help

Page 34: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

35

기본 명령

로그인/로그아웃

명령 기능 주요 옵션 예제

ssh 유닉스시스템에 접속 - ssh iter2.jbnu.ac.kr

logout 유닉스시스템에서 접속

해제

- logout

exit - exit

passwd 암호변경 - passwd

Page 35: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

36

기본 명령

파일/디렉토리 조작 명령

명령 기능 주요 옵션 예제

pwd 현재 디렉토리 경로 출력 - pwd

ls 디렉토리 내용 출력-a : 숨김파일출력-l : 파일 상세정보 출력-F : 종류 구분

ls -a /tmpls -l

cd 현재 디렉토리 변경 -cd /tmpcd ~han01

cp 파일/디렉토리 복사 -r : 디렉토리 복사cp a.txt b.txtcp -r dir1 dir2

mv파일/디렉토리 이름변경과이동

-mv a.txt b.txtmv a.txt dir1mv dir1 dir2

rm 파일/디렉토리 삭제 -r : 디렉토리 삭제rm a.txtrm -r dir1

Page 36: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

37

기본 명령

파일/디렉토리 조작 명령

명령 기능 주요 옵션 예제

mkdir 디렉토리 생성 - mkdir dir1

rmdir 빈 디렉토리 삭제 - mkdir dir2

cat 파일 내용 출력 - cat a.txt

more 파일 내용을 쪽단위로 출력 - more a.txt

chmod 파일 접근권한 변경 - chmod 755 a.out

grep 패턴 검색 grep abcd a.txt

Page 37: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

38

기본 명령

프로세스 관련 명령

명령 기능 주요 옵션 예제

ps 현재 실행 중인 프로세

스의 정보를 출력

-ef : 모든 프로세스에

대한 상세 정보 출력

ps

ps -ef

ps -ef | grep ftp

kill 프로세스 강제 종료 -9 : 강제 종료 kill 5000

kill -9 5001

Page 38: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

39

Directory hierarchy

특수 directory directives

• . : 현재 위치

• .. : 상위 directory

• ~ : 사용자의 홈 디렉토리 (ex. /home/user1)

절대 경로

• / <- 로 시작하는 경우

- cp /tmp/a.txt .

- cp /home/user1/a.txt /tmp

상대 경로

• / <- 로 시작하지 않는 경우

- cp ./a.txt /tmp

- cp ../../dir1/a.txt ../a.txt

Page 39: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

40

환경설정

alias

• 특정 명령의 다른 이름을 지정

env

• 지금 자신의 환경 변수를 나열

.bashrc (.profile)

• Bash shell이 시작시 참조하는 환경변수 설정

Hidden file

• Linux에서는 파일명 앞에 '.' 이 붙으면 숨김파일 임

PS1

• Bash shell에서 프롬프트를 설정

PATH

• 실행파일 또는 참조할 파일의 위치를 결정

• PATH 변수의 순서대로 특정파일을 찾음

Page 40: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

42

Tips

tar

• 여러 파일을 하나로 묶음

zip

• 여러 파일을 하나로 압축

gz

• 하나의 파일을 압축

history

• 사용한 명령어들을 나열

indent

• 소스코드를 예쁘게 정렬해 줌

• indent -npsl -bli0 -sob -nbc -i8 -l1000 code.c

• alias ind='indent -npsl -bli0 -sob -nbc -i8 -l1000'

Page 41: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

43

Tips

tab 자동완성

• Bash shell에서는 tab키로 나머지 명령을 자동으로 완성 가능

상하 커서키

• history에서 최근에 사용된 명령을 다시 사용할 때

ctrl + r

• history에서 최근 사용된 명령을 자동 완성

ctrl + s & ctrl + q

• 스크린 출력을 정지/재개

*

• 모든 문자들을 대체

Page 42: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

44

Meta characters

Char. Function

> Redirection from stdout to file

>> Redirection from stdout to file

< Redirection from file to stdin

* Any character

? Any character, but one

| Pipe from stdout of p1 to stdout of p2

; Command order

& Background execution

Page 43: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

45

Ex: Find Files

파일 다운로드 및 압축해제

• wget http://cslab.cbnu.ac.kr/_down/usp/kernel.tgz

• 압축풀기 : tar xfz kernel.tgz

실습내용

• kernel 아래에서 아래 파일 이름으로 된 파일의 위치를 찾으시오

• File names

- fcntl.c

- range.h

- xenfs.h

- builtin-stat.c

- algos.c

- slab.c

• e.g.

- kernel/aaa/bbb/fcntl.c

- kernel/ccc/ddd/range.h

- ...

Page 44: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

46

Ex: Find Files 2

실습내용

• kernel/mm 아래에서 아래 텍스트를 가지고 있는 파일의 위치와 몇 번째행에 있는지 찾으시오

• Texts

- 'YouGotIt'

- 모두 3개임

Page 45: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

VI EDITOR

Page 46: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

48

vi edit의 모드

Command mode

• Text의 위치나 편집 가능하게 함

Input mode

• 사용자가 text를 작성할 수 있는 모드

Page 47: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

49

How to exit from vi (command mode)

:q <enter>

• to exit, if you have not made any changes to the file

:q! <enter>

• the forced quit, it will discard the changes and quit

:wq <enter>

• for save and Exit

:x <enter>

• same as above command

ZZ

• for save and Exit (Note this command is uppercase)

The ! Character forces over writes, etc. :wq!

Page 48: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

50

Moving Around

You can move around only when you are in the command mode

Arrow keys usually works (but may not)

The standard keys for moving cursor are:

• h - for left

• l - for right

• j - for down

• k - for up

Page 49: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

51

Entering text

To enter the text in vi you should first switch to input mode

• To switch to input mode there are several different commands

• a - Append mode places the insertion point after the current character

• i - Insert mode places the insertion point before the current character

• I - places the insertion point at the beginning of current line

• o - is for open mode and places the insertion point after the current line

• O - places the insertion point before the current line

• R - starts the replace(overwrite) mode

Page 50: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

52

Editing text

x - deletes the current character

d - is the delete command but pressing only d will not delete anything you need to press a second key

• dw - deletes to end of word

• dd - deletes the current line

• d0 - deletes to beginning of line

There are many more keys to be used with delete command

Page 51: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

53

Undo and repeat command

u - undo the changes made by editing commands

. - repeats the last edit command

Page 52: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

54

Copy, cut and paste in vi

v - visual mode

y - yank

yy - (yank) copy current line to buffer

nyy - where n is number of lines

p - paste the yanked lines from buffer to the line below

P - paste the yanked lines from buffer to the line above

(the paste commands will also work after yank)

Page 53: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

55

Jump

gg - jump the first line of file

G - jump the end of file

nG - jump to the line (where n is line number)

Page 54: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

56

Search / Replace

/asdf - search forward for 'asdf'

• to move forward, type n

?asdf - search backward for 'asdf'

• to move backward, type N

:%s/asdf/xywz/g - replace 'asdf' by 'xywz'

Page 55: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

57

Everything in a Slide

Page 56: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

58

유용한 vi environment -> .vimrc

au FileType * setl fo-=cro

syntax on / off

set number / nonumber

set wrap / nowrap

set hlsearch

set ignorecase

set ts=4

Page 57: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

59

GCC Option(1)

-o

• 출력(output) 화일명을 정하는 옵션

• gcc -o hello hello.chello.c라는 파일을 hello라는 실행파일로 만듦

-c

• 컴파일(compile) 작업만 하고 싶은 경우

• gcc -c hello.chello.c 파일에서 hello.o라는 오브젝트 파일로 만듦

Page 58: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

60

GCC Option(2)

-I

• #include 문장에서 지정한 헤더 화일이 들어있는 곳을 정하는 옵션

• gcc -c source.c -Iinclude-I를 붙여씀

-g

• 컴파일된 오브젝트파일에 디버깅 코드를 추가

• gcc -g -o hello hello.c

Page 59: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

61

GCC Option(3)

-O

• 코드를 최적화(optimization)

• -O2는 가장 많이 최적화

• -O0라고 하면 최적화를 하지 않음

• 기본값: -O1

-l

• 어떤 라이브러리를 사용하는지 지정

• -l 은 명령줄의 맨 뒤에 사용할 것

-L

• 그 라이브러리가 어느 디렉토리에 있는지 알려 줌

Page 60: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

62

Ex: vi로 c code를 작성

실습목표

• vi를 이용하여, 1~100사이의 prime number를 출력하는 프로그램을 작성

• main.c 에서는 for 문으로 1~100의 loop

• prime.c 에서는 checkprime함수를 구현하여 prime number인지 확인

• Compile: gcc -o getprime main.c prime.c

• ./getprime 으로 실행

Page 61: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

MAKE

Page 62: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

64

make

Definition

• make is utility to maintain groups of programs

Object

• If some file is modified, make detects it and update files related with modified one

• It can use to command based programs

Page 63: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

65

make basic rules

3 components

• target, dependency, command

File format

• file name: Makefile (makefile)

• format

target: dependency[tab] command

Page 64: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

66

make example

make 를 사용하지 않을때

• gcc -c main.c

• gcc -c add.c

• gcc -c sub.c

• gcc -o test main.o add.o sub.o

• 혹은, gcc -o test main.c add.c sub.c

Page 65: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

67

Makefile example

Makefile

all: main.o add.o sub.o

gcc -o test main.o add.o sub.o

main.o: addsub.h main.c

gcc -c main.c

add.o: add.c

gcc -c add.c

sub.o: sub.c

gcc -c sub.c

반드시 tab으로 입력

Page 66: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

68

make example

어느 때 어떤 target이 다시 컴파일 될 것인가?

• main.c가 바뀌었을 경우

• add.c 혹은 sub.c가 바뀌었을 경우

• addsub.h가 바뀌었을 경우

Page 67: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

69

make clean

To clean up

• make clean

all: main.o add.o sub.o

gcc -o test main.o add.o sub.o

main.o: addsub.h main.c

gcc -c main.c

add.o: add.c

gcc -c add.c

sub.o: sub.c

gcc -c sub.c

clean:rm *.o test

Page 68: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

70

macro

CC=gcc

SRC=main.c add.c sub.c

main: ${SRC}

${CC} -o test ${SRC}

Page 69: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

71

미리 정해져 있는 매크로

make -p 를 통하여 모든 값들을 확인 가능

• ASFLAGS = as 명령어의 옵션 세팅

- AS = as

• CFLAGS = gcc 의 옵션 세팅

- CC = cc (= gcc)

• CPPFLAGS = g++ 의 옵션

- CXX = g++

• LDLFAGS = ld 의 옵션 세팅

- LD = ld

• LFLAGS = lex 의 옵션 세팅

- LEX = lex

• YFLAGS = yacc 의 옵션 세팅

- YACC = yacc

Page 70: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

72

Simple Makefile

CFLAGS = -Wall -O -g

bin=hello

t1=main

t2=funcs

obj=$(t1).o $(t2).o

all: $(bin)

$(bin): $(obj)

$(CC) $(obj) -o $@

clean:

rm -f $(bin) *.o

$(bin)을 나타내는 내부변수

CFLAGS = -Wall -O -g

bin=hello

t1=main

t2=funcs

obj=main.o funcs.o

all: hello

hello: main.o funcs.o

gcc main.o funcs.o -o hello

clean:

rm -f hello *.o

Page 71: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

73

Ex: Makefile 작성

Ex.

• 앞서 코딩한 getprime 프로그램을 build 할 수 있는 Makefile 작성

• make, make all, make clean이 정상적으로 동작하는지 확인

- make 와 make all은 동일 함

Ex.

• 파일을 복사하는 mycp 프로그램 작성

• build 할 수 있는 Makefile 작성

• ./mycp a.txt b.txt

Page 72: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

FILE TRANSFER USING WINSCP

Page 73: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

75

WinSCP

http://winscp.net

ssh를 이용한 파일 전송 tool

Page 74: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

76

WinSCP

Page 75: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

77

Exercise

Ex.

• 앞서 제작한 getprime 프로그램의 폴더를 모두 압축하여 Windows로전송하자

• 아래 두가지 방법 (gugu 폴더를 모두 압축할 경우)

tar cvfz gugu.tar.gz gugu

zip -r gugu.zip gugu

• WinSCP를 이용하여 다운로드

Page 76: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

GNU DEBUGGER (GDB)

Page 77: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

79

GDB 기본 사용법

gcc 컴파일시 -g 옵션 추가 (-O 옵션은 제거)

gdb 실행

• gdb a.out

• gdb --args a.out 1 2 3 4

root@iter1:/tmp> gdb a.outGNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04Copyright (C) 2012 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law. Type "show copying"and "show warranty" for details.This GDB was configured as "x86_64-linux-gnu".For bug reporting instructions, please see:<http://bugs.launchpad.net/gdb-linaro/>...Reading symbols from /tmp/a.out...done.(gdb)

Page 78: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

80

GDB 기본 사용법

종료

• q / ctrl+d

소스 찾아가기 (list)

• l : main 함수를 기점으로 소스의 내용이 출력된다

• l 10 : 10 행 주변의 소스가 출력

• l func : func 함수의 소스를 출력

• l a.c:func : a.c 파일의 func 함수부분을 출력

• l a.c:10 : a.c 파일의 10행을 기준으로 출력

Page 79: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

81

GDB 기본 사용법

중단점 사용하기 (breakpoint)

• b func : func 함수에 브레이크 포인트 설정

• b 10 : 10행에 브레이크 포인트 설정

• b a.c:func : a.c파일의 func함수에 브레이크 포인트 설정

• b a.c:10 : a.c파일의 10행에 브레이크 포인트 설정

• b +2 : 현재 행에서 2개 행 이후 지점에 브레이크 포인트 설정

• b -2 : 현재 행에서 2개 행 이전 지점에 브레이크 포인트 설정

• b *0x8049000 : 0x8049000 주소에 브레이크 포인트 설정 (어셈블리로디버깅 시 사용)

• b 10 if var == 0 : 10행에 브레이크 포인트를 설정해되, var 변수 값이0일 때 작동

Page 80: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

82

GDB 기본 사용법

중단점 삭제하기 (clear, delete)

• cl func : func 함수의 시작 부분에 브레이크 포인트 지움

• cl 10 : 10행의 브레이크 포인트 지움

• cl a.c:func : a.c 파일의 func함수의 브레이크 포인트 지움

• cl a.c:10 : a.c 파일의 10행의 브레이크 포인트 지움

• cl : 모든 브레이크 포인트 지움

Page 81: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

83

GDB 기본 사용법

프로그램 실행, 종료 (run, kill)

• r : 프로그램 수행 (재시작)

• r arg1 arg2 : arg1과 arg2를 인자로 프로그램 수행

• k : 프로그램 수행종료

역추적하기 (backtrace)

• bt : 오류가 발생한 함수를 역으로 찾아감

Page 82: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

84

GDB 기본 사용법

변수 정보보기 (info, print)

• info locals : 현재 상태에서 어떤 지역변수들이 있으며, 값은어떠한지를 알 수 있음

• info variables : 현재 상태에서의 전역변수 리스트를 확인

• p lval : lval 값을 확인

• p func : func 함수의 주소값을 확인

• p pt : pt가 구조체라면 구조체의 주소를 확인

• p *pt : pt가 구조체라면 구조체의 값을 확인

• p **pt : *pt가 구조체라면 구조체의 값을 확인

• info registers : 레지스트 값 전체를 한번에 확인

Page 83: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

85

GDB 기본 사용법

디버깅 하기 (step, next, continue, until, finish, return, step instruction, next instruction)

• s : 현재 출력된 행을 수행하고 멈추지만, 함수의 경우 함수의 내부로들어가서 수행

• s 5 : s를 5번 입력한 것과 동일

• n : 현재 행을 수행하고 멈추지만, 함수의 경우 함수를 수행하고 넘어감

• n 5 : n을 5번 입력한 것과 동일

• c : 다음 브레이크 포인트를 만날때 까지 계속 수행

• u : for 문에서 빠져나와서 다음 브레이크 포인트까지 수행

Page 84: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

86

Ex: gdb

실습목표

• 다음 프로그램을 정상적인 결과값이 나오도록 debugging 하시오

• gcc -g -o a.out a.c

# include <stdio.h>

void main(){

int i, num, j;printf ("Enter the number: ");scanf ("%d", &num );

for (i=1; i<num; i++)j=j*i;

printf("The factorial of %d is %d\n", num, j);}

Page 85: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

87

Ex: gdb

실습목표

• 다음 프로그램은 왜 segment fault를 만드는지 설명하시오

• gcc -g -o a.out a.c

#include <stdio.h>

void main(){

char *temp= "Paras";

int i;i=0;

temp[0]='F';

for (i=0 ; i < 5 ; i++ )printf("%c\n", temp[i]);

}

Page 86: LINUX EXERCISEcslab.cbnu.ac.kr/course/2019.2/cc/lab1.linux exercise.pdf · 2019-10-01 · 21. GNU 프로젝트. Richard Stallman • 1984년GNU 프로젝트가시작 • 표는 '두가

88

Ex: gdb

실습목표

• 다음 프로그램은 왜 segment fault를 만드는지 설명하시오

• gcc -g -o a.out a.c

#include <stdio.h>#include <stdlib.h>#include <string.h>

int main(int argc, char **argv){

char *buf;

buf = malloc(1<<31);strcpy(buf, "This is Test");printf("%s\n", buf);

return 0;}


Recommended