필수 도구 이해 및 사용

hahic·2023년 3월 2일
0

RHCSA

목록 보기
1/10
post-thumbnail

Shell prompt 접속

로그인 모드

  • Local text-mode console
  • Local graphical-mode console
  • Remote text-mode login
  • Remote graphical-mode console

Remote text-mode login

  • SSH (Secure SHell)
  • telnet

SSH VS telnet

  • 공통점
    원격 컴퓨터에 접속하기 위한 프로토콜

  • 차이점
    1. ssh가 telnet보다 보안적으로 안전
    ssh는 데이터를 암호화하여 전송. 반면, telnet은 암호화하지 않음
    따라서, telent은 패킷 스니핑 등의 공격에 노출되기 쉬움
    2. ssh: 22 port / telnet: 23 port

명령어

  • ip 확인 명령어
    ip a
  • ssh 접속 명령어
    ssh <user-name>@<ip-address>

도구

  • Console
  • Virtual Terminal
  • Terminal Emulator

시스템 문서 사용법

  • --help
  • man
  • apropos / mandb - (시스템 문서 업데이트)
  • systemctl list-dependencies

폴더와 파일 생성, 삭제, 복사, 이동

파일 시스템

파일 시스템은 트리 구조로 이루어져 있음

명령어

디렉토리 이동

  • cd - Change Directory

파일, 폴더 확인

  • ls -al
    ls-alh - h: Human readable format
    ls -ltr
    ls --full-time - (정확한 수정 시간 출력)
  • pwd - Print Working Directory
  • stat- (파일 또는 디렉토리 상세한 정보 출력)

파일, 폴더 생성

  • touch <file-name> - (파일 생성)
  • mkdir <directory-name> - Make Directory (폴더 생성)
    mkdir -p /tmp/1/2/3/4/5/6/7/8 - p: (존재하지 않는 부모 폴더가 있는 경우, 자동 생성)

파일, 폴더 삭제

  • rm <source>
    rm -r <source>

파일, 폴더 복사

  • cp <source> <destination> - Copy
    cp -r <source> <destination> - Recursive
    cp -a <source> <destination> - (복사할 파일이나 디렉토리의 속성을 그대로 유지하면서 복사)

cp -r <source><destination>

