www.swack.cn - 原文链接:Centos7 密钥对登陆(适用于群晖DSM)

1.生成证书

此处证书使用swack用户生成

注:不要使用root生成证书,因为我们后面会禁用root登陆

[swack@localhost ~]$ ssh-keygen -t rsa -b 2048 -C "swack_test"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/swack/.ssh/id_rsa):
Created directory '/home/swack/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/swack/.ssh/id_rsa.
Your public key has been saved in /home/swack/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:BRidXhVBsAYWEt73wp8KzQa52vXLMKyvyEpuV8NGd44 swack_test
The key's randomart image is:
+---[RSA 2048]----+
| +==o.+=o |
| ..+oo.. |
| ....= |
| o... |
| oS.o+. |
| =Eo.. |
| . oo.O o |
| o...+ = * |
| .oo+ +oo +. |
+----[SHA256]-----+

执行完毕后,会在用户home目录生成.ssh目录,其下生成两个证书文件

  • 公钥文件:.ssh/id_rsa.pub
  • 密钥文件:.ssh/id_rsa

2.配置ssh

修改 /etc/ssh/sshd_config 可以配置ssh的相关参数(需要root权限)

[root@localhost swack]# vim /etc/ssh/sshd_config

为了使得系统安全性进一步提高,我们做一下更改

  • 修改默认ssh的默认端口22到8022(此处端口可自定义)
  • 禁用密码认证进制
  • 禁止root权限登陆
  • 使用密钥对登陆
#       $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information. # This sshd was compiled with PATH=/usr/local/bin:/usr/bin # The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value. # If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 8022
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress :: HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key # Ciphers and keying
#RekeyLimit default none # Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO # Authentication: #LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10 PubkeyAuthentication yes # The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys #AuthorizedPrincipalsFile none #AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes # To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication no # Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no # Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes # GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no # Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes #AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none # no default banner path
#Banner none # Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS # override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server # Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server

此处如下修改:

  • Port 8022
  • PasswordAuthentication no
  • PermitRootLogin no
  • PubkeyAuthentication yes

重启ssh服务

[root@localhost swack]# systemctl restart sshd.service

2.配置公钥

在.ssh目录,新建一个文件

  • 证书验证文件:.ssh/authorized_keys

将公钥证书文件写到证书验证文件里

[swack@localhost ~]$ touch .ssh/authorized_keys
[swack@localhost ~]$ cat /home/swack/.ssh/id_rsa.pub >> /home/swack/.ssh/authorized_keys
[swack@localhost ~]$ cat .ssh/authorized_keys
ssh-rsa
AAAAB3NzaC1yc2EAAAA...
swack_test

注:这里需要把允许登陆的centos7服务的终端的公钥id_rsa.pub添加到authorized_keys文件中

3.设置 .ssh 目录权限

[swack@localhost ~]$ chmod -R 700 .ssh
[swack@localhost ~]$ chmod 600 .ssh/authorized_keys

4.测试登陆(基于windows 10)

需要生成客户本地主机的公钥,并把密钥写入服务器的authorized_keys文件中

ssh登陆

此处使用的工具是GitBash

下载地址:WinSCP-5.15-Setup.exe

使用 ssh -p 8022 swack@192.168.241.3 登陆

sftp登陆

此处我们使用的工具是WinSCP

下载地址:Git-2.15.1.2-64-bit.exe

在WinSCP的高级设置里选择密钥文件

