linux 시리즈 의 SSH

9790 단어
SSH 는 Secure Shell Protocol (보안 케이스 프로 토 콜) 의 약자 입 니 다.
서버 포트 번호: 22
SSH 는 인증서 검증 과 비밀번호 검증 두 가지 방식 이 있 습 니 다.
SSH 프로 토 콜 은 미리 설 정 된 상태 에서 두 개의 서버 기능 을 제공 합 니 다.
 1. 하 나 는 Telnet 과 유사 한 원 격 셸, 즉 SSH
 2. 하 나 는 FTP 와 유사 한 sftp - server, 즉 더욱 안전 한 FTP 서비스 입 니 다.
공개 키 와 비밀 키 의 개념
 1. 쉽게 말 하면 공개 키 는 다른 사람 에 게 주 는 것 이 고 비밀 키 는 자신 에 게 만 남 겨 질 수 있다.
 2. 자신 을 클 라 이언 트 로 서 먼저 서버 측의 공개 키 를 얻 은 다음 에 자신의 공개 키 를 서버 측 에 보 내야 합 니 다.
 3. 마지막 결 과 는?
   클 라 이언 트 = 클 라 이언 트 비밀 키 + 서버 측 공개 키
   서버 측 = 서버 측 비밀 키 + 클 라 이언 트 공개 키
연결 프로 세 스 개요
서버 쪽 에서 어떻게 새로운 ssh 공개 키 를 생 성 합 니까?
[root@study ~]# rm -rf /etc/ssh/ssh_host*
[root@study ~]# systemctl restart sshd
# 重启sshd服务即可

SSH 클 라 이언 트 프로그램
 링크 ux 에서 ssh 명령 을 직접 사용 합 니 다.
[root@study ~]# ssh 192.168.1.1
# 以 root 用户登录,默认以当前用户登录对方SSH

[root@study ~]# ssh [email protected]
# 以对方 study 用户登录SSH

 윈도 우즈 에서 사용
  putty    http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
  secureCRT   상업 소프트웨어 에 속 하 므 로 해독 판 을 다운로드 할 수 있다.
  x-shell    http://xshell.en.softonic.com/
SFTP
  SSH 는 원 격 서버 에 로그 인 합 니 다. 서버 에서 파일 을 다운로드 하거나 업로드 하려 면 sftp 나 scp 를 사용 해 야 합 니 다.이 두 명령 은 모두 ssh 를 사용 하 는 채널 (port 22) 로 FTP 와 복사 동작 만 모 의 합 니 다.
[root@study ~]# sftp [email protected]
  Connecting to localhost...
  student@localhost's password:    <== 输入密码
  sftp> exit   <== 输入 ftp 相关指令

서버 작업
현재 디 렉 터 리 보이 기
pwd
현재 디 렉 터 리 변경
cd
현재 디 렉 터 리 아래 파일 보이 기
ls
디 렉 터 리 만 들 기
mkdir
파일 삭제
rm
파일 그룹 변경
chgrp
파일 소유자 변경
chown
파일 권한 변경
chmod
파일 이름 변경
rename
로 컬 작업
로 컬 현재 디 렉 터 리 보이 기
lpwd
로 컬 현재 디 렉 터 리 변경
lcd
로 컬 현재 디 렉 터 리 아래 파일 보이 기
lls
로 컬 에 디 렉 터 리 만 들 기
lmkdir
파일 업로드/다운로드 작업
서버 에 로 컬 파일 업로드
put [로 컬 디 렉 터 리 나 파일] [원 격] put [로 컬 디 렉 터 리 나 파일] 이 형식 은 서버 현재 디 렉 터 리 에 업 로드 됩 니 다.
서버 에서 로 컬 로 파일 다운로드
get [원 격 디 렉 터 리 또는 파일] [로 컬] get [원 격 디 렉 터 리 또는 파일] 이 형식 은 로 컬 현재 디 렉 터 리 get * get * rpm 에 다운로드 할 수 있 습 니 다.
windows 아래 도구
  다운로드 주소:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
psftp> open 192.168.1.1
login as: root
[email protected]'s password:
Remote working directory is /root
# 该软件通过 open 打开远程主机

SCP
  보통 sftp 를 사용 하 는 이 유 는 서버 에 어떤 파일 이름 이 있 는 지 모 를 수 있 기 때 문 입 니 다. 서버 에 있 는 파일 이름 을 알 고 있다 면 가장 간단 한 파일 전송 은 scp 명령 을 통 해 이 루어 집 니 다.
  windows 아래 도구 다운로드 주소:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
[root@study ~]# scp [-pr] [-l 速率] file [帐号@]主机:目录名   《==上传
[root@study ~]# scp [-pr] [-l 速率] [帐号@]主机:file 目录名   《==下载
选项:
  -p : 保留原本文件的权限数据
  -r : 复制来源为目录时,可以包含子目录
  -l : 限制传输的速度,单位为 Kbits/s ,比如 [-l 800] 代表传输速率限制为 100Kbytes/s
  
