본문 바로가기
리눅스/서버운영

[리눅스] 파티션 복사 실습⑧ (완결편)

by 상달군 2024. 1. 19.
728x90

✔ 부팅중 에러 발생

 

[FAILED] Failed to start Journal Service.
See 'systemctl status systemd-journald.service' for details.

 

에러를 추적 결과 SELinux에서 거부 되어 데몬을 실행하지 못하는것이였다.


✔ SELinux의 로그를 확인

 

SELinux의 로그 /var/log/audit/audit.log에서  거부 당하고 있다는것을 알았고 그렇기 때문에 SELinux를 비활성화하게 되면 부팅이 가능하다는것을 알았다. 그치만 항상 비활성화를 해두고 쓸수는 없는것이다. 

 

🔻audit의 에러 내용 

[root@localhost ~]# cat /var/log/audit/audit.log |grep AVC

type=AVC msg=audit(1705384805.800:287): avc:  denied  { search } for  pid=1707 comm="iscsiadm" name="/" dev="sdb2" ino=128 scontext=system_u:system_r:iscsid_t:s0 tcontext=system_u:object_r:unlabeled_t:s0 tclass=dir permissive=0

 

✨ log에서  tcontext=system_u:object_r:unlabeled_t:s0 이부분이 힌트가 되었다 !


✔ SELinux에 대해서 공부

 

SELinux의 보안 컨텍스트라는것이 있는데 컨텍스트는 접근 권한을 확인하는데 사용되고 있으며 SELinux 를 이해하기 위한 핵심 요소 각 필드는 아래와같은 의미를 갖는다. 

요소
설명
사용자
시스템의 사용자와는 별도의 SELinux 사용자이며 역할이나 레벨과 연계하여 접근 권한을 관리하는데 사용.
역할(Role)
하나 혹은 그 이상의 타입과 연결되어 SELinux 의 사용자의 접근을 허용할 지 결정하는데 사용
타입(Type)
Type Enforcement의 속성중 하나로 프로세스의 도메인이나 파일의 타입을 지정하고 이를 기반으로 접근 통제를 수행
레벨(Level)
레벨은 MLS(Multi Level System)에 필요하며 강제 접근 통제보다 더 강력한 보안이 필요할 때 사용하는 기능. 필자도 모릅니다.

✔공부한 내용을 가지고 로그를 간단하게 해석 

 

"SELinux가 타입에 unlabeled_t 이라고 되어있기 때문에 찾는것에 대한 권한을 거부 한것이다."

 

처음에는 어디서 unlabeled_t 가 되어있는지 몰라서 모든 디렉토리를 다 찾아보았다.. 

그치만 생각도 못했던 루트(/)디렉토리였다.. 

[root@localhost ~]# ls -ldZ /
drwxr-xr-x. 19 root root system_u:object_r:unlabeled_t:s0 249 Jan 18 19:16 /

💢 생각 조차 하지 못했다...하지만 당연하다. 아무리 모든 디렉토리와 파일이 내용과 권한이 같다고 해도 루트(/)에서 거부당한다면 그 밑에는 의미가 없는것이다.


 chcon 명령어

 

SELinux의 보안 컨텍스트를 수정할수 있는 chcon 명령어를 사용하여 변경 해주었다. 

chcon -t root_t /

 

✨ 파일이나 디렉토리를 복사 또는 이동할때, SELinux 컨텍스트는 일반적으로 보존되지만, 목적지 디렉토리의 SELinux 정책에 따라 일부 변경될 있다고 한다. 이는 SELinux 정책이 서로 다른 디렉토리 간에 일관성을 유지하고, 시스템의 보안 정책을 준수하기 위한 조치라고한다.

 

변경후 모든게 해결되었다...


🎈그래서 결론적으로 해결방법은 ! 

첫번째 방법!  sda에서 sdb로 복사 할때 처음부터 루트(/)를 포함 해서 복사를 하는방법 

[root@localhost ~]# cp -ax / /sdb

두번째 방법!  audit.log 확인후 문제가 발견되었다면 맞는 정책을 설정 해주는 방법

chcon -t root_t /

세번째 방법! 애초에 SELinux를 사용하지 않을거 같다면 sda에서 SELinux를 비활성화 하고 복사할것 !

* SELinux 비활성화
[root@localhost ~]# setenforce 0
* SELinux 상태 확인 방법 2가지 
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      33

* SELinux 활성화
[root@localhost ~]# setenforce 1
* SELinux 상태 확인 방법 2가지 
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      33

rsync와 cp의 차이는 내가 복사를 할때 고려하지 않은 내용이였다... 

나는 cp를 할때 내가 원하는 디렉토리만 복사하기위해서 루트(/)를 제외하고 복사를 하였다. 

