Net Eng

SSH 본문

Linux

SSH

欲心 2024. 6. 18. 21:13

■ SSH 요약

- 프로그램 : openssh, openssh-server

- 데몬 & 포트/프로토콜 : sshd(22/tcp)

- 설정 파일 : /etc/ssh/sshd.config

- 하위 설정 파일 : /etc/ssh/*

- 서비스 : sshd.service

 

■ ssh 설치 및 기본 설정

[설치]
# yum install openssh-server openssh openssh-client
# cd /etc/ssh 
# ls
합계 604K
-rw-r--r--. 1 root root     564K  1월 10  2023 moduli
-rw-r--r--. 1 root root     1.8K  1월 10  2023 ssh_config
-rw-r--r--. 1 root root     1.8K  1월 10  2023 ssh_config.OLD
drwxr-xr-x. 2 root root       28  7월 18 12:35 ssh_config.d
-rw-r-----. 1 root ssh_keys  492  7월 18 12:47 ssh_host_ecdsa_key
-rw-r--r--. 1 root root      162  7월 18 12:47 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys  387  7월 18 12:47 ssh_host_ed25519_key
-rw-r--r--. 1 root root       82  7월 18 12:47 ssh_host_ed25519_key.pub
-rw-r-----. 1 root ssh_keys 2.6K  7월 18 12:47 ssh_host_rsa_key
-rw-r--r--. 1 root root      554  7월 18 12:47 ssh_host_rsa_key.pub
-rw-------. 1 root root     4.4K  8월 17 15:10 sshd_config

[서비스 데몬 확인]
# systemctl enable --now sshd
# systemctl status sshd

[방화벽 설정]
# firewall-cmd --permanent --add-service=ssh
# firewall-cmd --reload
# firewall-cmd --list-all

[포트 확인]
# cat /etc/services | grep ssh

 

ssh/scp/sftp 모두 22번 포트를 사용한다.

→aws 에서는 공개키를 직접 적용해야함

 

■ WireShark로 암호화 통신 확인

- Telnet 이용 접속 Wireshark로 패킷 확인

[root@server1 ~]# telnet 192.168.10.30
Trying 192.168.10.30...
Connected to 192.168.10.30.
Escape character is '^]'.

Kernel 4.18.0-492.el8.x86_64 on an x86_64
server2 login: root
Password: 
Last login: Thu Aug 17 12:49:52 on tty2
[root@server2 ~]# hostname
server2.example.com
[root@server2 ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@server2 ~]# exit
logout
Connection closed by foreign host.

평문으로 전송되어 패스워드 확인 가능

 

■ SSH 이용 접속 Wireshark로 패킷 확인

[root@server1 ~]# ssh 192.168.10.30
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Thu Aug 17 19:08:30 2023 from 192.168.10.20
[root@server2 ~]# hostname
server2.example.com
[root@server2 ~]# pwd
/root
[root@server2 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  공개      문서      비디오  서식
bin              server2.txt           다운로드  바탕화면  사진    음악
[root@server2 ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@server2 ~]# exit
logout
Connection to 192.168.10.30 closed.

암호화 되어 확인 불가

 

■ 인증키 설정

- 인증키를 설정하여 비밀번호 입력 없이 원격 접속

 

□ Server2에서 Server1 ssh 접속

<server2>

# ssh 192.168.10.20

The authenticity of host 'server1 (192.168.10.20)' can't be established.
ECDSA key fingerprint is SHA256:amdYa3CTFlE4VneuNrB7EzkzdlO90tIoh12I3re39lA.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'server1,192.168.10.20' (ECDSA) to the list of known hosts.
root@server1's password: 
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Thu Aug 17 21:15:33 2023

# cd ~/.ssh/
# ls
known_hosts

# cat known_hosts
server1,192.168.10.20 
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHS1
bgHsn2mBGOgCHujDh8WR9V6F7i4VRCDdDSRcHao6xk2Av9wz45VNpjN9TSyhN1UEQ384ZzAs5PhW7nvsqI8=

<server1>
# cd /etc/ssh
# ls
moduli          ssh_config.d            ssh_host_ed25519_key      ssh_host_rsa_key.pub
ssh_config      ssh_host_ecdsa_key      ssh_host_ed25519_key.pub  sshd_config
ssh_config.OLD  ssh_host_ecdsa_key.pub  ssh_host_rsa_key

# cat ssh_host_ecdsa_key.pub
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHS1
bgHsn2mBGOgCHujDh8WR9V6F7i4VRCDdDSRcHao6xk2Av9wz45VNpjN9TSyhN1UEQ384ZzAs5PhW7nvsqI8=

 

<server2>known_hosts = <server1>ssh_host_ecdsa_key.pub

 

■ 인증없이 SSH 접속하기

- Server2 인증키 생성 (개인키, 공개키)

# ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): <ENTER>
Enter passphrase (empty for no passphrase): centos123
Enter same passphrase again: centos123
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:U68o6hpIReeABUqu+lttTQtHvg5FMvG6gCYLbIju5/s root@server2.example.com
The key's randomart image is:
+---[RSA 3072]----+
| o++ ..          |
|+.. +  o         |
|.. . .o + .      |
|= ..   B . .     |
|*+o . o S   .    |
|*=   o B = .     |
|+.. . * = .      |
|.. + o +         |
| .**=E  .        |
+----[SHA256]-----+

# cd ~/.ssh
# ls -l
-rw-------. 1 root root 2655  8월 17 21:37 id_rsa
-rw-r--r--. 1 root root  578  8월 17 21:37 id_rsa.pub
-rw-r--r--. 1 root root  183  8월 17 21:24 known_hosts

 

Server1에 인증키 전송

~/.ssh 폴더 안에서 진행

# ssh-copy-id root@server1
/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
root@server1's password: 

Number of key(s) added: 1

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


[인증키 전송 참고]
# scp ~/.ssh/id_rsa.pub root@server1:.ssh/server2.pub
# ssh root@server1
# cat ~/.ssh/server2.pub >> ~/.ssh/authorized_keys
# exit
or
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@server1


# ssh server1

 

[참고] 원격 서버에 인증 없이 로그인 가능하도록 설정하는 방법

(임시적인 방법)
# ssh-keygen -t rsa
-> <ENTER>
-> centos123/centos123
# ssh-copy-id root@server1
# ssh-agent bash 
# ssh-add 
# ssh server1 hostname
# exit

(영구적인 방법)
# ssh-keygen -t rsa 
-> 모두 [ENTER]
# ssh-copy-id root@server1
# ssh server1 hostname

 

■ SSH 정책 변경/ 포트 변경

<기본정책>

- Root, 일반 사용자 모두 SSH 서버에 접속 가능

 

- 정책 변경 (Root 사용자 접속 권한)

# vi /etc/ssh/sshd_config

PermitRootLogin yes
-> PermitRootLogin no

Root 사용자로 접속 불가능

- 정책변경 (일반 사용자 접속 권한) * 들어오는 기준

# vi /etc/ssh/sshd_config 
	---------------------------------
	AllowUsers fedora user01		/* fedora, user01 사용자만 ssh 사용가능 */
	#DenyUsers user02
	---------------------------------
	#AllowUsers fedora user01
	DenyUsers user02				/* user02 사용자만 ssh 사용 불가능 */
	---------------------------------

[중요] AllowUsers를 사용하는 경우 root 사용자도 등록해야 로그인 가능

 

→ 예를 들어 Server1 설정 파일이 AllowUsers user02라면 Server1의 user02로만 들어올 수 있다는 뜻

 

■ SSH 서비스 포트 번호 변경

# vi /etc/ssh/sshd.config
# systemctl restart sshd

 

# netstat -an | grep :22
tcp        0      0 0.0.0.0:2222            0.0.0.0:*               LISTEN     
tcp6       0      0 :::2222                 :::*                    LISTEN

# ssh localhost
ssh: connect to host localhost port 22: Connection refused
-> 접속 불가

# ssh -p 2222 localhost
-> 2222 포트로 접속 가능

 

■ 이론 보고서

SSH 연구 보고서.pdf
0.04MB

'Linux' 카테고리의 다른 글

SAMBA  (0) 2024.06.18
Mail  (0) 2024.06.18
VIM Editor  (0) 2024.06.18
Process  (0) 2024.06.18
Crontab 명령어  (0) 2024.06.18