[root@study ~]# scp /etc/hosts* [email protected]:~
[email protected]'s password:    <==输入 student 密码
# 将本机的 /etc/hosts* 全部复制到 192.168.1.1 上 student 用户的家目录下

[root@www ~]# scp [email protected]:/etc/bashrc /tmp
# 将 192.168.1.1 远程主机下的 /etc/bashrc 复制到本机的 /tmp 目录下

1. 서버 가 다시 설치 하거나 공개 키 가 바 뀌 면 서버 가 같은 IP 를 사용 하여 같은 IP 의 서버 공개 키 가 다르다 고 가정 하면 어떻게 해결 해 야 합 니까?(클 라 이언 트 에서 작 동)
[root@study ~]# ssh [email protected]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middleattack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
a7:2e:58:51:9f:1b:02:64:56:ea:cb:9c:92:5e:79:f9.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending key in /root/.ssh/known_hosts:1 <==有问题的数据行号
RSA host key for localhost has changed and you have requested strict checking.
Host key verification failed.

# 上面那句话告诉你:/root/.ssh/known_hosts 的第 1 行,上次的公钥与本次收到的公钥结果不同
# 方法:vim /root/.ssh/known_hosts,并将第 1 行删除,再重新 ssh 连接就可以了

2 、 어떻게 비밀 번 호 를 입력 하지 않 고 SSH 연결 을 만 듭 니까?(클 라 이언 트 에서 작 동)
 1. 클 라 이언 트 가 로 컬 공개 키 생 성
 ssh-keygen  //로 컬 에서 RSA 공개 키 와 비밀 키 를 생 성 합 니 다. 실행 에 성공 하면 현재 사용자 의 집 디 렉 터 리. ssh/에서 id 를 생 성 합 니 다.rsa (비밀 키) 와 idrsa. pub (공개 키)
[root@study ~]# ssh-keygen
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):   <==按enter
Enter same passphrase again:   <==按enter
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:
24:6e:f2:c7:b6:2c:b1:4c:df:8a:31:e8:07:eb:78:20 [email protected]
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
|                 |
|                 |
|      . .        |
|     . o         |
|    . o S        |
|E . .=o.         |
| . ..=+++.       |
|   oo =B...      |
|  .oo...+.       |
+-----------------+

 2. 로 컬 공개 키 를 대상 호스트 (서버 쪽) 에 복사 하고 연결
 ssh-copy-id  //이 컴퓨터 의 공개 키 를 원 격 기기 의 authorized 로 복사 합 니 다.keys 파일 에서 ssh - copy - id 도 원 격 기기 의 홈, ~./ssh, 그리고 ~/. ssh/authorizedkeys 의 권리
 ssh-copy-id 10.0.0.11  //id 내 보 내기rsa. pub (공개 키), 상대방 을 연결 하고 공개 키 를 상대방 서버 에 전달 합 니 다.
 ssh-copy-id [email protected]   //공개 키 를 10.0.0.11 루트 사용자. ssh 디 렉 터 리 로 내 보 냅 니 다.
 ssh-copy-id [email protected]   //공개 키 를 10.0.0.11 의 student 사용자. ssh 디 렉 터 리 로 내 보 냅 니 다.
[root@study ~]# ssh-copy-id 10.0.0.11
The authenticity of host '10.0.0.11 (10.0.0.11)' can't be established.
ECDSA key fingerprint is c9:77:ee:3f:f6:82:25:66:0b:85:d8:d1:46:59:8f:ef.
Are you sure you want to continue connecting (yes/no)? 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
[email protected]'s password:        <==输入目标主机root用户密码

Number of key(s) added: 1

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

[root@study .ssh]# ssh 10.0.0.11
Last login: Fri Feb  5 23:48:56 2016 from 10.0.0.129
[root@test ~]#
# 成功,不再需要输入密码
# 其实,当前 root 用户通过 ssh-keygen 生成的本地公私钥不仅仅 root 用户可以用,其他本地用户也可以。
# 假设本地还有一个叫做 hello 的用户,我想通过 hello 用户连接到远端的 root 用户和 student 用户也不需要密码。
[root@study /]# mkdir /home/hello/.ssh
[root@study /]# cp /root/.ssh/id_rsa /home/hello/.ssh/
[root@study /]# chown -R hello:hello /home/hello/.ssh
[root@study hello]# su - hello

[hello@study ~]$ ssh [email protected]
The authenticity of host '10.0.0.11 (10.0.0.11)' can't be established.
ECDSA key fingerprint is c9:77:ee:3f:f6:82:25:66:0b:85:d8:d1:46:59:8f:ef.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.11' (ECDSA) to the list of known hosts.
Last login: Sat Feb  6 00:00:15 2016 from 10.0.0.129
[root@test ~]#
# 成功!hello 用户登录到对方 root 用户

