TIL 2021/9/30

Park Choong Ho·2021년 9월 30일
0

Missing Semester 1강

파일 권한

missing:~$ ls -l /home
drwxr-xr-x 1 missing  users  4096 Jun 15  2019 missing

맨앞에 "d"는 폴더임을 의미(directory) "-"는 그냥 파일을 의미
뒤에 9글자는 권한을 의미
앞에 3개는 소유자
중간 3개는 소유자가 속한 그룹
마지막 3개는 모든 사용자
각각 rwxr은 읽기 w는 쓰기 x는 실행 권한을 의미(폴더의 경우, r은 폴더요소들을 보는 것, w는 그 요소들을 추가하거나 삭제하는 것, x는 해당 폴더에 들어가는 것)
-로 표시되어 있으면 해당 권한이 없다는 말

스트림 연결

프로그램은 두개의 주요 stream을 가집니다. (input, output stream)

이러한 input, ouput stream을 프로그램에 redirect해서 활용할 수 있습니다.

< file, > file 이 2가지가 있습니다.

linux:~$ echo hello > hello.txt
linux:~$ cat hello.txt
hello
linux:~$ cat < hello.txt
hello
linux:~$ cat < hello.txt > hello2.txt
linux:~$ cat hello2.txt
hello

있는 파일에 붙히고 싶은 경우에는 >> 를 사용합니다.

linux:~$ echo park >> hello.txt
linux:~$ cat hello.txt
hello
park

|| 기준 왼쪽 프로세스의 output을 오른쪽 프로세스의 input과 연결해 줍니다.

linux:~$ ls -l | tail -n1
drwxrwxr-x 3 ubuntu ubuntu 4096 Sep 27 05:56 jungle
linux:~$ ls -l
total 12
-rw-rw-r-- 1 ubuntu ubuntu   11 Sep 30 13:20 hello.txt
-rw-rw-r-- 1 ubuntu ubuntu    6 Sep 30 13:18 hello2.txt
drwxrwxr-x 3 ubuntu ubuntu 4096 Sep 27 05:56 jungle
linux:~$ curl --head --silent google.com
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 30 Sep 2021 13:29:01 GMT
Expires: Sat, 30 Oct 2021 13:29:01 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN

linux:~$ curl --head --silent google.com | grep --ignore-case 219
Content-Length: 219

root 권한

$ sudo find -L /sys/class/backlight -maxdepth 2 -name '*brightness*'
/sys/class/backlight/thinkpad_screen/brightness
$ cd /sys/class/backlight/thinkpad_screen
$ sudo echo 3 > brightness
An error occurred while redirecting file 'brightness'
open: Permission denied

일반적으로 sudo 명령은 시스템상에서 절대적인 권한을 가지며 모든 파일을 읽고 쓰고 변경할 수 있습니다. 그런데 위 예시에서는 에러가 발생했습니다.

|, >, < 과 같은 작업들은 쉘에 의해서 실행됩니다. 위 경우는 쉘 프로그램이 brightness 파일에 쓰려고 하는 상황이기 떄문에 쉘 프로그램이 root 권한으로 실행되지 않았다면 접근이 제한됩니다.

$ echo 3 | sudo tee brightness

tee 프로그램은 특정 파일을 쓰기 위해 여는 프로그램이며 root 권한으로 실행했기에 위 예시는 올바르게 동작합니다.

4번 문제

$:/tmp/missing$ echo -e '#!/bin/sh\ncurl --head --silent https://missing.csail.mit.edu' > semester
$:/tmp/missing$ cat semester
#!/bin/sh
curl --head --silent https://missing.csail.mit.edu

echo 프로그램에서 개행 문자를 인식하기 위해서는 -e 옵션을 넘겨주어야 합니다.

profile
백엔드 개발자 디디라고합니다.

0개의 댓글