Centos7 密钥对登陆(适用于群晖DSM)的更多相关文章

  1. 群晖DSM修改ssh权限实现免密码登陆

    问题 使用ssh-id-copy正确上传公钥后依然无法免密码登陆 原因 群晖DSM中.ssh文件夹权限不当 解决 赋予正确权限 admin@DiskStation:/var/services/home ...

  2. 如何在ProXmoX VE 下虚拟机安装 黑群晖 DSM 6.1.6

    ProXmoX 官方下载地址: https://www.proxmox.com/en/downloads Proxmox是一个非常优秀的开源虚拟化环境,集诸多优点于一身:开源免费,稳定可靠,架构简洁, ...

  3. 黑群晖DSM 6.1网卡支持列表

    黑群晖DSM 6.1网卡支持列表 Network Drivers====================================AMDamd8111e : AMD 8111 (new PCI ...

  4. 【转】NAS群晖DSM 5.0-4458安装教程

    需要准备: 1.一个闲置的U盘,容量大于64M即可. 墙裂建议用如下U盘,可以隐藏成灰群晖,小巧方便. 闪迪(SanDisk)酷豆(CZ33)8GB U盘¥29.9京东商城    2.一台显示器,用于 ...

  5. 黑群晖DSM 6.x 配置文件grub.cfg修改 mac地址/sn等修改

    新的DSM 6.x配置文件和以前的XPEnoboot的配置文件不一样了,我们可以通过OSFMount虚拟光驱软件打开img后再修改. 安装完成后运行OSFMount点击左下角-Mount new,选择 ...

  6. [转载]在Vmware ESXI中安装群晖Synology DSM 5.0 (4528)

    转载 在Vmware ESXI中安装群晖Synology DSM 5.0 (4528) 文件准备 Vmware ESXi用户安装需要的文件 NB_x64_5032_DSM_50-4528_Xpenol ...

  7. Vmware ESXi安装群晖Synology DSM 5.x

    简介 在Vmware ESXI中安装群晖Synology DSM 5.0 (4528) 文件准备 Vmware ESXi用户安装需要的文件 NB_x64_5032_DSM_50-4528_Xpenol ...

  8. 【转】NAS 黑群晖 配置完成(不含硬盘),NAS能做什么?

    在配黑群晖前,240元入手过一个艾美佳的NAS感受了下,功能倒还合适,就是配置太老,厂家固件也停止更新了,一直不太满意. 后来经常关注NAS1,发现现在X86的NAS也很好自己DIY了,就长草了,向女 ...

  9. 为群晖加把锁:使用ssh密钥保障数据安全

    对每一个使用群晖nas的人而言,能保证群晖里保存的数据不被未经授权的人访问下载甚至破坏可能是最重要的事情.但数据只要上网,就免不了担心密码被破解,群晖被侵入.现在网络上,要破解密码可能是最简单不过的事 ...

随机推荐

  1. 归并排序(c++,递归)

    放上c++代码模板(但是该版本中,还可以再进一步优化成原地算法,即不开辟新的空间:本代码中空间复杂度为n,不是1) 1 #include <iostream> 2 #include< ...

  2. Spring引用数据库文件配置数据源

    例子:引用 druid.properties 在Spring配置文件(applicationContext.xml)引入外部配置文件,需要指定特定的 key才能被正确识别并使用 在原本的 url.us ...

  3. Android全面解析之Context机制

    前言 很高兴遇见你~ 欢迎阅读我的文章. 在文章Android全面解析之由浅及深Handler消息机制中讨论到,Handler可以: 避免我们自己去手动写 死循环和输入阻塞 来不断获取用户的输入以及避 ...

  4. MySQL技术内幕InnoDB存储引擎(五)——索引及其相关算法

    索引概述 索引太多可能会降低运行性能,太少就会影响查询性能. 最开始就要在需要的地方添加索引. 常见的索引: B+树索引 全文索引 哈希索引 B+树索引 B+树 所有的叶子节点存放完整的数据,非叶子节 ...

  5. 升级openssl和openssh版本

    一.安装telnet-server服务(建议安装) 1. 查看系统是否已安装telnet-server,linux系统上默认已经安装telnet-client(或telnet),而telnet-ser ...

  6. MySQL锁:03.InnoDB行锁

    目录 InnoDB 行锁 锁排查可以用的视图和数据字典 InnoDB 行锁兼容性 InnoDB行锁之共享锁 共享锁: 查看InnoDB锁 InnoDB行锁实现机制 对普通索引上锁 InnoDB隐式.显 ...

  7. MyBatis详细源码解析(上篇)

    前言 我会一步一步带你剖析MyBatis这个经典的半ORM框架的源码! 我是使用Spring Boot + MyBatis的方式进行测试,但并未进行整合,还是使用最原始的方式. 项目结构 导入依赖: ...

  8. 为什么类只能用public修饰?

    为什么类只能使用public修饰? 首先,类只能使用public修饰是一个伪命题,应该说我们只见到过使用public修饰的类,还有一些类没有访问修饰符,此时访问权限为default.其次,类实际上分为 ...

  9. js上 十、循环语句-1:

    十.循环语句-1: 非常之重要. 作用:重复执行一段代码 ü while ü do...while ü for 它们的相同之处,都能够实现循环. 不同的地方,格式不一样,使用的场景略有不同. #10- ...

  10. vue第十单元(动态组件 keep-alive(钩子函数) 递归组件(name) 组件命名约定)

    第十单元(动态组件 keep-alive(钩子函数) 递归组件(name) 组件命名约定) #课程目标 熟练掌握动态组件的实现 掌握keep-alive缓存组件,以及相应的钩子函数 熟练掌握递归组件, ...