3. 인증 서 를 통 해 뒷문 을 설치 하 는 사고
#一般来说,SSH会在用户的家目录 .ssh/ 下有以下四个文件:
[root@study .ssh]# ls
authorized_keys id_rsa  id_rsa.pub  known_hosts
# authorized_keys 服务器端上记录通过证书验证连接进来的客户端公钥
# id_rsa 客户端本地的私钥
# id_rsa.pub 客户端本地的公钥
# known_hosts 客户端记录的服务器端的公钥

# 通过以下命令即可实现无密码登录
[root@study ~]# ssh-keygen
# 生成客户端本地公私钥
[root@study ~]# ssh-copy-id [email protected]
# 输入 ssh-copy-id 命令后还是需要输入服务器端用户的密码的
# 将客户端的公钥导出到 10.0.0.11 的 root 的家目录下的 .ssh/authorized_keys 文件里

# 故可通过其他方式(比如物理接触),将生成的客户端公钥拷贝至 root 的家目录下的 .ssh/authorized_keys 文件里,这样便能实现远程无密码登录

4. 현재 원 격 로그 인 사용자 가 위험 을 제거 하고 제거 하 는 방법 을 발견 합 니까? (서버 에서 작 동 합 니 다)
[root@test ~]# who 或 w
root     tty1         2016-02-05 21:56
root     pts/0        2016-02-06 00:27 (10.0.0.2)
root     pts/1        2016-02-06 00:35 (10.0.0.129)
# 假设root从 pts/1 这个入口非法登录

[root@test ~]# pkill -kill -t pts/1
# 杀死pts/1远程登录进程(相当于踢掉远程登录的非法用户)

# 但是仅仅踢掉是不够的,假设非法登录是通过上面的证书验证方式
[root@test ~]# cd .ssh
[root@test .ssh]# vim authorized_keys
#  这里可能不仅仅在 root 的家目录下有 authorized_keys 文件,最好把 /home/ 目录下所有用户的家目录都检查一遍

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3WTJS0riNqh8AW06s7CiXiZ+QAdeYmdsHUNxkR+8BgYbamHuy7v27nKHdT9909zPruu/JIJLgNZx2ZgtbRQMgUUWma+gHBtE1GY7hdn/sRc5nBxEnpC+go2/RIC5oBO+Ly17Eg7TnepM7QZNA4Uq5nx+CxFsEy4eM75p9DP78nP+8ZRdJkiXsQAD95teGxQDe8ckrYokwLUfXbk9Q1mwbrfNGC2nR2i5AzxOvdo//YkYX9xydUIB2YM27KE/o9WEBR7/K6iPPFi7Cv5mvRX85+kyCN8RpkReuiLA+LDmGbB3z2OfQsVtbfFxW+2q1vhPuuhl8KJStR7zMEjDFmBfb [email protected]

# 删除该条记录即可,即为删除非法公钥

5. SSH 서버 에서 간단하게 설정 (서버 에서 동작)
[root@test ~]# vim /etc/ssh/sshd_config

PermitRootLogin no   禁止root用户连入SSH
PasswordAuthentication no   禁止通过密码验证,即输入用户名后不会出现输入密码的框

6. 보다 안전 한 SSH 로그 인 방식
# 假设有两台主机,第三方想要通过 SSH 登录到你的重要主机上,必须先经过跳板主机,通过跳板主机才能到达重要主机。

# 假设重要主机的 IP 地址为192.168.1.1,且存在 root 和 student 两个用户。
# 假设跳板主机有两张以上的网卡,并在一个无线局域网的环境里(与重要主机相互隔离的网段),该局域网内只有跳板主机自己。
# 第三方先通过连接至跳板主机所在的局域网,进入无线局域网,然后连接到跳板主机,通过跳板主机中转,最终到达重要主机。

# 思路如下:
1、跳板主机生成密码
ssh-keygen

2、跳板主机以 student 用户连入重要主机
ssh-copy-id [email protected]
# 这里会要求输入一次 student 用户的密码

3、修改重要主机的配置
vim /etc/ssh/sshd_config
 PermitRootLogin no
 PasswordAuthentication no
# 禁止重要主机的 root 用户登录,禁止通过密码验证

4、重启服务
systemctl restart sshd

5、结果
 1)第三方首先得连入跳板主机所在的局域网
 2)连入跳板主机所在的局域网后,还需要跳板主机的 root 用户权限
# 这里更复杂的做法是:禁止跳板主机的 root 用户登录,以其他用户登录后再 su - 切换为 root
 3)获取到跳板主机的 root 用户权限后,使用 root 用户是无法登录到重要主机的,只能通过 student 用户登录,student 用户由于已经传递了公钥,所以是不需要密码的,且这里也无法通过密码验证。
 4)获得 student 用户的权限后,再通过 su - 切换成 root 用户,这里需要重要主机的 root 密码

다음으로 전송:https://blog.51cto.com/buxiuzi/1746207

좋은 웹페이지 즐겨찾기