20160419 리눅스 기초
모의해킹 침해대응 전문가 과정 / 2016. 4. 20. 00:35
===============================메모===============================
#
# Specific Configuration
#
export PS1='[\u@\h \w]\$ '
alias ls='ls --color=tty -h'
alias pps='ps -ef | head -1 ; ps -ef | grep $1'
alias vi='/usr/bin/vim'
alias grep='grep -i --color'
# Specific Configuration
#
export PS1='[\u@\h \w]\$ '
alias ls='ls --color=tty -h'
alias pps='ps -ef | head -1 ; ps -ef | grep $1'
alias vi='/usr/bin/vim'
alias grep='grep -i --color'
[참고] telnet 서비스 Open 방법
# chkconfig --list krb5-telnet
# chkconfig krb5-telnet on
# service xinetd restart
# telnet localhost
user01 사용자 로그인
-> root 사용자는 로그인이 가능하지 않음
# vi /etc/securetty
......
pts/1
pts/2
.....
pts/11
-> root 사용자로 로그인이 가능한 상태
# chkconfig --list krb5-telnet
# chkconfig krb5-telnet on
# service xinetd restart
# telnet localhost
user01 사용자 로그인
-> root 사용자는 로그인이 가능하지 않음
# vi /etc/securetty
......
pts/1
pts/2
.....
pts/11
-> root 사용자로 로그인이 가능한 상태
===============================복습===============================
diff/cmp CMD
# diff --recursive dir1 dir2
# diff --recursive dir1 dir2
sort CMD
# CMD | sort -k 3
# CMD | sort -k 3 -r
# CMD | sort -k 3
# CMD | sort -k 3 -r
# df -k //내컴퓨터에서 보는거랑 같음
# du -sk /var //특정폴더 용량
# cd /var ; du -sk * | sort -ar | more
# find / -size +5G -type f //특정폴더 용량큰 파일 확인
# du -sk /var //특정폴더 용량
# cd /var ; du -sk * | sort -ar | more
# find / -size +5G -type f //특정폴더 용량큰 파일 확인
file CMD
# cd /etc; file *
# cd /etc; file *
grep CMD
# grep OPTIONS PATTERN file1
OPTIONS: -l, -n, -i, -v, -w, --coloe
PATTERN: * . root root$ [abc]
# grep OPTIONS PATTERN file1
OPTIONS: -l, -n, -i, -v, -w, --coloe
PATTERN: * . root root$ [abc]
# cat /var/log/messages | egrep -i '(warn|err|crit|alert|emerg)'
find CMD
# find / -name core -type f
# find / -name core -type f -exec rm -f {} \;
# find / -name core -type f
# find / -name core -type f -exec rm -f {} \;
gzip/gunzip CMD
bzip2/bunzip2 CMD
tar CMD
jar CMD
zip CMD
bzip2/bunzip2 CMD
tar CMD
jar CMD
zip CMD
===============================강의===============================
쉘의 특성
리다이렉션(Redirection)
입력 리다이렉션(Redirection stdin)
# mail -s "OK : linux2XX" admin@example.com < report.txt
출력 리다이렉션(Redirection stdout)
# ls -l > file.list
에러 리다이렉션(Redirection stderr)
# ls /var /nodir > file.log 2>&1
리다이렉션(Redirection)
입력 리다이렉션(Redirection stdin)
# mail -s "OK : linux2XX" admin@example.com < report.txt
출력 리다이렉션(Redirection stdout)
# ls -l > file.list
에러 리다이렉션(Redirection stderr)
# ls /var /nodir > file.log 2>&1
# ./script.sh > script.log 2>&1
# ./configure --prefix=/usr/local/apache > apache.log 2>&1
파이프(Pipe)
# CMD | more
# CMD | grep xinetd
# ./configure --prefix=/usr/local/apache > apache.log 2>&1
파이프(Pipe)
# CMD | more
# CMD | grep xinetd
[참고] CMD | tee file.log
# while true
do
CMD | tee -a file.log
sleep 2
done
# script -a /dev/null | tee /dev/pts2 | tee /dev/pts/3
쉘 자체의 기능
# set -o
# set -o vi
# set +o vi
변수(Variable)
변수의 종류
- 지역변수(Local Variable) # VAR=5
- 환경변수(Environmental Variable) # export VAR=5
- 특수변수(Special Variable) : $$, $?, $!, $1, $2, $3, ...
변수 선언 방법
# VAR=5 ; export VAR (# export VAR=5)
# echo $VAR
# unset VAR
export 의미
시스템 환경변수(system environment variable) set, env
PS1, PS2
PATH
HISTIMEFORMAT
HOME, PWD, LOGNAME, TERM, LANG, USER, UID
쉘 메타캐릭터(Metacharacter)
'' "" \ `` ;
히스토리(History)
~/.bash_history
앨리어스(Alias)
# alias cp='cp -i'
# alias cp
# unalias cp
환경파일
/etc/profile
$HOME/.bash_profile
$HOME/.bashrc
# while true
do
CMD | tee -a file.log
sleep 2
done
# script -a /dev/null | tee /dev/pts2 | tee /dev/pts/3
쉘 자체의 기능
# set -o
# set -o vi
# set +o vi
변수(Variable)
변수의 종류
- 지역변수(Local Variable) # VAR=5
- 환경변수(Environmental Variable) # export VAR=5
- 특수변수(Special Variable) : $$, $?, $!, $1, $2, $3, ...
변수 선언 방법
# VAR=5 ; export VAR (# export VAR=5)
# echo $VAR
# unset VAR
export 의미
시스템 환경변수(system environment variable) set, env
PS1, PS2
PATH
HISTIMEFORMAT
HOME, PWD, LOGNAME, TERM, LANG, USER, UID
쉘 메타캐릭터(Metacharacter)
'' "" \ `` ;
히스토리(History)
~/.bash_history
앨리어스(Alias)
# alias cp='cp -i'
# alias cp
# unalias cp
환경파일
/etc/profile
$HOME/.bash_profile
$HOME/.bashrc
프로세스 관리(Process Admin)
프로세스 정보(/proc/PID)
PID, PPID, Control Terminal, UID
프로세스 관리1
프로세스 실행
프로세스 확인
프로세스 종료
프로세스 정보(/proc/PID)
PID, PPID, Control Terminal, UID
프로세스 관리1
프로세스 실행
프로세스 확인
프로세스 종료
===============================실습===============================
pipe
커널버퍼(커널에 있는 임시적인 저장공간)
커널버퍼(커널에 있는 임시적인 저장공간)
[참고] 명령어 비교
--------------------------+---------------------------
# ps -ef > file.log | # ps -ef | grep inetd
# grep inetd file.log |
--------------------------+---------------------------
--------------------------+---------------------------
# ps -ef > file.log | # ps -ef | grep inetd
# grep inetd file.log |
--------------------------+---------------------------
ps > file.log > grep
ps > 커널버퍼 > grep
ps > 커널버퍼 > grep
[EX] 출력 결과를 파일로 저장
# CMD > file.log
# CMD | tee file.log
# CMD > file.log
# CMD | tee file.log
[root@linux220 ~]# cd /test
[root@linux220 /test]# ls
[root@linux220 /test]# cal 2002 > file.log
[root@linux220 /test]# cat file.log
2002
[root@linux220 /test]# ls
[root@linux220 /test]# cal 2002 > file.log
[root@linux220 /test]# cat file.log
2002
1월 2월 3월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
4월 5월 6월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
7월 8월 9월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
4월 5월 6월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
7월 8월 9월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
10월 11월 12월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
[root@linux220 /test]# cal 2002 | tee file2.log
2002
1월 2월 3월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
4월 5월 6월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
7월 8월 9월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
4월 5월 6월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
7월 8월 9월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
10월 11월 12월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
[root@linux220 /test]# cat file2.log
2002
1월 2월 3월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
4월 5월 6월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
7월 8월 9월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
4월 5월 6월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
7월 8월 9월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
10월 11월 12월
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
일 월 화 수 목 금 토 일 월 화 수 목 금 토 일 월 화 수 목 금 토
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
[EX1] 실시간 모니터링 하면서 파일로 내용을 저장
[root@linux220 /test]# service httpd restart
httpd 를 정지 중: [실패]
httpd (을)를 시작 중: [ OK ]
# while true
> do
> ps -ef | grep httpd | wc -l | tee -a file.log (> w root >> file.log)
> sleep 2
> done
[root@linux220 /test]# while true
> do
> ps -ef | grep httpd | wc -l | tee -a file.log
> sleep 2
> done
10
10
10
10
10
> do
> ps -ef | grep httpd | wc -l | tee -a file.log
> sleep 2
> done
10
10
10
10
10
[root@linux220 /test]# cat file.log
10
10
10
10
10
10
10
10
10
10
[EX2] 다른 터미널 창에서 입력하는 내용이 그대로 지정한 터미널 창으로 출력 되도록 설정
[참고] script CMD
# cd /test ; rm –rf /test/*
# script –a file.log
# ls
# date
# cal
# exit
# cat file.log
실습내용 그림1
bash 쉘의 기능
# set -o /* 쉘 자체의 기능 전체 목록 확인 */
# set -o vi /* 쉘 자체의 기능 중 vi 기능을 ON */
# set +o vi /* 쉘 자체의 기능 중 vi 기능을 OFF */
# set -o /* 쉘 자체의 기능 전체 목록 확인 */
# set -o vi /* 쉘 자체의 기능 중 vi 기능을 ON */
# set +o vi /* 쉘 자체의 기능 중 vi 기능을 OFF */
[root@linux220 ~]# telnet 172.16.9.252
Trying 172.16.9.252...
Connected to 172.16.9.252 (172.16.9.252).
Escape character is '^]'.
Trying 172.16.9.252...
Connected to 172.16.9.252 (172.16.9.252).
Escape character is '^]'.
linux252.example.com (Linux release 2.6.18-409.el5.centos.plusPAE #1 SMP Tue Mar 15 18:56:56 EDT 2016) (6)
login: root
Password:
Last login: Tue Apr 19 16:54:16 from 172.16.6.203
You have new mail.
[linux252@~]# hostname
linux252.example.com
[linux252@~]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
[linux252@~]# ps
PID TTY TIME CMD
6290 pts/6 00:00:00 login.krb5
6354 pts/6 00:00:00 bash
6860 pts/6 00:00:00 ps
Password:
Last login: Tue Apr 19 16:54:16 from 172.16.6.203
You have new mail.
[linux252@~]# hostname
linux252.example.com
[linux252@~]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
[linux252@~]# ps
PID TTY TIME CMD
6290 pts/6 00:00:00 login.krb5
6354 pts/6 00:00:00 bash
6860 pts/6 00:00:00 ps
<Ctrl + D>
[linux252@~]# logout
Connection closed by foreign host.
Connection closed by foreign host.
[root@linux220 ~]# telnet 172.16.9.252
Trying 172.16.9.252...
Connected to 172.16.9.252 (172.16.9.252).
Escape character is '^]'.
linux252.example.com (Linux release 2.6.18-409.el5.centos.plusPAE #1 SMP Tue Mar 15 18:56:56 EDT 2016) (16)
login: root
Password:
Last login: Tue Apr 19 16:55:21 from 172.16.6.204
You have new mail.
[linux252@~]# set -o | grep ignoreeof
ignoreeof off
[linux252@~]# set -o ignoreeof
[linux252@~]# Use "logout" to leave the shell.
[linux252@~]# exit
logout
Connection closed by foreign host.
Password:
Last login: Tue Apr 19 16:55:21 from 172.16.6.204
You have new mail.
[linux252@~]# set -o | grep ignoreeof
ignoreeof off
[linux252@~]# set -o ignoreeof
[linux252@~]# Use "logout" to leave the shell.
[linux252@~]# exit
logout
Connection closed by foreign host.
<Tab> 파일이름 자동 완성 기능
[EX1] <TAB> 이용한 디렉토리 이동
# cd /etc/sysco<TAB>
# cd /etc/sysconfig/netw<TAB>
# cd /etc/sysconfig/network-s<TAB>
# cd /etc/sysco<TAB>
# cd /etc/sysconfig/netw<TAB>
# cd /etc/sysconfig/network-s<TAB>
[EX2] 자동 완성 기능이 되지 않을때의 <TAB><TAB> 사용
# cd /etc/sys<TAB><TAB>
sysconfig/ sysctl.conf syslog.conf
# cd /etc/sysco<TAB> /* 다른 문자와 구별 할 수 있는 글자까지 적어주고 다시<Tab>을 누르면 완성 */
/etc/sysconfig
# cd /etc/sys<TAB><TAB>
sysconfig/ sysctl.conf syslog.conf
# cd /etc/sysco<TAB> /* 다른 문자와 구별 할 수 있는 글자까지 적어주고 다시<Tab>을 누르면 완성 */
/etc/sysconfig
[root@linux220 ~]# cd /etc/sys
sysconfig/ sysctl.conf syslog.conf
sysconfig/ sysctl.conf syslog.conf
[EX3] <TAB> 이용한 명령어의 종류 확인
# ch[TAB][TAB]
# ch[TAB][TAB]
[root@linux220 ~]# ch
chacl check-binary-files chown
chage checkmodule chpasswd
change_console checkpolicy chroot
charmap chfn chrt
chat chgrp chsh
chattr chkconfig chvt
chcat chkfontpath
chcon chmod
[root@linux220 ~]# ch
chacl check-binary-files chown
chage checkmodule chpasswd
change_console checkpolicy chroot
charmap chfn chrt
chat chgrp chsh
chattr chkconfig chvt
chcat chkfontpath
chcon chmod
[root@linux220 ~]# ch
[root@linux220 ~]# system-config-
system-config-authentication system-config-nfs
system-config-date system-config-packages
system-config-display system-config-printer
system-config-httpd system-config-rootpassword
system-config-kdump system-config-samba
system-config-keyboard system-config-securitylevel
system-config-language system-config-securitylevel-tui
system-config-lvm system-config-services
system-config-network system-config-soundcard
system-config-network-cmd system-config-time
system-config-network-gui system-config-users
system-config-network-tui
[root@linux220 ~]# system-config-
system-config-authentication system-config-nfs
system-config-date system-config-packages
system-config-display system-config-printer
system-config-httpd system-config-rootpassword
system-config-kdump system-config-samba
system-config-keyboard system-config-securitylevel
system-config-language system-config-securitylevel-tui
system-config-lvm system-config-services
system-config-network system-config-soundcard
system-config-network-cmd system-config-time
system-config-network-gui system-config-users
system-config-network-tui
[root@linux220 ~]# system-config-
[참고] redhat-config-* & system-config-*
RHEL 3.X(CentOS 3.X) : redhat-config-*
REEL 4.X(CentOS 4.X) : system-config-*
RHEL 3.X(CentOS 3.X) : redhat-config-*
REEL 4.X(CentOS 4.X) : system-config-*
# set –o vi // 예전에 나온 쉘들은 tap이 없어서 편집기쓰듯 써야함
(ㄱ) 파일이름 자동 완성 기능(File name completion)
(ㄴ) 명령행 편집(Command Line Edit)
(ㄱ) 파일 이름 자동 완성 기능
# cd /etc/sysco[TAB]
# cd /etc/sysco[ESC][\]
(ㄴ) 명령행 편집
(이전 명령어를 되살려서 쓰는 경우)
# ↑↑↑↓↓↓
# [ESC][k][k][k][j][j][j]
(이전 명령어를 편집해서 사용하는 경우)
# find / -name core –type f
<CTRL + C>
# [ESC][k]
# find / -name file1 –type f
# cp /etc/service file1
# cp file1 file2
# cp file1 file3
# cp file1 file4
# cp file1 file5
(이전에 사용한 명령어를 확장해서 사용하는 경우)
# df –h
# df –h /
# df –h / | tail –1
# df –h / | tail –1 | awk '{print $5}'
# ps –ef | head
# ps –ef | head | sort –k 2
# ps –ef | head | sort –k 2 –n
# ps –ef | head | sort –k 2 –nr
(확인 + 명령어 수행 + 확인)
# chkconfig --list krb5-telnet
# chkconfig krb5-telnet on
# ↑↑
(ㄱ) 파일이름 자동 완성 기능(File name completion)
(ㄴ) 명령행 편집(Command Line Edit)
(ㄱ) 파일 이름 자동 완성 기능
# cd /etc/sysco[TAB]
# cd /etc/sysco[ESC][\]
(ㄴ) 명령행 편집
(이전 명령어를 되살려서 쓰는 경우)
# ↑↑↑↓↓↓
# [ESC][k][k][k][j][j][j]
(이전 명령어를 편집해서 사용하는 경우)
# find / -name core –type f
<CTRL + C>
# [ESC][k]
# find / -name file1 –type f
# cp /etc/service file1
# cp file1 file2
# cp file1 file3
# cp file1 file4
# cp file1 file5
(이전에 사용한 명령어를 확장해서 사용하는 경우)
# df –h
# df –h /
# df –h / | tail –1
# df –h / | tail –1 | awk '{print $5}'
# ps –ef | head
# ps –ef | head | sort –k 2
# ps –ef | head | sort –k 2 –n
# ps –ef | head | sort –k 2 –nr
(확인 + 명령어 수행 + 확인)
# chkconfig --list krb5-telnet
# chkconfig krb5-telnet on
# ↑↑
변수(Variable)
변수의 종류
- 지역변수(Local Variable) : # VAR=5
- 환경변수(Environment Variable) : # export VAR=5
- 특수변수(Special Variable) : $$, $?, $!, $0, $1, $#, $*, ....
변수의 종류
- 지역변수(Local Variable) : # VAR=5
- 환경변수(Environment Variable) : # export VAR=5
- 특수변수(Special Variable) : $$, $?, $!, $0, $1, $#, $*, ....
(1) 지역변수 선언
변수 선언 방법(EX: bash)
# VAR=5 (# export VAR=5)
# export VAR
# echo $VAR (# print $VAR)
# unset VAR
지역 변수는 현재 사용하고 있는 쉘에만 적용되는 변수 값으로 선언 하는 것이다.
서브 쉘에서 지역 변수 값을 확인하려고 할 경우 값이 나타나지 않는다.
변수 선언 방법(EX: bash)
# VAR=5 (# export VAR=5)
# export VAR
# echo $VAR (# print $VAR)
# unset VAR
지역 변수는 현재 사용하고 있는 쉘에만 적용되는 변수 값으로 선언 하는 것이다.
서브 쉘에서 지역 변수 값을 확인하려고 할 경우 값이 나타나지 않는다.
var=5 echo $var (0)
----------+ +----------
| |
| echo $var (X) |
+---------------+
----------+ +----------
| |
| echo $var (X) |
+---------------+
[root@linux220 ~]# var=5
[root@linux220 ~]# echo $var
5
[root@linux220 ~]# var=hello
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# bash
[root@linux220 ~]# echo $var
[root@linux220 ~]# echo $var
5
[root@linux220 ~]# var=hello
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# bash
[root@linux220 ~]# echo $var
[root@linux220 ~]# exit
exit
[root@linux220 ~]# echo $var
hello
exit
[root@linux220 ~]# echo $var
hello
(2) 환경변수 선언
환경변수는 현재 쉘과 서브쉘에 변수를 모두 적용되는 변수 값으로 선언해 주는 것이다.
환경변수를 사용하는 이유는 이전쉘에서 선언한 변수 값을 서브쉘에서도 동일하게 적용하기 위한 것이다.
export var=hello echo $var (0)
----------+ +----------
| |
| echo $var (0) |
+---------------+
----------+ +----------
| |
| echo $var (0) |
+---------------+
[root@linux220 ~]# var=hello
[root@linux220 ~]# export var
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# bash
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# exit
exit
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# export var
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# bash
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# exit
exit
[root@linux220 ~]# echo $var
hello
export var=hello echo $var
----------+ +----------
| |
| export var=centos |
+---------------+ |
| |
| echo $var |
+---------------+
[root@linux220 ~]# export var=hello
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# bash
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# export var=centos
[root@linux220 ~]# echo $var
centos
[root@linux220 ~]# bash
[root@linux220 ~]# echo $var
centos
[root@linux220 ~]# ps
PID TTY TIME CMD
16295 pts/1 00:00:00 bash
16355 pts/1 00:00:00 bash
16367 pts/1 00:00:00 bash
16379 pts/1 00:00:00 ps
[root@linux220 ~]# exit
exit
[root@linux220 ~]# exit
exit
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# bash
[root@linux220 ~]# echo $var
hello
[root@linux220 ~]# export var=centos
[root@linux220 ~]# echo $var
centos
[root@linux220 ~]# bash
[root@linux220 ~]# echo $var
centos
[root@linux220 ~]# ps
PID TTY TIME CMD
16295 pts/1 00:00:00 bash
16355 pts/1 00:00:00 bash
16367 pts/1 00:00:00 bash
16379 pts/1 00:00:00 ps
[root@linux220 ~]# exit
exit
[root@linux220 ~]# exit
exit
[root@linux220 ~]# echo $var
hello
(결론) 환경 변수는 자신이 선언한 쉘의 서브쉘에서 적용되는 것이다. 상위의 쉘에는 적용되지 않는다.
[참고] env와 set의 차이점 (선언된 변수를 확인하는 명령어)
■ set : 모든 변수에 대해 출력 (지역변수 + 환경변수)
■ env : 환경변수만 출력
[root@linux220 ~]# var1=CentOS
[root@linux220 ~]# export var2=Linux
[root@linux220 ~]# set | grep var1
var1=CentOS
[root@linux220 ~]# set | grep var2
var2=Linux
[root@linux220 ~]# env | grep var1
[root@linux220 ~]# env | grep var2
var2=Linux
[root@linux220 ~]# export var2=Linux
[root@linux220 ~]# set | grep var1
var1=CentOS
[root@linux220 ~]# set | grep var2
var2=Linux
[root@linux220 ~]# env | grep var1
[root@linux220 ~]# env | grep var2
var2=Linux
(3-1) 시스템 지역변수
i) PS1 명령어 프롬프트에 나타내는 시스템 환경 변수
# echo $PS1
[\u@\h \W]\$
[root@linux203 ~]#
PS1='[\u@\h \W]\$ '
-----------------------------------------------------------
\u : username
\h : hostname
\W : working directory (예: /home/user01 -> user01)
\w : working directory (예: /home/user01 -> /home/user01)
\$ : root(#), user01($)
-----------------------------------------------------------
[\u@\h \W]\$
[root@linux203 ~]#
PS1='[\u@\h \W]\$ '
-----------------------------------------------------------
\u : username
\h : hostname
\W : working directory (예: /home/user01 -> user01)
\w : working directory (예: /home/user01 -> /home/user01)
\$ : root(#), user01($)
-----------------------------------------------------------
[root@linux220 ~]# echo $PS1
[\u@\h \w]\$
[root@linux220 ~]# PS1='\w> '
~> cd /test
/test> cd
~> rm -rf /test/*
~> set | grep PS1
PS1='\w> '
~> bash
[root@linux220 ~]# PS1='[$PWD]# '
[/root]# bash
[root@linux220 ~]# export PS1='[\u@\w]# '
[root@~]# bash
[root@linux220 ~]# exit
exit
[root@~]# exit
exit
[/root]# tty
/dev/pts/1
[/root]# . ~/.bashrc //bashrc 설정파일 불러옴
[root@linux220 ~]#
[root@linux220 ~]#
ii) PS2 명령어가 아직 끝나지 않았음을 나타낼 때 사용
[root@linux220 ~]# PS2='> '
[root@linux220 ~]# echo $PS2
>
[root@linux220 ~]# ls -a -l
합계 224K
drwxr-x--- 20 root root 4.0K 4월 19 17:35 .
drwxr-xr-x 28 root root 4.0K 4월 19 16:12 ..
-rw------- 1 root root 1.5K 4월 19 16:12 .ICEauthority
생략
[root@linux220 ~]# echo $PS2
>
[root@linux220 ~]# ls -a -l
합계 224K
drwxr-x--- 20 root root 4.0K 4월 19 17:35 .
drwxr-xr-x 28 root root 4.0K 4월 19 16:12 ..
-rw------- 1 root root 1.5K 4월 19 16:12 .ICEauthority
생략
[root@linux220 ~]# ls -a \
> -l
합계 224K
drwxr-x--- 20 root root 4.0K 4월 19 17:35 .
drwxr-xr-x 28 root root 4.0K 4월 19 16:12 ..
-rw------- 1 root root 1.5K 4월 19 16:12 .ICEauthority
생략
> -l
합계 224K
drwxr-x--- 20 root root 4.0K 4월 19 17:35 .
drwxr-xr-x 28 root root 4.0K 4월 19 16:12 ..
-rw------- 1 root root 1.5K 4월 19 16:12 .ICEauthority
생략
[root@linux220 ~]# echo "hello linux
> "
hello linux
[root@linux220 ~]# while true //while~do~done
> do
> CMD
> sleep 5
> done
bash: CMD: command not found
> do
> CMD
> sleep 5
> done
bash: CMD: command not found
PS1은 수정해서 쓰이지만 PS2는 수정해서 쓰지 않는편이다
(3-2) 시스템 환경변수
# echo $HOME
/root
/root
# cd $HOME
# pwd
/root
# pwd
/root
# echo $PWD
/root
/root
# echo $LOGNAME
root
root
# echo $TERM
xterm
xterm
# echo $LANG
ko_KR.UTF-8
ko_KR.UTF-8
# echo $USER
root
root
# echo $UID
0
0
(4) 알아두면 좋은 특수 변수
① $ : 현재쉘의 PID를 저장하고 있다. 쉘 스크립트내에서 임시 파일의 이름을 지정할 때 보통 사용이 된다.
[root@linux220 ~]# echo $$
16412
[root@linux220 ~]# ps
PID TTY TIME CMD
16295 pts/1 00:00:00 bash
16412 pts/1 00:00:00 bash
16500 pts/1 00:00:00 ps
16412
[root@linux220 ~]# ps
PID TTY TIME CMD
16295 pts/1 00:00:00 bash
16412 pts/1 00:00:00 bash
16500 pts/1 00:00:00 ps
② ? : 바로 이전 명령어의 정상 실행 여부에 대한 결과값이 들어 있다.
쉘 스크립트내에서 이전 명령어의 정상 수행 여부를 확인할 때 주로 사용된다.
쉘 스크립트내에서 이전 명령어의 정상 수행 여부를 확인할 때 주로 사용된다.
[root@linux220 ~]# ls /nodir
ls: /nodir: 그런 파일이나 디렉토리가 없음
[root@linux220 ~]# echo $? //바로 이전 명령어의 리턴값을 확인
2
[root@linux220 ~]# cd /nodir
bash: cd: /nodir: 그런 파일이나 디렉토리가 없음
[root@linux220 ~]# echo $?
1
[root@linux220 ~]# no
bash: no: command not found
[root@linux220 ~]# echo $?
127
[root@linux220 ~]# ls
Desktop anaconda-ks.cfg install.log install.log.syslog
[root@linux220 ~]# echo $? //0이면 정상수행 했다는 말
0
ls: /nodir: 그런 파일이나 디렉토리가 없음
[root@linux220 ~]# echo $? //바로 이전 명령어의 리턴값을 확인
2
[root@linux220 ~]# cd /nodir
bash: cd: /nodir: 그런 파일이나 디렉토리가 없음
[root@linux220 ~]# echo $?
1
[root@linux220 ~]# no
bash: no: command not found
[root@linux220 ~]# echo $?
127
[root@linux220 ~]# ls
Desktop anaconda-ks.cfg install.log install.log.syslog
[root@linux220 ~]# echo $? //0이면 정상수행 했다는 말
0
(실무 예) 백업 스크립트 작성시
# vi backup.sh
-------------------------------------------------------
tar cvzf /backup/backup.tar.gz /home
if [ $? -eq 0 ] ; then /* -eq : equal */
echo "success"
else
echo "error"
fi
-------------------------------------------------------
# vi backup.sh
-------------------------------------------------------
tar cvzf /backup/backup.tar.gz /home
if [ $? -eq 0 ] ; then /* -eq : equal */
echo "success"
else
echo "error"
fi
-------------------------------------------------------
[참고] /root/bin/script.sh 편하게 실행하기
■ 관리자가 생성한 쉘 스크립트(쉘 프로그램)들을 모아 놓은 디렉토리
- /root/bin or /root/shell
[root@linux220 /bin]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
PATH : 명령어들이 위치한 디렉토리를 선언할 때 사용하는 변수
# ls -al
(절대경로) # /bin/ls -al
(상대경로) # cd /bin
# ./ls -al
(절대경로) # /bin/ls -al
(상대경로) # cd /bin
# ./ls -al
[root@linux220 ~]# mkdir -p /backup
[root@linux220 ~]# mkdir -p /root/bin
[root@linux220 ~]# vi /root/bin/backup.sh
---------------------------------------------------------------------
#!/bin/bash
tar cvzf /backup/back-`date +%m%d`.tar.gz --absolute-name /test
---------------------------------------------------------------------
[root@linux220 ~]# chmod 755 /root/bin/backup.sh
[root@linux220 ~]# export PATH=$PATH:/root/bin
[root@linux220 ~]# backup.sh
/test/
[root@linux220 ~]# cd /backup ; ls
back-0419.tar.gz
[root@linux220 ~]# mkdir -p /root/bin
[root@linux220 ~]# vi /root/bin/backup.sh
---------------------------------------------------------------------
#!/bin/bash
tar cvzf /backup/back-`date +%m%d`.tar.gz --absolute-name /test
---------------------------------------------------------------------
[root@linux220 ~]# chmod 755 /root/bin/backup.sh
[root@linux220 ~]# export PATH=$PATH:/root/bin
[root@linux220 ~]# backup.sh
/test/
[root@linux220 ~]# cd /backup ; ls
back-0419.tar.gz
[root@linux220 /test]# vi script.sh
---------------------------------------------------------------------
#!/bin/bash
---------------------------------------------------------------------
#!/bin/bash
/root/bin/backup.sh
if [ $? -eq 0 ] ; then
echo "Backup Success"
else
echo "Backup Fail"
fi
---------------------------------------------------------------------
[root@linux220 /test]# chmod 755 /test/script.sh
[root@linux220 /test]# ./script.sh
/test/
/test/script.sh
Backup Success
if [ $? -eq 0 ] ; then
echo "Backup Success"
else
echo "Backup Fail"
fi
---------------------------------------------------------------------
[root@linux220 /test]# chmod 755 /test/script.sh
[root@linux220 /test]# ./script.sh
/test/
/test/script.sh
Backup Success
[root@linux220 /test]# vi /root/bin/backup.sh
---------------------------------------------------------------------
#!/bin/bash
tar cvzf /backup/back-`date +%m%d`.tar.gz --absolute-name /nodir
---------------------------------------------------------------------
[root@linux220 /test]# ./script.sh
tar: /nodir: Cannot stat: 그런 파일이나 디렉토리가 없음
tar: Error exit delayed from previous errors
Backup Fail
[root@linux220 /test]# ./script.sh > backup.log 2>&1
[root@linux220 /test]# cat backup.log
tar: /nodir: Cannot stat: 그런 파일이나 디렉토리가 없음
tar: Error exit delayed from previous errors
Backup Fail
③ ! : 바로 이전에 백그라운드로 실행한 프로세스의 PID번호가 저장 된다.
[root@linux220 /test]# sleep 300 &
[1] 16613 //[1]: Job ID, 16613: PID
[root@linux220 /test]# echo $!
16613
[root@linux220 /test]# ps
PID TTY TIME CMD
16548 pts/1 00:00:00 bash
16613 pts/1 00:00:00 sleep
16616 pts/1 00:00:00 ps
[root@linux220 /test]# sleep 400 &
[2] 16617
[root@linux220 /test]# ps
PID TTY TIME CMD
16548 pts/1 00:00:00 bash
16613 pts/1 00:00:00 sleep
16617 pts/1 00:00:00 sleep
16618 pts/1 00:00:00 ps
[root@linux220 /test]# echo $!
16617
[1] 16613 //[1]: Job ID, 16613: PID
[root@linux220 /test]# echo $!
16613
[root@linux220 /test]# ps
PID TTY TIME CMD
16548 pts/1 00:00:00 bash
16613 pts/1 00:00:00 sleep
16616 pts/1 00:00:00 ps
[root@linux220 /test]# sleep 400 &
[2] 16617
[root@linux220 /test]# ps
PID TTY TIME CMD
16548 pts/1 00:00:00 bash
16613 pts/1 00:00:00 sleep
16617 pts/1 00:00:00 sleep
16618 pts/1 00:00:00 ps
[root@linux220 /test]# echo $!
16617
④ 인자변수(Argument Variable)
$로 표시하여 인자변수의 값이 몇 번째인지 나타낼 수 있다.
[root@linux220 /test]# vi test.sh
---------------------------------------------------------------------
#!/bin/bash
---------------------------------------------------------------------
#!/bin/bash
echo $1
echo $2
echo $3
echo $2
echo $3
echo $*
echo $# //인자의 갯수
---------------------------------------------------------------------
[root@linux220 /test]# chmod 755 test.sh
[root@linux220 /test]# ./test.sh test1 test2 test3
test1
test2
test3
test1 test2 test3
3
echo $# //인자의 갯수
---------------------------------------------------------------------
[root@linux220 /test]# chmod 755 test.sh
[root@linux220 /test]# ./test.sh test1 test2 test3
test1
test2
test3
test1 test2 test3
3
[root@linux220 /test]# date
2016. 04. 19. (화) 19:31:48 KST
[root@linux220 /test]# set $(date)
[root@linux220 /test]# echo $1
2016.
[root@linux220 /test]# echo $2
04.
[root@linux220 /test]# echo $3
19.
[root@linux220 /test]# echo $4
(화)
[root@linux220 /test]# echo $5
19:32:01
[root@linux220 /test]# echo $6
KST
(5) PATH 변수
명령어를 검색할 디렉토리를 선언할 때 사용하여 절대 경로를 쓰지 않고
실행파일명만을 이용해서 사용할 수 있게한다.
which라는 명령도 PATH에 설정되어 있는 디렉토리의 파일의 경로만을 알려준다.
[root@linux220 ~]# script.sh
bash: script.sh: command not found
[root@linux220 ~]# /test/script.sh
tar: /nodir: Cannot stat: 그런 파일이나 디렉토리가 없음
tar: Error exit delayed from previous errors
Backup Fail
[root@linux220 ~]# cd /test ; ./script.sh
tar: /nodir: Cannot stat: 그런 파일이나 디렉토리가 없음
tar: Error exit delayed from previous errors
Backup Fail
bash: script.sh: command not found
[root@linux220 ~]# /test/script.sh
tar: /nodir: Cannot stat: 그런 파일이나 디렉토리가 없음
tar: Error exit delayed from previous errors
Backup Fail
[root@linux220 ~]# cd /test ; ./script.sh
tar: /nodir: Cannot stat: 그런 파일이나 디렉토리가 없음
tar: Error exit delayed from previous errors
Backup Fail
[root@linux220 /test]# export PATH=$PATH:/test
[root@linux220 /test]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:/test
[root@linux220 /test]# script.sh
tar: /nodir: Cannot stat: 그런 파일이나 디렉토리가 없음
tar: Error exit delayed from previous errors
Backup Fail
[root@linux220 /test]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:/test
[root@linux220 /test]# script.sh
tar: /nodir: Cannot stat: 그런 파일이나 디렉토리가 없음
tar: Error exit delayed from previous errors
Backup Fail
[root@linux220 /test]# vi ~/.bash_profile
[root@linux220 /test]# . ~/.bash_profile
[root@linux220 /test]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:/test:/root/bin:/test
(6) HISTTIMEFORMAT 변수
■ 메타캐릭터(Metacharacter)
'', "", ``, \, ;
''(작은 따옴표(single quotation))
""(큰 따옴표(Double quotation))
``(역 따옴표(Back quotation))
\ (역 슬래쉬(Back slash))
; (세미콜론(Semicolon))
'', "", ``, \, ;
''(작은 따옴표(single quotation))
""(큰 따옴표(Double quotation))
``(역 따옴표(Back quotation))
\ (역 슬래쉬(Back slash))
; (세미콜론(Semicolon))
■ ''(작은 따옴표(single quotation))
쉘이 해석 할 수 없도록 막아 준다.
쉘이 해석 할 수 없도록 막아 준다.
# echo $HOME
# echo '$HOME'
# echo '$HOME'
■ ""(큰 따옴표(Double quotation))
쉘이 해석 할 수 없도록 막아 준다. 단 인식되는 문자($, ``, \)들도 있다.
쉘이 해석 할 수 없도록 막아 준다. 단 인식되는 문자($, ``, \)들도 있다.
# echo $HOME
# echo "$HOME"
# echo "$HOME is my directory.????"
# echo "$HOME"
# echo "$HOME is my directory.????"
■ ``(역 따옴표(Back quotation))
쉘이 해석할 때 명령어로 인식한다. 따라서 역 따옴표 안의 내용을 실행한다.
쉘이 해석할 때 명령어로 인식한다. 따라서 역 따옴표 안의 내용을 실행한다.
# date
# echo `date` (# echo $(date))
# echo `hostname` (# echo $(hostname))
# echo "`hostname` is my hostname."
# echo `date` (# echo $(date))
# echo `hostname` (# echo $(hostname))
# echo "`hostname` is my hostname."
# touch server_`date +%m%d`.log
# tar cvzf /backup/home_`date +%m%d`.tar.gz /home
# while true
> do
> echo "------------`date`---------"
......
> sleep 2
> done
# tar cvzf /backup/home_`date +%m%d`.tar.gz /home
# while true
> do
> echo "------------`date`---------"
......
> sleep 2
> done
■ \ (역 슬래쉬(Back slash))
바로 이후에 있는 문자을 쉘이 해석 할 수 없도록 막아 준다.
바로 이후에 있는 문자을 쉘이 해석 할 수 없도록 막아 준다.
# echo $HOME
# echo \$HOME (# echo '$HOME')
# find / -name core -type f -exec rm -f {} \;
# find / \( -perm -4000 -o -perm -2000 \) -type f
# echo \$HOME (# echo '$HOME')
# find / -name core -type f -exec rm -f {} \;
# find / \( -perm -4000 -o -perm -2000 \) -type f
[참고] \(역 슬래쉬)
# \CMD
# CMD\ //다음줄에 이어서 대신 그 줄의 마지막이어야한다 뒤에 공백도 오면 안돼
# \CMD
# CMD\ //다음줄에 이어서 대신 그 줄의 마지막이어야한다 뒤에 공백도 오면 안돼
# ./configure --prefix=/usr/local/apache2 --options2=.... --options3=.... .....
# ./configure —prefix=/usr/local/apache2 \
> --options2=.... \
> --options3=.... \
> --options4=....
# ./configure —prefix=/usr/local/apache2 \
> --options2=.... \
> --options3=.... \
> --options4=....
■ ; (세미콜론(Semicolon))
한개의 라인에 여러개 명령어 수행할 때 사용한다.
한개의 라인에 여러개 명령어 수행할 때 사용한다.
# ls
# date
# cal
# date
# cal
# ls ; date ; cal
# alias pps='ps -ef | head -1 ; ps -ef | grep $1’
# EDITOR=/usr/bin/vim ; export EDITOR (# export EDITOR=/usr/bin/vim)
# EDITOR=/usr/bin/vim ; export EDITOR (# export EDITOR=/usr/bin/vim)
history CMD
HISTSIZE /* 히스토리를 기록하는 스택의 크기를 지정 */
HISTFILE /* 히스토리 내용을 지속적으로 저장하는 파일 이름 지정 */
/* 기본값: ~/.bash_history */
HISTFILESIZE /* 히스토리파일의 크기 지정 */
HISTFILE /* 히스토리 내용을 지속적으로 저장하는 파일 이름 지정 */
/* 기본값: ~/.bash_history */
HISTFILESIZE /* 히스토리파일의 크기 지정 */
# history | less /* 현재까지 사용한 스택에 들어간 명령이 출력 */
# history -c /* (clear) 현재까지 사용한 명령을 삭제 */
alias
별칭(Alias)
# alias cp=’cp –i’
# alias (# alias cp)
# unalias cp
# alias cp=’cp –i’
# alias (# alias cp)
# unalias cp
# alias cp=’cp –i’
# alias mv=’mv –i’
# alias rm=’rm –i’
# alias mv=’mv –i’
# alias rm=’rm –i’
# alias vi=’/usr/bin/vim’
# alias pps=’ps –ef | head –1 ; ps –ef | $1’
# alias pps=’ps –ef | head –1 ; ps –ef | $1’
[root@linux220 ~]# alias a='cd /test && rm -rf /test/*'
[root@linux220 ~]# alias b='cp /etc/passwd file1 ; cp file1 file2 ; cp file1 file3'
[root@linux220 ~]# a
[root@linux220 /test]# b
[root@linux220 /test]# ls
file1 file2 file3
[root@linux220 ~]# alias b='cp /etc/passwd file1 ; cp file1 file2 ; cp file1 file3'
[root@linux220 ~]# a
[root@linux220 /test]# b
[root@linux220 /test]# ls
file1 file2 file3
(주의) alias 이름과 명령어 이름이 중복되는 경우
alias cp='cp -i'
/bin/cp
alias cp='cp -i'
/bin/cp
#cp file1 file2 (# cp -i file1 file2)
[root@linux220 /test]# alias cp='echo hello'
[root@linux220 /test]# alias cp
alias cp='echo hello'
[root@linux220 /test]# cp
hello
[root@linux220 /test]# alias cp
alias cp='echo hello'
[root@linux220 /test]# cp
hello
사용자 환경 파일(Bash Initialization)
(1) 환경 파일이 읽혀 지는 순서
(1-1) 로그인시에 읽혀 지는 환경 파일
■ /etc/profile
■ /etc/profile.d/*.sh
■ /etc/profile.d/*.sh
■ ~/.bash_profile (~/.bash_profile -> 파일이 없으면 -> ~/.bash_login -> 파일이 없으면 -> ~/.profile)
■ ~/.bashrc
■ /etc/bashrc
■ ~/.bashrc
■ /etc/bashrc
(1-2) 쉘이 실행 될때 마다 읽혀 지는 환경 파일
■ ~/.bashrc
■ /etc/bashrc
■ /etc/profile.d/*.sh
■ /etc/bashrc
■ /etc/profile.d/*.sh
(1-3) 로그아웃 할 때 마다 읽혀 지는 환경 파일
■ ~/.bash_logout
(2) 사용자 환경 초기값 설정
(2-1) 관리자가 일반사용자의 환경을 설정 시켜 주는 경우
■ /etc/profile : 로그인 할 때만 읽혀짐
■ /etc/bashrc : 쉘이 실행 될때 마다 읽혀짐
■ /etc/profile.d/*.sh : 쉘이 실행 될때 마다 읽혀짐
■ /etc/bashrc : 쉘이 실행 될때 마다 읽혀짐
■ /etc/profile.d/*.sh : 쉘이 실행 될때 마다 읽혀짐
(2-2) 일반 사용자가 자신의 환경을 설정 하는 경우
■ ~/.bash_profile : 로그인 할 때만 읽혀짐
■ ~/.bashrc : 쉘이 실행 될때 마다 읽혀짐
■ ~/.bashrc : 쉘이 실행 될때 마다 읽혀짐
(3) 환경파일 분석
(3-1) /etc/profie 파일
# cat /etc/profile
(3-2) /etc/profile.d 디렉토리
# cd /etc/profile.d
# ls
# cd /etc/profile.d
# ls
(3-3) $HOME/.bash_profile 파일
# cat ~/.bash_profile
# cat ~/.bash_profile
(3-4) $HOME/.bashrc 파일
# cat ~/.bashrc
# cat ~/.bashrc
(3-5) /etc/bashrc 파일
# cat /etc/bashrc
# cat /etc/bashrc
(3-6) $HOME/.bash_logout
# cat ~/.bash_logout
# cat ~/.bash_logout
[EX] 사용자 환경 파일이 읽혀 지는 순서에 대한 실습
[root@linux220 ~]# vi /etc/profile
[root@linux220 /etc/profile.d]# vi ~/.bash_profile
[root@linux220 /etc/profile.d]# vi ~/.bashrc
[root@linux220 /etc/profile.d]# vi /etc/bashrc
[root@linux220 /etc/profile.d]# vi ~/.bash_logout
[root@linux220 /etc/profile.d]# vi ~/.bash_profile
[root@linux220 /etc/profile.d]# vi ~/.bashrc
[root@linux220 /etc/profile.d]# vi /etc/bashrc
[root@linux220 /etc/profile.d]# vi ~/.bash_logout
|---> ~/.bashrc read
|---> /etc/bashrc read
|---> /etc/profile.d/*.sh read
[root@linux220 ~]# telnet localhost
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
linux220.example.com (Linux release 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:35 EDT 2010) (3)
login: root
Password:
Last login: Tue Apr 19 21:41:10 from localhost.localdomain
You have new mail.
|---> /etc/profile read
|---> /etc/profile.d/*.sh read
|---> ~/.bash_profile read
|---> ~/.bashrc read
|---> /etc/bashrc read
[root@linux220 ~]# bash
|---> ~/.bashrc read
|---> /etc/bashrc read
|---> /etc/profile.d/*.sh read
[root@linux220 ~]# exit
exit
[root@linux220 ~]# exit
logout
|---> ~/.bash_logout read
Connection closed by foreign host.
Password:
Last login: Tue Apr 19 21:41:10 from localhost.localdomain
You have new mail.
|---> /etc/profile read
|---> /etc/profile.d/*.sh read
|---> ~/.bash_profile read
|---> ~/.bashrc read
|---> /etc/bashrc read
[root@linux220 ~]# bash
|---> ~/.bashrc read
|---> /etc/bashrc read
|---> /etc/profile.d/*.sh read
[root@linux220 ~]# exit
exit
[root@linux220 ~]# exit
logout
|---> ~/.bash_logout read
Connection closed by foreign host.
(4) 환경 파일의 활용 예
(4-1) 사용자 환경 파일에 등록 될수 있는 내용들
■ 변수 설정
PATH, PS1, 사용자 정의 변수 설정(JAVA_HOME, TOMCAT_HOME, ...)
PATH, PS1, 사용자 정의 변수 설정(JAVA_HOME, TOMCAT_HOME, ...)
■ 엘리어스(Alias) 설정
alias ls='ls -hF'
alias ls='ls -hF'
■ 쉘 자체의 기능
set -o vi
set -o vi
(4-2) 선언 예 *****
# cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i' /* 기본적인 명령어에 대한 alias 선언 */
alias cp='cp -i'
alias mv='mv -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions /* /etc/bashrc 실행 */
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
---------------------------- ~/.bashrc 파일 하단에 추가 -----------------------------
# (a). 기본적인 alias
alias c='clear' /* 명령어를 짧게 사용하기 위한 설정 */
alias h='history'
alias t='/usr/kerberos/bin/telnet 172.16.9.252'
alias d='cd /test && rm -rf /test/*'
# (a). 기본적인 alias
alias c='clear' /* 명령어를 짧게 사용하기 위한 설정 */
alias h='history'
alias t='/usr/kerberos/bin/telnet 172.16.9.252'
alias d='cd /test && rm -rf /test/*'
alias grep='/bin/grep -i --color' /* 명령어 옵션 */
alias cat='/bin/cat -n'
alias df='/bin/df -h -T'
alias ls='ls -h --color=tty'
alias cat='/bin/cat -n'
alias df='/bin/df -h -T'
alias ls='ls -h --color=tty'
# (b). 필요한 경우 선언하는 alias
alias lsf='/bin/ls -al | grep ^-' /* 새로운 명령어를 위한 alias */
alias lsd='/bin/ls -al | grep ^d'
alias pps='/bin/ps -ef | head -1 ; ps -ef | grep $1'
alias ddf='/bin/df -h -F ext3 ; echo ; /bin/df -i'
alias lsf='/bin/ls -al | grep ^-' /* 새로운 명령어를 위한 alias */
alias lsd='/bin/ls -al | grep ^d'
alias pps='/bin/ps -ef | head -1 ; ps -ef | grep $1'
alias ddf='/bin/df -h -F ext3 ; echo ; /bin/df -i'
alias dir='/bin/ls -ailhF | more' -----> 윈도우즈 명령어 실행 alias
alias ipconfig='ifconfig' -----> 윈도우즈 명령어 실행 alias
alias topas='top' -----> AIX
alias bdf='df -h' -----> HP-UX
alias prstat='top' -----> Solars
alias ipconfig='ifconfig' -----> 윈도우즈 명령어 실행 alias
alias topas='top' -----> AIX
alias bdf='df -h' -----> HP-UX
alias prstat='top' -----> Solars
alias nslookup='/usr/local/bin/nslookup' /* 다른 위치의 명령어 실행 */
alias vi='/usr/bin/vim'
alias vi='/usr/bin/vim'
# (c). 편리한 기능의 alias
/*
Webserver(Apache)
/etc/httpd/conf/httpd.conf -----> Configuration File
/var/www/html -----> Source Directory
/etc/httpd/logs -----> Log Directory
*/
/*
Webserver(Apache)
/etc/httpd/conf/httpd.conf -----> Configuration File
/var/www/html -----> Source Directory
/etc/httpd/logs -----> Log Directory
*/
# 설정 파일
alias fconf='vi /etc/vsftpd/vsftpd.conf'
alias wconf='vi /etc/httpd/conf/httpd.conf'
alias vsftpd.conf='vi /etc/vsftpd/vsftpd.conf'
alias httpd.conf='vi /etc/httpd/conf/httpd.conf'
alias fconf='vi /etc/vsftpd/vsftpd.conf'
alias wconf='vi /etc/httpd/conf/httpd.conf'
alias vsftpd.conf='vi /etc/vsftpd/vsftpd.conf'
alias httpd.conf='vi /etc/httpd/conf/httpd.conf'
# 소스디렉토리 이동
alias wdir='cd /var/www/html'
alias wdir='cd /var/www/html'
# 로그파일 모니터링
alias mlog='tail -f /var/log/messages'
alias wlog='tail -f /etc/httpd/logs/access_log'
alias welog='tail -f /etc/httpd/logs/error_log'
alias slog='tail -f /var/log/secure'
alias mlog='tail -f /var/log/messages'
alias wlog='tail -f /etc/httpd/logs/access_log'
alias welog='tail -f /etc/httpd/logs/error_log'
alias slog='tail -f /var/log/secure'
/* 실무에서 많이 사용되는 로그 파일 이름 형식 */ (예) file_1210.log
# tail -f /logs/file_1210.log
# tail -f /logs/file_`date +%m%d`.log
# alias slog='tail -f /logs/file_`date +%m%d`.log'
# alias slog="view /logs/file_`date --date '1 days ago' +%m%d`.log"
# alias slog="view /logs/file_`date --date '2 days ago' +%m%d`.log"
---------------------------- ~/.bashrc 파일 하단에 추가 -----------------------------
# tail -f /logs/file_1210.log
# tail -f /logs/file_`date +%m%d`.log
# alias slog='tail -f /logs/file_`date +%m%d`.log'
# alias slog="view /logs/file_`date --date '1 days ago' +%m%d`.log"
# alias slog="view /logs/file_`date --date '2 days ago' +%m%d`.log"
---------------------------- ~/.bashrc 파일 하단에 추가 -----------------------------
프로세스(process)란? 실행중인 프로그램이다
DISK(한글.exe) ----> MEM(한글 process) ----> CPU(중앙 처리 장치)
프로세스는 프로그램이 메모리에 적재되어 실제로 실행되고 있는 상태를 말한다.
프로세스는 컴퓨터 내에서 실행중인 프로그램의 인스턴스이다.
프로세스는 컴퓨터 내에서 실행중인 프로그램의 인스턴스이다.
(1) 자식프로세스(Child Process) & 부모프로세스(Parent Process)
PID(Process Identification) : 프로세스가 시작할 때 할당받는 프로세스 식별번호
PPID(Parent Process Identification) : 부모 프로세스 식별번호 (서브 프로세스를 실행시킨 프로세스)
PID(Process Identification) : 프로세스가 시작할 때 할당받는 프로세스 식별번호
PPID(Parent Process Identification) : 부모 프로세스 식별번호 (서브 프로세스를 실행시킨 프로세스)
[root@linux220 ~]# ps
PID TTY TIME CMD
18788 pts/1 00:00:00 bash
18811 pts/1 00:00:00 ps
[root@linux220 ~]# bash
[root@linux220 ~]# ps
PID TTY TIME CMD
18788 pts/1 00:00:00 bash
18812 pts/1 00:00:00 bash
18824 pts/1 00:00:00 ps
[root@linux220 ~]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 18788 18782 0 21:43 pts/1 00:00:00 bash
root 18812 18788 0 21:48 pts/1 00:00:00 bash
root 18825 18812 0 21:48 pts/1 00:00:00 ps -f
[root@linux220 ~]# ps -ef | more
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 16:11 ? 00:00:00 init [5]
root 2 1 0 16:11 ? 00:00:04 [migration/0]
root 3 1 0 16:11 ? 00:00:00 [ksoftirqd/0]
root 4 1 0 16:11 ? 00:00:00 [watchdog/0]
root 5 1 0 16:11 ? 00:00:04 [migration/1]
root 6 1 0 16:11 ? 00:00:00 [ksoftirqd/1]
root 7 1 0 16:11 ? 00:00:00 [watchdog/1]
root 8 1 0 16:11 ? 00:00:04 [migration/2]
root 9 1 0 16:11 ? 00:00:00 [ksoftirqd/2]
root 10 1 0 16:11 ? 00:00:00 [watchdog/2]
root 11 1 0 16:11 ? 00:00:04 [migration/3]
root 12 1 0 16:11 ? 00:00:00 [ksoftirqd/3]
root 13 1 0 16:11 ? 00:00:00 [watchdog/3]
root 14 1 0 16:11 ? 00:00:00 [events/0]
root 15 1 0 16:11 ? 00:00:00 [events/1]
root 16 1 0 16:11 ? 00:00:00 [events/2]
root 17 1 0 16:11 ? 00:00:00 [events/3]
root 18 1 0 16:11 ? 00:00:00 [khelper]
root 19 1 0 16:11 ? 00:00:00 [kthread]
root 25 19 0 16:11 ? 00:00:00 [kblockd/0]
root 26 19 0 16:11 ? 00:00:00 [kblockd/1]
root 27 19 0 16:11 ? 00:00:00 [kblockd/2]
root 28 19 0 16:11 ? 00:00:00 [kblockd/3]
root 29 19 0 16:11 ? 00:00:00 [kacpid]
root 190 19 0 16:11 ? 00:00:00 [cqueue/0]
root 191 19 0 16:11 ? 00:00:00 [cqueue/1]
root 192 19 0 16:11 ? 00:00:00 [cqueue/2]
root 193 19 0 16:11 ? 00:00:00 [cqueue/3]
root 196 19 0 16:11 ? 00:00:00 [khubd]
root 198 19 0 16:11 ? 00:00:00 [kseriod]
root 279 19 0 16:11 ? 00:00:00 [khungtaskd]
root 280 19 0 16:11 ? 00:00:00 [pdflush]
root 281 19 0 16:11 ? 00:00:00 [pdflush]
root 282 19 0 16:11 ? 00:00:00 [kswapd0]
root 283 19 0 16:11 ? 00:00:00 [aio/0]
root 284 19 0 16:11 ? 00:00:00 [aio/1]
root 285 19 0 16:11 ? 00:00:00 [aio/2]
root 286 19 0 16:11 ? 00:00:00 [aio/3]
root 502 19 0 16:11 ? 00:00:00 [kpsmoused]
root 567 19 0 16:11 ? 00:00:00 [mpt_poll_0]
root 568 19 0 16:11 ? 00:00:00 [mpt/0]
root 569 19 0 16:11 ? 00:00:00 [scsi_eh_0]
데몬(Daemon)의 정의
시스템(System)을 위해 또는 서비스(Service)를 위해 백그라운드에서 동작하는 프로세스이다
EX) 웹 데몬 ----> httpd
메일 데몬 ----> sendmail
ftp 데몬 ----> vsftpd
telnet 데몬 ----> telnetd
메일 데몬 ----> sendmail
ftp 데몬 ----> vsftpd
telnet 데몬 ----> telnetd
프로세스의 정보 (Process Information)
(1) 프로세스 정보가 존재하는 디렉토리
/proc 라는 디렉토리에 각 프로세스에 해당되는 PID디렉토리들이 있다.
/proc 라는 디렉토리는 커널 메모리를 마운트 한 것이다.
/proc 라는 디렉토리에 각 프로세스에 해당되는 PID디렉토리들이 있다.
/proc 라는 디렉토리는 커널 메모리를 마운트 한 것이다.
[root@linux220 ~]# ls /proc
1 18788 2407 285 3629 3996 4252 4386 4483 5 8 fs modules uptime
10 18812 2479 286 3638 4 4254 4388 4510 502 9 ide mounts version
11 18927 2481 2883 3662 4005 4279 4392 4532 567 acpi interrupts mpt vmcore
12 19 2483 29 3677 4038 4281 4393 4534 568 asound iomem mtrr vmmemctl
13 190 2485 3 3734 4055 4313 4397 4536 569 buddyinfo ioports net vmstat
14 191 2487 3231 3744 4082 4316 4399 4544 575 bus irq partitions zoneinfo
15 192 25 3290 3757 4083 4341 4414 4550 576 cmdline kallsyms schedstat
16 193 26 3516 3758 4144 4342 4435 4665 577 cpuinfo kcore scsi
17 196 27 3518 3767 4149 4348 4450 4667 578 crypto key-users self
18 198 279 3540 3774 4150 4350 4457 4668 579 devices keys slabinfo
18702 1993 28 3543 3783 4151 4355 4459 4669 595 diskstats kmsg stat
18710 2 280 3586 3825 4152 4357 4461 4670 6 dma loadavg swaps
18740 2403 281 3597 3842 4154 4362 4463 4671 631 driver locks sys
18782 2404 282 3626 3847 4155 4363 4469 4672 657 execdomains mdstat sysrq-trigger
18786 2405 283 3627 3860 4157 4365 4475 4673 690 fb meminfo sysvipc
18787 2406 284 3628 3869 4250 4382 4481 4674 7 filesystems misc tty
[root@linux220 ~]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 18788 18782 0 21:43 pts/1 00:00:00 bash
root 18812 18788 0 21:48 pts/1 00:00:00 bash
root 18930 18812 0 21:55 pts/1 00:00:00 ps -f
1 18788 2407 285 3629 3996 4252 4386 4483 5 8 fs modules uptime
10 18812 2479 286 3638 4 4254 4388 4510 502 9 ide mounts version
11 18927 2481 2883 3662 4005 4279 4392 4532 567 acpi interrupts mpt vmcore
12 19 2483 29 3677 4038 4281 4393 4534 568 asound iomem mtrr vmmemctl
13 190 2485 3 3734 4055 4313 4397 4536 569 buddyinfo ioports net vmstat
14 191 2487 3231 3744 4082 4316 4399 4544 575 bus irq partitions zoneinfo
15 192 25 3290 3757 4083 4341 4414 4550 576 cmdline kallsyms schedstat
16 193 26 3516 3758 4144 4342 4435 4665 577 cpuinfo kcore scsi
17 196 27 3518 3767 4149 4348 4450 4667 578 crypto key-users self
18 198 279 3540 3774 4150 4350 4457 4668 579 devices keys slabinfo
18702 1993 28 3543 3783 4151 4355 4459 4669 595 diskstats kmsg stat
18710 2 280 3586 3825 4152 4357 4461 4670 6 dma loadavg swaps
18740 2403 281 3597 3842 4154 4362 4463 4671 631 driver locks sys
18782 2404 282 3626 3847 4155 4363 4469 4672 657 execdomains mdstat sysrq-trigger
18786 2405 283 3627 3860 4157 4365 4475 4673 690 fb meminfo sysvipc
18787 2406 284 3628 3869 4250 4382 4481 4674 7 filesystems misc tty
[root@linux220 ~]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 18788 18782 0 21:43 pts/1 00:00:00 bash
root 18812 18788 0 21:48 pts/1 00:00:00 bash
root 18930 18812 0 21:55 pts/1 00:00:00 ps -f
[root@linux220 ~]# gedit &
[1] 18980
[root@linux220 ~]# ps
PID TTY TIME CMD
18967 pts/1 00:00:00 bash
18980 pts/1 00:00:00 gedit
18981 pts/1 00:00:00 ps
[root@linux220 ~]# ls -ld /proc/18980
dr-xr-xr-x 5 root root 0 4월 19 21:56 /proc/18980
[root@linux220 ~]# ps
PID TTY TIME CMD
18967 pts/1 00:00:00 bash
18986 pts/1 00:00:00 ps
[1]+ Done gedit
[root@linux220 ~]# ls -ld /proc/18980
ls: /proc/18980: 그런 파일이나 디렉토리가 없음
프로세스 관리 (백그라운드와 포그라운드 관리 - &, bg, fg, jobs)
(프로그램이 실행되는 형태)
(fg) # ls (# gedit )
(bg) # ls & (# gedit &)
(fg) # ls (# gedit )
(bg) # ls & (# gedit &)
그림2
(fg 형태로 프로그램이 실행되면)
[root@linux220 ~]# ps
PID TTY TIME CMD
18967 pts/1 00:00:00 bash
18998 pts/1 00:00:00 ps
[root@linux220 ~]# gedit
ls
date
-> 명령어 창으로 돌아와서 "# ls" 명령어를 수행해도 명령어가 출력결과가 보이지 않는다.
-> 부모 프로세스(bash)가 종료되면 자식 프로세스가 종료된다.
-> 부모 프로세스(bash)가 종료되면 자식 프로세스가 종료된다.
(bg 형태로 프로그램이 실행되면)
[root@linux220 ~]# ps
PID TTY TIME CMD
19021 pts/1 00:00:00 bash
19035 pts/1 00:00:00 ps
[root@linux220 ~]# gedit &
[1] 19036
[root@linux220 ~]# ls
Desktop anaconda-ks.cfg bin install.log install.log.syslog
[root@linux220 ~]# date
2016. 04. 19. (화) 22:03:43 KST
[root@linux220 ~]# ps
PID TTY TIME CMD
19021 pts/1 00:00:00 bash
19036 pts/1 00:00:00 gedit
19039 pts/1 00:00:00 ps
-> 명령어 창으로 돌아와서 "# ls" 명령어를 수행하면 정상적으로 수행이 된다.
-> 부모 프로세스(bash)가 종료되었다고 해서 자식 프로세스가 종료되지 않는다.(# exit 명령어 사용)
터미널 윈도우 오른쪽 위에 X 버튼으로 종료하면 차일드프로세스도 같이 종료된다 왜지??
-> 부모 프로세스(bash)가 종료되었다고 해서 자식 프로세스가 종료되지 않는다.(# exit 명령어 사용)
터미널 윈도우 오른쪽 위에 X 버튼으로 종료하면 차일드프로세스도 같이 종료된다 왜지??
[root@linux220 ~]# cat &
[1] 19088
[root@linux220 ~]# ps -ef | grep cat
root 4532 1 0 16:13 ? 00:00:00 /usr/libexec/notification-area-applet --oaf-activate-iid=OAFIID:GNOME_NotificationAreaApplet_Factory --oaf-ior-fd=21
root 4550 1 0 16:13 ? 00:00:00 /usr/libexec/notification-daemon
root 19088 19072 0 22:06 pts/1 00:00:00 cat
root 19090 19072 0 22:06 pts/1 00:00:00 grep -i --color cat
[1] 19088
[root@linux220 ~]# ps -ef | grep cat
root 4532 1 0 16:13 ? 00:00:00 /usr/libexec/notification-area-applet --oaf-activate-iid=OAFIID:GNOME_NotificationAreaApplet_Factory --oaf-ior-fd=21
root 4550 1 0 16:13 ? 00:00:00 /usr/libexec/notification-daemon
root 19088 19072 0 22:06 pts/1 00:00:00 cat
root 19090 19072 0 22:06 pts/1 00:00:00 grep -i --color cat
[1]+ Stopped cat
[root@linux220 ~]# ls -ld /proc/19088
dr-xr-xr-x 5 root root 0 4월 19 22:06 /proc/19088
[root@linux220 ~]# jobs
[1]+ Stopped cat
[root@linux220 ~]# fg %1
cat
[root@linux220 ~]# ls -ld /proc/19088
dr-xr-xr-x 5 root root 0 4월 19 22:06 /proc/19088
[root@linux220 ~]# jobs
[1]+ Stopped cat
[root@linux220 ~]# fg %1
cat
[root@linux220 ~]# ls -ld /proc/19088
ls: /proc/19088: 그런 파일이나 디렉토리가 없음
ls: /proc/19088: 그런 파일이나 디렉토리가 없음
[EX2] 포그라운드 & 백그라운드 실행 실습
[root@linux220 ~]# sleep 500
<Ctrl + Z>
[1]+ Stopped sleep 500
[root@linux220 ~]# jobs
[1]+ Stopped sleep 500
[root@linux220 ~]# sleep 600
<Ctrl + Z>
[1]+ Stopped sleep 500
[root@linux220 ~]# jobs
[1]+ Stopped sleep 500
[root@linux220 ~]# sleep 600
[2]+ Stopped sleep 600
[root@linux220 ~]# sleep 700
[root@linux220 ~]# sleep 700
[3]+ Stopped sleep 700
[root@linux220 ~]# jobs
[1] Stopped sleep 500
[2]- Stopped sleep 600
[3]+ Stopped sleep 700
[root@linux220 ~]# bg %1
[1] sleep 500 &
[root@linux220 ~]# bg %2
[2]- sleep 600 &
[root@linux220 ~]# bg %3
[3]+ sleep 700 &
[root@linux220 ~]# jobs
[1] Running sleep 500 &
[2]- Running sleep 600 &
[3]+ Running sleep 700 &
[root@linux220 ~]# jobs
[1] Stopped sleep 500
[2]- Stopped sleep 600
[3]+ Stopped sleep 700
[root@linux220 ~]# bg %1
[1] sleep 500 &
[root@linux220 ~]# bg %2
[2]- sleep 600 &
[root@linux220 ~]# bg %3
[3]+ sleep 700 &
[root@linux220 ~]# jobs
[1] Running sleep 500 &
[2]- Running sleep 600 &
[3]+ Running sleep 700 &
[root@linux220 ~]# fg %1
sleep 500
sleep 500
[root@linux220 ~]# fg %2
sleep 600
sleep 600
[root@linux220 ~]# fg %3
sleep 700
sleep 700
[root@linux220 ~]# jobs
언제 bg(&) 사용하냐?
오래 돌아가는 프로그램(백업), gui형태 프로그램(gedit)
이런 경우를 제외하면 fg로 작동한다
'모의해킹 침해대응 전문가 과정' 카테고리의 다른 글
20160420 리눅스 관리자 (0) | 2016.04.21 |
---|---|
20160420 리눅스 기초 (0) | 2016.04.21 |
20160418 리눅스 기초 (0) | 2016.04.19 |
20160415 리눅스 기초 (0) | 2016.04.15 |
20160414 리눅스 기초 (0) | 2016.04.15 |