제일 중요한 루트(/)디렉토리의 정보는 복사가 되지 않았던것이다. 

 

예를 들어 

test1

[root@localhost ~]# mkdir test1
[root@localhost ~]# ls -ldZ test1/
drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Jan 19 02:49 test1/
[root@localhost ~]# cp -ax / ./test1
cp: cannot copy a directory, '/', into itself, './test1/'
[root@localhost ~]# ls -ldZ ./test1/
dr-xr-xr-x. 5 root root system_u:object_r:root_t:s0 40 Jan 19 02:51 ./test1/
[root@localhost ~]#

test2 

[root@localhost ~]# mkdir test2
[root@localhost ~]# cp -ax /* test2/

... 가상파일시스템들 때문에 에러발생

[root@localhost ~]# ls -ldZ ./test2/
drwxr-xr-x. 13 root root unconfined_u:object_r:admin_home_t:s0 172 Jan 19 02:52 ./test2/

test1는 루트(/) 정보까지 복사를 한것이고 

test2는 루트(/) 하위에 이는 모든 파일을 복사 한것이기때문에 루트(/)의 정보는 복사되지 않는다.... 


 

# 셀리눅스 비활성화
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive

[root@localhost ~]# /usr/bin/cp -ax / /cpax/

# usr,var 복사 (scr에도 복사할 내용이 있다면 추가 해야합니다.)
[root@localhost ~]# mount /dev/sdb2 /parted/2
[root@localhost ~]# mount /dev/sdb3 /parted/3
[root@localhost ~]# /usr/bin/cp -ax /usr/* /parted/2
[root@localhost ~]# /usr/bin/cp -ax /var/* /parted/3

[root@localhost ~]# umount /dev/sdb2
[root@localhost ~]# umount /dev/sdb3
[root@localhost ~]# mount /dev/sdb2 /cpax/usr
[root@localhost ~]# mount /dev/sdb3 /cpax/var

# GRUB를 설치 위한 chroot
[root@localhost ~]# mount --bind /sys /cpax/sys
[root@localhost ~]# mount --bind /proc /cpax/proc
[root@localhost ~]# mount --bind /dev /cpax/dev
[root@localhost ~]# chroot /cpax/
[root@localhost /]# blkid
/dev/sdb1: UUID="ff3c3f5a-56c8-4f10-bfa0-ba26ee7332c4" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="3b51cf42-01"
/dev/sdb2: UUID="feff2884-f065-430d-bf59-98c3512f3de3" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="3b51cf42-02"
/dev/sdb3: UUID="e072337c-b52f-43b8-b708-eb65b10a82c5" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="3b51cf42-03"
/dev/sdb5: UUID="25fa8d88-8b1a-4f88-bd53-f28bcbf22745" TYPE="swap" PARTUUID="3b51cf42-05"
/dev/sdb6: UUID="d31824ce-8503-4379-a357-a67402c43de1" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="3b51cf42-06"
/dev/sda1: UUID="1ace0d6f-c787-4b8f-85ec-8ec442e99ed7" BLOCK_SIZE="4096" TYPE="xfs" PARTUUID="95ccf150-01"
/dev/sda2: UUID="f942a759-24c5-45f2-9ce8-a176f450a0bd" BLOCK_SIZE="4096" TYPE="xfs" PARTUUID="95ccf150-02"
/dev/sda3: UUID="5e14c968-6782-46f5-b983-23c53714ed47" BLOCK_SIZE="4096" TYPE="xfs" PARTUUID="95ccf150-03"
/dev/sda5: UUID="d7258d2d-be7b-48d5-85ed-78a12116e47c" TYPE="swap" PARTUUID="95ccf150-05"
/dev/sda6: UUID="72bab45a-dbe2-4b12-9bed-9981103293b1" BLOCK_SIZE="4096" TYPE="xfs" PARTUUID="95ccf150-06"

[root@localhost /]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=d7258d2d-be7b-48d5-85ed-78a12116e47c"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

[root@localhost /]# vi /etc/default/grub

[root@localhost /]# grub2-install /dev/sdb
Installing for i386-pc platform.
Installation finished. No error reported.

[root@localhost /]#  grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
device-mapper: reload ioctl on osprober-linux-sda1 (253:0) failed: Device or resource busy
Command failed.
device-mapper: reload ioctl on osprober-linux-sda2 (253:0) failed: Device or resource busy
Command failed.
device-mapper: reload ioctl on osprober-linux-sda3 (253:0) failed: Device or resource busy
Command failed.
device-mapper: reload ioctl on osprober-linux-sda6 (253:0) failed: Device or resource busy
Command failed.
done
[root@localhost /]# exit

하드디스크교체
728x90

댓글