파일, 폴더 이동

  • mv <source> <destination> - Move
    mv /home/bob/lfcs/* /home/bob/new-data/

Hard Links, Soft Links

: 원본 파일에 대한 다른 이름을 가지는 파일

  • 파일에만 가능 (폴더는 불가능)
  • 같은 파일 시스템의 파일에만 가능

  • ln <source> <destination> - Link

: 원본 파일에 대한 포인터 역할을 하는 파일

  • 파일, 폴더 둘 다 가능
  • 다른 파일 시스템에도 가능

  • ln -s <source> <destination> - Link Soft
  • Hard Links는 원본 파일과 같은 파일을 추가로 생성
    반면, Soft Links는 원본 파일과는 다른 파일 생성
  • Hard Links는 동일한 파일을 참고
    반면, Soft Links는 파일의 경로를 참조하므로, 다른 파일도 참조 가능

파일 권한 목록, 설정, 변경

파일 유형

권한 유형

명령어

  • chown - change owner
    ex) chown jane family_dog.jpg
    ex) chown aaron:family family_dog.jpg
  • chgrp - change group
    ex) chgrp wheel family_dog.jpg
  • chmod - change mode
    ex) chmod u+w family_dog.jpg
    ex) chmod o-r family_dog.jpg
    ex) chmod g=r family_dog.jpg

SUID, SGID, and sticky bit

SUID(Set User ID)

: 파일 실행 시에 일시적으로 해당 파일의 소유자 권한으로 실행
특정한 권한이 필요한 명령어를 일반 사용자가 실행해야 할 때 사용됨

명령어

  • chmod u+s filename
  • chmod 4XXX <file-name>

SGID(Set Group ID)

: 파일 실행 시에 일시적으로 해당 파일의 파일의 그룹 권한으로 실행
여러 사용자가 공통으로 사용하는 디렉토리를 설정할 때 사용됨

명령어

  • chmod g+s <file-name>
  • chmod 2XXX <file-name>

Sticky bit

: Sticky bit가 설정된 디렉토리는 해당 디렉토리 내의 파일을 수정하거나 삭제할 때, 파일 소유자나 디렉토리 소유자 혹은 root 권한이여만 가능. 주로 여러 사용자가 공통으로 사용하는 디렉토리에서 다란 사용자가 파일을 삭제하지 못하도록 보호할 때 사용

명령어

  • chmod o+t <file-name>
  • chmod 1XXX <file-name>

파일 검색

find

: 지정한 디렉토리 내에서 파일을 검색하는데 사용하는 명령어

find <path-to-directory> <search-parameters>

<search-parameters>

  • -name - (이름이나 패턴을 사용하여 파일 검색)
  • -iname - (-name과 유사하지만 대소문자를 구별하지 않고 파일 검색)
  • -mmin - modified minute (n분에 마지막으로 수정된 파일 검색)
  • -mtime - modified time (n*24시간에 마지막으로 수정된 파일 검색)
  • -cmin - change minute (n분에 마지막으로 권한 수정된 파일 검색)
  • -ctime - change time (n*24시간에 마지막으로 권한 수정된 파일 검색)
  • -size
    - c: bytes
    - k: kilobytes
    - M: megabytes
    - G: gigabytes
    ex) sudo find /usr -type f -size +5M -size -10M
  • -perm - permission
    ex) find -perm u=rw,g=rw,o=r - (정확하게 644 권한인 파일 검색)
    ex) find -perm -u=rw,g=rw,o=r - (적어도 644 권한인 파일 검색)
    ex) find -perm /u=rw,g=rw,o=r
  • -type
    - b: file
    - c: character special file
    - d: directory
    - l: symbolic link
    - p: FIFO
    - f: regular file
    - s: socket
    ex) sudo find /var/ -type d -name pets
    ex) sudo find /var -type f -perm 0770
    ex) sudo find /usr -type f -mmin -120 | wc -l
  • -n: n 단위 이내에 수정된 파일 검색
  • +n: n 단위보다 이전에 수정된 파일 검색

search expression

  • -not
  • \!
  • -o - OR operator

grep

grep <option> <source>

<option>

  • -i - (특정 문자열을 대소문자 구분 없이 검색)
  • -v - (특정 문자열을 제외한 나머지 행 검색)
  • -r - (하위 디렉토리를 포함한 모든 파일에서 검색)
  • -A - (특정 문자열로부터 num 이후의 라인까지 출력)
  • -B - (특정 문자열로부터 num 이전의 라인까지 출력)
  • -C - (특정 문자열로부터 num 이전,이후의 라인까지 출력)
  • -c - (일치하는 행의 수만 출력)
  • -d - (특정 디렉토리에서 특정 문자열 검색)
  • -w - (단어와 일치하는 행만 출력)
  • -E = egrep - (찾을 패턴을 정규 표현식으로 검색)

정규표현식

  • ^ - (the line begins with)
  • $ - (the line ends with)
  • \ - (escaping for special characters)
  • . - (match any one character)
  • * - (match the previous element 0 or more times)
  • + - (match the previous element 1 or more times)
  • {min, max} - (최소 min개 이상, 최대 max개 이하)
  • | - (or)
  • [] - (ranges or sets)
  • [^] - (negated ranges or sets)
  • () - (subexpressions)

파일 내용 확인, 변경 및 비교

파일 내용 확인

  • cat
  • tac
  • tail / tail -n X
  • head / head -n X

파일 내용 변경

  • sed - stream editor
    sed -i
    sed -i --in-place
    ex) sed 's/canda/canada/g userinfo.txt
    ex) sed 's/canda/canada/gi userinfo.txt
    ex) sed -i '500,2000s/enabled/disabled/g' values.conf
    ex) sed -i 's~#%$2jh//238720//31223~$2//23872031223~g' /home/bob/data.txt
  • cut
    - -d - (필드 구분 기호로 TAB 대신 DELIM 사용)
    - -f - (특정 필드만 선택)
    ex) cut -d ' ' -f 1 userinfo.txt
  • uniq
  • sort
    - -d: alpha 순서대로 정렬
    - -u: uniq
    - -f: 대소문자 구별하지 않음

grep -e 옵션을 사용한 output은 redirection 불가능
egrep은 가능하므로, 정규표현식을 이용하여 검색할 경우 egrep 사용하는것을 권장

파일 내용 비교

  • diff - differences
    diff -c - c: context
    diff -y = sdiff: side-by-side diff
    diff -i

VI, VIM

문서 보기

  • less
  • more

vi 명령어

  • yy - (복사)
  • p - (붙여넣기)
  • o - (다음 라인에 삽입)
  • dd - (한 라인 삭제)
  • x - (한 글자 삭제)
  • :set number - (vi 줄 번호 표시)

파일 압축, 압축 해제

tar - tap archive

  • tar cvf = tar --create --verbose --file
  • tar xvf = tar --extract --verbose --file
  • tar tf = tar --list --file
  • tar rf = tar --append --file
  • tar cvfzP = tar --create --verbose --file --zip --preserve-permissions
  • tar --extract --file --directory = tar xf -C

ex) tar --create --gzip --file archive.tar.gz file = tar czf archive.tar.gz file
ex) tar --create --bzip2 --file archive.tar.bz2 file = tar cjf archive.tar.bz2 file
ex) tar --create --xz --file archive.tar.xz file = tar cJf archive.tar.xz file
ex) tar --create --autocompress --file archive.tar.gz file

gzip

  • gzip <file>
    gzip -k, --keep
    gzip -l, --list
    gzip -r
  • gunzip <file> = gzip -d <file> - d: decompress

bzip2

  • bzip2 <file>
  • bunzip <file> = bzip2 -d <file>

xz

  • xz <file>
  • unxz <file> = xz -d <file>

stdin, stout, stderr

  • /dev/null
  • 2>&1

기호 '>'

> <file-name>

  • Redirection
  • Overwrite

기호 '>>'

>> <file-name>

  • Redirection
  • append

star

: Standard TAR"의 약자로, 파일과 디렉토리를 아카이브 파일로 묶거나 푸는 작업을 수행하는 명령어

star <option> file=<file-or-directory>

<option>

  • -c: 파일과 디렉토리를 아카이브 파일 압축
  • -x: 아카이브 파일 압축 해제
  • -f: 아카이브 파일 이름 지정
  • -v: 아카이브 파일의 내용 출력
  • -z: gzip으로 아카이브 파일 압축
  • -bz: bzip2으로 아카이브 파일 압축
  • -Z: compress로 아카이브 파일 압축

(보안) 다른 시스템으로 파일 전송

scp - secure copy

: SSH(보안 셸) 프로토콜을 사용하여 로컬 시스템과 원격 시스템 간에 파일을 복사하는 명령어

scp <option> <remote-user>@<remote-host>:<remote-file-path> <local-file-path>

<option>

  • -P: 원격 호스트의 SSH 포트 지정
  • -r: 디렉토리 전체를 복사할 때 사용
  • -p: 파일의 권한 및 타임스탬프 정보 보존
  • -v: 디버그 정보 출력

stfp - ssh file transfer protocol

: SSH(Secure Shell) 프로토콜을 이용하여 파일을 전송하기 위한 프로토콜

sftp <option> <remote-user>@<remote-host>

  • get <file\>
  • get -r <directory\>
  • put <file\>
  • put -r <file\>
profile
👩‍💻 Junior Backend Developer

0개의 댓글