본문 바로가기
WordPress/Linux

(처음서버설정) SUDO, SSH key 인증, Server Updates

by 계영수 2024. 2. 28.
728x90

- SUDO

- SSH Key Authentication

- Config File

- Server Update

- Firewall

- Fail2Ban

 

▶ SUDO

루트 사용자는 관리자 업무를 수행하기 위해서 필요한 모든 권한을 가지고 명령어를 실행할 수 있다. 반면에 일반 사용자는 실행에 루트 권한이 필요한 명령어는 실행할 수 없기 때문에, 일반사용자로 리눅스 서버의 관리업무를 수행할 수 없다. 이러한 문제를 해결하기 위해서, 일반사용자가 루트 권한이 필요한 명령어를 실행할때에 한해서 루트 권한을 가질수 있다. 이때 사용하는 명령어를 SUDO (Super User DO)라고 하고, su 라는 명령어를 사용한다.

 

- 루트 권한이 필요한 명령어 앞에 sudo 라는 명령어를 주어서 명령어를 실행한다.

- sudo 명령어 실행시 입력한 패스워드는 5분간만 유효하다.

- "sudo -k" 명령어를 입력하면 캐시된 패스워드를 지울 수 있다.

 

▶ SSH

* Windows PowerShell-GitBash 터미널에서 작업한다.

MyLearnPub@DESKTOP-89I0B5P MINGW64 ~
$ ls -l .ssh
total 8
-rw-r--r-- 1 MyLearnPub 197121 1683  2월 21 10:44 known_hosts
-rw-r--r-- 1 MyLearnPub 197121  937  2월 21 10:44 known_hosts.old

MyLearnPub@DESKTOP-89I0B5P MINGW64 ~

 

* SSH Key 생성하기

MyLearnPub@DESKTOP-89I0B5P MINGW64 ~
$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/MyLearnPub/.ssh/id_rsa): .ssh/mykeys
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in .ssh/mykeys
Your public key has been saved in .ssh/mykeys.pub
The key fingerprint is:
SHA256:lE7YSMQ3ae44ZRB0fXo0jnHUJVNot4QAKzFA59+IsyI MyLearnPub@DESKTOP-89I0B5P
The key's randomart image is:
+---[RSA 4096]----+
|    .**=.+.oo.++o|
|     .==B.+ =.++.|
|      oB=o O o...|
|       +B = o  . |
|       BSo o     |
|      o +        |
|   E . o         |
|    . .          |
|                 |
+----[SHA256]-----+

MyLearnPub@DESKTOP-89I0B5P MINGW64 ~
$

 

아래와 같이 키가 생성되었다.

 

이제 Public Key를 서버로 복사해야 한다.

MyLearnPub@DESKTOP-89I0B5P MINGW64 ~
$ ssh-copy-id -i .ssh/mykeys.pub stzuser@X.X.X.X
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/mykeys.pub"
The authenticity of host 'X.X.X.X (X.X.X.X)' can't be established.
ED25519 key fingerprint is SHA256:hmVvIBrPQCdonUPTBBeafY/bIkZn6fPUaIF5bx0ybis.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
stzuser@49.247.154.81's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'stzuser@X.X.X.X'"
and check to make sure that only the key(s) you wanted were added.


MyLearnPub@DESKTOP-89I0B5P MINGW64 ~
$

 

이제 SSH Key를 이용한 로그인이 가능하다.

stzuser@mylearn-239626:~$ cd .ssh
stzuser@mylearn-239626:~/.ssh$ ls -la
total 12
drwx------ 2 stzuser stzuser 4096 Feb 28 17:05 .
drwxr-x--- 4 stzuser stzuser 4096 Feb 28 17:08 ..
-rw------- 1 stzuser stzuser  752 Feb 28 17:05 authorized_keys
stzuser@mylearn-239626:~/.ssh$ cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EDangc9wgqucb1YfZcIKFXrKm/KxbFU6xI~~~~ 생략
stzuser@mylearn-239626:~/.ssh$

 

패스워드 로그인 방식 비활성화

stzuser@mylearn-239626:/etc/ssh$ sudo nano sshd_config

 

PasswordAuthentication 주석처리

 

728x90