前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通过async程序配合私钥认证,机械硬盘加树莓派,对个人服务器进行低成本容灾

通过async程序配合私钥认证,机械硬盘加树莓派,对个人服务器进行低成本容灾

作者头像
zhaoolee
发布2021-07-01 10:25:39
4450
发布2021-07-01 10:25:39
举报
文章被收录于专栏:木子昭的博客木子昭的博客

公钥相当于锁,私钥相当于钥匙,在安装了ssh的计算机中, 都有 ssh-keygen 这个命令

运行 ssh-keygen 命令, 可以同时生成一组公钥和私钥, 放在 $HOEM/.ssh 目录下

运行ssh-keygen命令后一路回车, 即可在 $HOME/.ssh/目录下发现 id_rsa 和 id_rsa.pub 这个两个文件

生成公钥和私钥

然后我们拷贝 id_rsa.pub 中的所有字符串, 粘贴到服务器端的 $HOME/.ssh/authorized_keys 中, 就相当于把锁放到服务器上了, authorized_keys 里面可以放很多锁, 每个锁就是一行字符串.

在个人电脑这一侧, 我们也可以存放很多钥匙, 这些钥匙需要一个钥匙串统一管理, 记录每一把钥匙, 对应哪一把锁, 这个钥匙串就是 $HOME/.ssh/config 文件, 在文件中可以配置多个以下的代码块

代码语言:javascript
复制
# host 可以是ip 也可以是绑定ip的域名
Host 47.23.55.66
# 需要通过ssh登陆的用户名
User root
# 私钥钥在个人电脑中存放的位置
IdentityFile ~/.ssh/id_rsa

当我们在个人电脑使用 ssh root@47.23.55.66 登录服务器时, ssh程序会自动使用 ~/.ssh/id_rsa 私钥内容与服务端进行通信, 进行登录验证

另外为了保证私钥和config文件的安全,私钥和config权限配置为 600

代码语言:javascript
复制
chmod 600 config
chmod 600 id_rsa

在客户端,如果要搬家, 只需把.ssh文件夹全部搬走,就实现全部密钥和配置文件的搬家,十分方便。

通过查阅config文件我们还可以了解自己手底下掌管了多少台机器,每台机器的ip, 用户名,和密钥文件尽收眼底

在服务端

sshd

我们前面使用的是客户端程序ssh, 而服务端用来给ssh提供服务的是sshd,也就是服务端程序

如果你按照上文,配置好公钥密钥后,无法实现ssh自动登录, 那需要检查sshd的配置文件 /etc/ssh/sshd_config, 以下是我服务器的配置, 可以直接拷贝覆盖。

代码语言:javascript
复制
#       $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm 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 22
#ListenAddress 0.0.0.0
#ListenAddress ::

# The default requires explicit activation of protocol 1
#Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
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

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Ciphers and keying
#RekeyLimit default none

# Logging
# obsoletes QuietMode and FascistLogging
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#StrictModes yes
#MaxAuthTries 6
MaxSessions 10000

#RSAAuthentication yes
#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
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and 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!
#PermitEmptyPasswords 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
# PAM authentication via ChallengeResponseAuthentication may bypass
# If you just want the PAM account and session checks to run without
# 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          # Default for new installations.
#PermitUserEnvironment no
#Compression delayed
ClientAliveInterval 60
#ClientAliveCountMax 3
#ShowPatchLevel no
#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
UseDNS no
AddressFamily inet
PermitRootLogin yes
SyslogFacility AUTHPRIV
PasswordAuthentication yes

设置免登录后有什么好处?

  • 可以不用记密码
  • 可以愉快的使用scp命令, 将服务器上的文件夹子备份到本地硬盘

备份

  • 不止scp, 还能使用支持增量同步,断点续传的,更高级的async命令

rsync -azvvv --bwlimit=100 --append-verify root@v2fy.com:/opt/ ./opt/

代码语言:javascript
复制
-a 递归同步,同步源信息
-z 对进行同步的数据文件进行压缩后传输
-v 显示传输细节 (-vvv细节更详细)
--bwlimit 限制传输带宽 单位为kb
--append-verify 支持断点续传,并对传输完成的文件进行校验

rsync

通过async程序配合私钥认证,对个人服务器进行低成本容灾

编写简单的定时小程序,部署到外接一个大机械硬盘的树莓派,程序每日凌晨定时运行,通过 rsync 对服务器资源进行备份,按日期新建文件夹,保留服务器30天的记录,即使服务器丢失了数据, 也能快速回滚数据, 降低损失。

个人站长,并没有大量的金钱去购买大量服务器空间和企业级安全服务,进行数据容灾, 防黑客攻击;

如果个人服务器被黑客攻破服务器,清空了数据,就会十分悲惨。

而服务器存储的只是私钥, 黑客可以即使攻破了服务器,也无法定位到持有私钥的树莓派。最极端的情况,把硬盘从树莓派一拔,即使黑客通过社会工程学,顺着网线找到你的树莓派,也无法删除与树莓派分离的硬盘数据。

在2021年,将服务器数据通过私钥认证,下载到本地,保存到机械硬盘里,依然是最经济,也最稳定的数据备份方式。

树莓派加1TB机械硬盘配合私钥使用async进行增量同步,足以容纳个人服务器最近一个月的数据

树莓派加1TB机械硬盘配合私钥使用async进行增量同步,足以容纳个人服务器最近一个月的数据,5W的树莓派,一个月备份的额外成本只有几度电而已~

本文永久更新地址(欢迎来读留言,写评论):

https://www.v2fy.com/p/2021-06-28-ssh-1624891495000

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在服务端
  • 设置免登录后有什么好处?
  • 通过async程序配合私钥认证,对个人服务器进行低成本容灾
  • 本文永久更新地址(欢迎来读留言,写评论):
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档