码迷,mamicode.com
首页 > 其他好文 > 详细

ssh服务配置

时间:2016-09-06 23:40:19      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:private   服务器   public   字符串   中间人   ssh   


   SSH的英文全称是Secure SHell。通过使用SSH,你可以把所有传输的数据进
行加密,这样“中间人”这种攻击方式就不可能实现了,而且也能够防止DNS和IP
欺骗。还有一个额外的好处就是传输的数据是经过压缩的,所以可以加快传输的
速度。SSH有很多功能,它既可以代替telnet,又可以为ftp、pop、甚至ppp提供一
个安全的“通道”。

1.ssh的key认证(本机生成KEY)
    所谓的密钥认证,实际上是使用一对加密字符串,一个称为公钥(public key),
任何人都可以看到其内容,用于加密;另一个称为密钥(private key),只有拥有者
才能看到,用于解密。 通过公钥加密过的密文使用密钥可以轻松解密,但根据公
钥来猜测密钥却十分困难。
    ssh 的密钥认证就是使用了这一特性。服务器和客户端都各自拥有自己的公钥
和密钥。
  在认证之前,客户端需要通过某种方法将公钥 Ac 登录到服务器上。

#######生成key###########
[test@foundation0 ~]$ ssh-keygen ###生成公钥和私钥的工具
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test/.ssh/id_rsa):『enter』 ###指定加密字符
保存文件,使用默认
Created directory ‘/home/test/.ssh‘.
Enter passphrase (empty for no passphrase): ###密码,必须大于4位
Enter same passphrase again:
Your identification has been saved in /home/test/.ssh/id_rsa.
Your public key has been saved in /home/test/.ssh/id_rsa.pub.
The key fingerprint is: ###确认密码
a5:4f:02:51:68:59:f4:e8:e3:c5:91:1f:6f:86:99:06 test@foundation0.ilt.example.com
The key‘s randomart image is:
+--[ RSA 2048]----+
|      .*+        |
|      +. o .     |
|     .. . E .    |
|       o + + *   |
|        S + * +  |
|       . * . o   |
|        . .      |
|                 |
|                 |
+-----------------+
[test@foundation0 .ssh]$ pwd
/home/test/.ssh ###生成密钥存放位置
[test@foundation0 .ssh]$ ls
id_rsa  id_rsa.pub ####id_rsa位私钥,id_rsa.pub位公钥

2.使用key加密目标主机的目标用户
[test@foundation0 ~]$ssh-copy-id -i /home/test/.ssh/id_rsa.pub test@172.25.254.100

The authenticity of host ‘172.25.254.100 (172.25.254.100)‘ can‘t be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are
already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is t
o install the new keys
westos@172.25.254.100‘s password:

Number of key(s) added: 1

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

ssh-copy-id     ####上传key的工具
-i     ####指定使用的公钥
/home/test/.ssh/id_rsa.pub #####使用公钥的名称
westos ####被管理的目标用户
172.25.254.100    ####被管理用户所在主机的ip

authorized_keys ###此文件在目标用户加目录的.ssh中,这个文件就是目标
用户被加密的标识,文件内容位公钥内容。

3.传文件(分发私钥)
scp /home/test/id_rsa client@12.23.21.23:/home/client/.ssh   #把当前主机中的id_rsa文
件推送到

4.远程文件传输:scp,rsync

ssh(远程shell)
sftp(软件)

scp ##远程复制
-v 显示进度-C 压缩传输选项-P 指定端口-4 强行使用 IPV4 地址-6 强行使用 IPV6 地
址-i 指定私钥文件路径-r 遍历路径复制-l 限制最大传输带宽,单位是Kb/s
scp file user@ip:/directory ##上传文件
scp -r dir user@ip:/directory ##长传目录
scp user@ip:/filename /direcotry ##下载文件
scp -r user@ip:/directory /direcotry ##下载目录


scp是复制新建的过程
rsync是镜像的过程(同步,不能同步链接)

rsync file|direcotry user@ip:/directory
rsync user@ip:/directory /directory
-r ##目录
-l ##不忽略链接
-p ##不忽略权限
-t ##不忽略时间戳
-g ##不忽略组信息
-o ##不忽略用户信息
-D ##不忽略设备文件

5.sshd服务的简单配置
systemctl restart sshd ###从新加载配置

5.1.修改/etc/ssh/sshd_config,具体如下:
Protocol 2  #只支持SSH2协议Port <端口号>  #修改默认端口号
MaxStartups 5    #同时允许5个尚未登录的SSH联机
MaxAuthTries  3   #最大登录尝试次数为3
ServerKeyBits 1024          #将ServerKey强度改为1024比特
PermitEmptyPasswords no     #禁止空密码进行登录
ChallengeResponseAuthentication no #禁用s/key密码
UsePAM no                   #不通过PAM验证
PermitRootLogin no           #禁止root远程登录
PasswordAuthentication no   #不允许密码方式的登录
RSAAuthentication no         #不允许RSA认证,只针对SSH1
PubkeyAuthentication yes     #允许公钥认证
AuthorizedKeysFile .ssh/authorized_keys #保存公钥的认证文件
UsePrivilegeSeparation yes    #用户权限设置
SyslogFacility AUTH #记录SSH登录情况
LogLevel INFO    #记录等级为
INFOPrintMotd yes    #登录成功后显示/etc/motd 文件的内容
PrintLastLog no   #不显示上次登录信息
Compression yes    #是否压缩命令
KeepAlive yes    #防止死连接
StrictModes yes   #接受连接请求前对用户主目录和相关的配置文件进行宿主和权限
检查
UseDNS no  #不使用DNS反解
AllowUsers <用户名>  #允许通过远程访问的用户,多个用户以空格分隔
AllowGroups <组名>   #允许通过远程访问的组,多个组以
空格分隔
DenyUsers <用户名>   #禁止通过远程访问的用户,多个用户以空格分隔
DenyGroups <组名>    #禁止通过远程访问的组,多个组以空格分隔

5.2、如果需要限制来路IP的话,可以修改/etc/hosts.deny和/etc/hosts.allow两个文件,
通过添加sshd:来限制或允许SSH远程链接IP。





本文出自 “技术人生,简单不简单” 博客,请务必保留此出处http://willis.blog.51cto.com/11907152/1846791

ssh服务配置

标签:private   服务器   public   字符串   中间人   ssh   

原文地址:http://willis.blog.51cto.com/11907152/1846791

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!