。゚(*´□`)゚。

코딩의 즐거움과 도전, 그리고 일상의 소소한 순간들이 어우러진 블로그

[네이버클라우드] 클라우드 기반의 개발자 과정 7기/리눅스

주짓수 갈까말까 금요일 5일 차!

quarrrter 2023. 4. 28. 18:40

잊지 말자 난, root다. #이다. 관리자다.
설정을 마음대로 할 수도, 시스템을 망가트릴 수도 있다..

*오늘 과제*
시스템 자원(패키지, 프로세스 등)을 관리해야 한다.
사용자, 권한(소유권, 허가권)을 관리한다.

 


목차:

useradd, adduser 차이

tty

group

사용자 용량 확인 및 제어하기 (du, quota)

파일 권한 (3개씩 끊어읽기, chmod, umask)

mysql 


nl /etc/passwd  : 계정 보기(쉘 정보확인 가능)   //tail -3 /etc/passwd 최신 목록보기 

 

★user 추가 및 쉘 변

/bin/sh :본쉘 

useradd k1     /bin/sh :본쉘 
adduser k2        /bin/bash :bash쉘
passwd k1

 

chsh  쉘 변경 (k1 의 쉘/본쉘을 bash쉘로)

[k1]$ chsh
Changing shell for bible.
암호: ********
New shell [/bin/bash]: /bin/sh    
Shell changed.

echo $SHELL 쉘확인하기

 

useradd -D  기본값확인

userdel kbs  kbs 계정만 삭제

userdel -r mbc   홈디렉터리까지 삭제
nl /etc/default/useradd
useradd -D -s /bin/bash   / 본쉘을 bash로 바꾸기  //다른 방법: vi 로 수정

useradd할때 home 만드는 설정 추가하기
vi /etc/login.defs  => 12번줄에 CREATE_HOME yes 추가

 


★tty란 ,,, 터미널임 

k2@psh:~$ who-a
j        tty1         2023-04-28 00:56
j        pts/0        2023-04-28 00:56 (10.0.2.2)
j        pts/1        2023-04-28 00:57 (10.0.2.2)
k1       pts/2        2023-04-28 01:18 (10.0.2.2)
k2       pts/3        2023-04-28 01:13 (10.0.2.2)

k2@psh:~$ tty
/dev/pts/3             => 내 현재 터미널은 3번임 

root@psh:~# tty
/dev/pts/1             => 루트 터미널은 1번 

루트에서
echo "오늘은 금요일. 신나~ "     스탠다드 1이어서 루트창에 나옴 
echo "오늘은 금요일. 신나~ " >/dev/pts/3 
k2에 출력됨 

 


★group 추가 및 설정 
groupadd KING

root@psh:~# groups k2
k2 : k2
root@psh:~# groups k1
k1 : k1


root@psh:~# nl /etc/group
    64  k1:x:1001:
    65  k2:x:1002:
    66  KING:x:1003:


root@psh:~# usermod -G KING k2
root@psh:~# usermod -G KING k1


root@psh:~# nl /etc/group

    64  k1:x:1001:
    65  k2:x:1002:
    66  KING:x:1003:k2,k1


★사용자 용량 확인 


k6@psh:~$ du -sh ~         현재 사용 용량 
32K     /home/k6


k6@psh:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           198M  1.1M  197M   1% /run
/dev/sda4        12G  4.3G  6.9G  39% /
tmpfs           988M     0  988M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/sda2       974M  130M  777M  15% /boot
/dev/sda5       4.9G  3.6G  1.1G  77% /home
/dev/sda6       9.8G   24K  9.3G   1% /data
tmpfs           198M  4.0K  198M   1% /run/user/1000
tmpfs           198M  4.0K  198M   1% /run/user/1001
tmpfs           198M  4.0K  198M   1% /run/user/1002
tmpfs           198M  4.0K  198M   1% /run/user/1006

4.9G /home 에서 500메가 넘는 파일 찾기! 


root@psh:~# find / -size +500M -exec ls -al {} \;
-rw-rw-r-- 1 k2 k2 3758096384 Apr 28 01:30 /home/k2/a2
-r-------- 1 root root 140737471590400 Apr 28 00:56 /proc/kcore
find: ‘/proc/3545/task/3545/fd/6’: No such file or directory


root@psh:~# rm -rf /home/k2/a2

 

★제어하기 quota

 


root@psh:~# vi /etc/fstab
(12번째 줄에 /home에 usrquota 추가)   >> 기본 설정 

root@psh:~# mount | grep /home
/dev/sda5 on /home type ext4 (rw,relatime)
root@psh:~# mount -o remount /home
root@psh:~# mount | grep /home
/dev/sda5 on /home type ext4 (rw,relatime,quota,usrquota)


root@psh:~# ls /home
it1  it2  it3  j  k1  k2  k5  k6  lost+found
root@psh:~# quotacheck -aug                          >>쿼타 켜
root@psh:~# ls /home
aquota.user  it1  it2  it3  j  k1  k2  k5  k6  lost+found

=>aquota.user   쿼타를 설정하는 파일 

 


root@psh:~# repquota -a                               >>쿼타 내용보
*** Report for user quotas on device /dev/sda5
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      24       0       0              3     0     0
j         --      28       0       0             10     0     0
k2        --      24       0       0              7     0     0
it1       --      28       0       0              7     0     0
it2       --      28       0       0              7     0     0
it3       --      64       0       0             17     0     0
k6        --      32       0       0              9     0     0

 


root@psh:~# repquota -t k6                      >>k6 쿼타 수정

소프트 20000블럭   // 20M
hard 25000블럭 수정하기  //25M

 


root@psh:~# repquota -a
*** Report for user quotas on device /dev/sda5
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      24       0       0              3     0     0
j         --      28       0       0             10     0     0
k2        --      24       0       0              7     0     0
it1       --      28       0       0              7     0     0
it2       --      28       0       0              7     0     0
it3       --      64       0       0             17     0     0
k6        --      32   20000   25000              9     0     0

끝, k6가서 확인하기 
k6@psh:~$ whoami
k6

k6@psh:~$ du -sh ~
36K     /home/k6

k6@psh:~$ quota
Disk quotas for user k6 (uid 1006):
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/sda5      36   20000   25000              10       0       0

 

 

*실습*
root@psh:~# edquota -u it1
it1 용량제어하기

root@psh:~# repquota -a
*** Report for user quotas on device /dev/sda5
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      24       0       0              3     0     0
j         --      28       0       0             10     0     0
k2        --      24       0       0              7     0     0
it1       --      28   50000   60000              7     0     0
it2       --      28       0       0              7     0     0
it3       --      64       0       0             17     0     0
k6        +-   25000   20000   25000  6days      26     0     0


root@psh:~# edquota -p it1 it2 it3                //it1 quota내용을 it2, it3에 복사
root@psh:~# repquota -a
*** Report for user quotas on device /dev/sda5
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      24       0       0              3     0     0
j         --      28       0       0             10     0     0
k2        --      24       0       0              7     0     0
it1       --      28   50000   60000              7     0     0
it2       --      28   50000   60000              7     0     0
it3       --      64   50000   60000             17     0     0
k6        +-   25000   20000   25000  6days      26     0     0

 

 

 


★파일 권한 

 

root@psh:~# ll /usr/bin/date
-rwxr-xr-x 1 root root 104960 Feb  7  2022 /usr/bin/date*

 

파일의 형식

-rw-r--r-- 1 root root 127 NOV 14 07:50 tt
권한 하드링크 수 소유주 소유그룹 size 접근날짜 파일명

소유권과 권한 (3개씩 끊어 읽음)

drw/x---/--- 
주인/그룹/나머지

d r w x r - w r - x
- r w - r - - r - -
파일분류 소유주(usr) 그룹(group) 타인(others)

권한 읽기

기호 숫자 의미
r 2^2=4 read only
w 2^1=2 write 권한 (생성, 삭제, 수정)
x 2^0=1 file excute 실행권한, directory 진입권한
- - 권한 없음 

소유주 만들기 : chwon

그룹 만들기: chgrp

 

소유주 변경 : chwon kbs aa.txt   \  chwon -R kbs /bb    >> bb 디렉토리 + 이하 파일들의 소유권도 변경 

그룹 변경: chgrp blue aa.txt     

소유주, 그룹 한 번에 변경 : chwon kbs.blue cc = chwon kbs:blue cc 

 

▶권한 변경 chmod // 기호 모드는 잘 사용하지 않고 숫자 모드를 사용 

chmod u=+x dd // dd파일의 소유주에게 실행 권한 추가 

chmod +x dd // dd 파일의 user,group,other 에게 실행 권한 추가 

chmod 744 dd  // dd파일의 권한을 744로 변경

chmod 644 /yyy/*    // /yyy디렉터리 이하 모든 파일을 644로 변경 

 

**사용자에서 passwd 바꿀 수 있는 이유가 바꾸는 순간 root 권한이 부여됨. 그래서 수정 가능

chmod 75 /usr/bin/passwd    : 일반 사용자 $는 pw 변경 불가

 

★umask

사용자가 파일 생성시 자동으로 권한을 조정해주는 값

(현재 umask 값 보기 :  umask -S)

루트의 기본 값 : 0022

일반 사용자 기본 값: 0002 

 

전체권한      777
umask     -   022
------------------------------
                   755          - 디렉토리 권한
x권한      -   111 
=================
                   644         - 파일 권한

 

 

★ 파일 속성 확인 및 변경 

238  chattr +i a2          //i 넣어서 지우기 방지
  239  lsattr              // 파일 속성확인, i 추가된 것 확인 가능 
  240  rm -f a2            // 안 지워짐 
 244  chattr -i a2           // i 삭제 
  245  lsattr              //  i 없어진거확인
  246  rm -f a2             // a2 지우기 가능 

 


mysql 하기 

 

root 에서 

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> alter user root@localhost identified \                mysql 비번 바꿈
    -> with mysql_native_password by 'jj'
    -> ;
Query OK, 0 rows affected (0.01 sec)

mysql> create user myk2@localhost identified \                 mysql myk2 계정을 만듬
    -> with mysql_native_password by 'j';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on k2DB.* to myk2@localhost;          k2DB에 대해 모든 권한 허용
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)



k2에서
k2@psh:~$ mysql -u myk2 -p
비번 입력 j 접속 ~ 


mysql> create database k2DB
    -> ;
Query OK, 1 row affected (0.00 sec)

mysql> use k2DB
Database changed
mysql> show table;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1


mysql> create table Man(
    -> name char(20),
    -> age int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into Man values('손흥민', 32);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Man values('이강인', 25);
Query OK, 1 row affected (0.01 sec)

mysql> select * from Man;
+-----------+------+
| name      | age  |
+-----------+------+
| 손흥민    |   32 |
| 이강인    |   25 |
+-----------+------+
2 rows in set (0.00 sec)