본문 바로가기

Linux/1) 리눅스 기초 관리자 과정

15_3 Remote Connection & File Transfer - SSH 명령어

SSH (Secure Shell)

- ssh (ssh client)는 원격 시스템에 로그인하기 위한 프로그램으로 원격 시스템에서 명령을 실행하는데 사용하기 위한 것

- rlogin(remote login) 및 rsh를 대체하고 보안되지 않은 네트워크를 통해 신뢰할 수 없는 두 호스트 간에 보안 암호화 통신 제공하기 위한 것

- X11 연결 및 임의 TCP 포트를 보안 채널을 통해 전달 가능

 

: ssh를 연결하고 지정된 호스트 이름에 로그인 (선택 사항인 사용자 이름으로)

사용자는 사용하는 프로토콜 버전에 따라 몇 가지 방법 중 하나를 사용해 원격 시스템에 자신의 ID를 증명해야함

 

∇ ssh 명령어 형식

# ssh 192.168.10.30

# ssh root@192.168.10.30

# ssh fedora@192.168.10.30

 

# ssh 192.168.10.30 CMD

# ssh 192.168.10.30 ls /test

 

예1) telnet/ssh 명령어 실습

∇ telnet 명령어 실습

 

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

Kernel 4.18.0-240.el8.x86_64 on an x86_64

server2 login: root			# root 사용자로 로그인
Password: 				# 패스워드 입력
Last login: Sun Aug 22 17:34:32 on tty1

[root@server2 ~]# hostname		# 호스트 네임 확인
server2.example.com

[root@server2 ~]# id			# id 확인
uid=0(root) gid=0(root) groups=0(root)

[root@server2 ~]# exit			#로그아웃
logout
Connection closed by foreign host.

 

∇ ssh 명령어 실습

 

[root@server1 ~]# cd ~/.ssh

[root@server1 ~/.ssh]# ls
id_rsa
id_rsa.pub
known_hosts

[root@server1 ~/.ssh]# >known_hosts

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

Last login: Sun Aug 22 17:34:56 2021 from ::ffff:192.168.10.20

[root@server2 ~]# id
uid=0(root) gid=0(root) groups=0(root)

[root@server2 ~]# exit
logout
Connection to 192.168.10.30 closed.

 

예2) ssh/scp 명령어를 통한 파일 전송

scp 명령어

: ssh 원격 접속 프로토콜을 기반으로 한 Secure Copy(scp)의 약자로서 원격지에 있는 파일과 디렉터리를 보내거나 가져올 때 사용하는 파일 전송 프로토콜

: ssh와 동일한 22번 포트와 identify file을 사용해 파일을 송수신하기에 안정된 프로토콜

 

server1- 선수 작업

 

[root@server1 ~/.ssh]# cd /test

[root@server1 /test]# rm -rf /test/*

[root@server1 /test]# cp /bin/* /test

[root@server1 /test]# mkdir -p /backup

[root@server1 /test]# tar cvzf /backup/server1.tar.gz /test
tar: 구성 요소 이름 앞에 붙은 `/' 제거 중
/test/
/test/cancel.cups
/test/captoinfo
/test/captype
---------------------------(중략)-------------------------------
[root@server1 /test]# ll /backup
합계 108M
-rw-r--r-- 1 root root 108M  8월 22 17:46 server1.tar.gz

 

server2 

 

 

server1

 

[root@server1 /test]# scp /backup/server1.tar.gz 192.168.10.30:/backup
root@192.168.10.30's password: 
server1.tar.gz                                       100%  107MB  34.7MB/s   00:03 

[root@server1 /test]# ssh 192.168.10.30 ls /backup
root@192.168.10.30's password: 
server1.tar.gz

 

예3) 로컬 && 원격 파일 / 디렉토리 비교

-----------------------------------(로컬)----------------------------------
[root@server1 /test]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6


#
# Server List
#
192.168.10.10	main.example.com 	main
192.168.10.20	server1.example.com	server1
192.168.10.30	server2.example.com	server2

------------------------------------(원격)---------------------------------
[root@server1 /test]# ssh 192.168.10.30 cat /etc/hosts
root@192.168.10.30's password: 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6