1. 添加普通账号

众所周知,linux下的root拥有最高权限,可以执行任何命令。在使用root身份操作时,有时的一个不注意就可能将非常重要的删除(最可怕的是 rm -rf /)。而linux不像windows有可以撤销的回收箱,。所以建议建立普通用户账号,在平时的时候以普通用户身份登录,只在需要root权限时才通过sudo 临时提高普通用户的权限或是通过su - 切换到root用户,执行完任务后立刻exit。

新建普通用户,用户名以example_user 为例

useradd example_user && passwd example_user
# 将对应的用户加入wheel组,wheel组用于sudo权限
usermod -aG wheel example_user

2. 创建ssh登录时进行身份验证的密钥对

假设有以下情景,有3台主机:

  • node3    ip: 192.168.35.120
  • node4    ip:  192.168.35.130
  • node5    ip: 192.168.35.140

node3上的用户root 想通过私钥 有密码登录node4,无密码登录node5

# 配置密码登录 node4
# 产生4096位的rsa密钥对
[root@node3 .ssh]# ssh-keygen -b
Generating public/private rsa key pair.
# 指定存储路径
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/node4_id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/node4_id_rsa.
Your public key has been saved in /root/.ssh/node4_id_rsa.pub. # 将公钥发给node4主机,追加在 root用户的~/.ssh/authorized_keys文件末尾
[root@node3 .ssh]# ssh-copy-id -i /root/.ssh/node4_id_rsa.pub root@node4
The authenticity of host 'node4 (192.168.35.130)' can't be established.
ECDSA key fingerprint is a7::be::f5:b5::1f:ce::ea:6d:df:e2:1a:.
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: key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node4's password: Number of key(s) added: Now try logging into the machine, with: "ssh 'root@node4'"
and check to make sure that only the key(s) you wanted were added. # 远程登录
[root@node3 .ssh]# ssh -i ~/.ssh/node4_id_rsa root@node4
Enter passphrase for key '/root/.ssh/node4_id_rsa':
Last login: Fri Sep :: from 192.168.35.1 # 配置无密码登录node5
[root@node3 .ssh]# ssh-keygen -b
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/node5_id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/node5_id_rsa.
Your public key has been saved in /root/.ssh/node5_id_rsa.pub.
The key fingerprint is:
:ef::a2::f1:::af:bf:::a7:7d:ed:2b root@node3 [root@node3 .ssh]# ssh-copy-id -i ~/.ssh/node5_id_rsa.pub root@node5
The authenticity of host 'node5 (192.168.35.140)' can't be established.
ECDSA key fingerprint is a7::be::f5:b5::1f:ce::ea:6d:df:e2:1a:.
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: key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node5's password: Number of key(s) added: Now try logging into the machine, with: "ssh 'root@node5'"
and check to make sure that only the key(s) you wanted were added. [root@node3 .ssh]# ssh -i ~/.ssh/node5_id_rsa root@node5
Last login: Fri Sep :: from 192.168.35.1

除了ssh-copy-id,还可以通过下面的方法进行公钥的上传

Step1: 通过ssh远程登录

ssh 用户名@ip地址远程登录

Step 2:  通过文件上传工具如filezilla,或是直接通过命令rz(通过yum install lrzsz安装)上传公钥 xxx.pub

Step 3:  将公钥以追加的形式写入authorized_keys文件中(该文件可以记录多个公钥信息)

cat xxx.pub >> ~/.ssh/authorized_keys

Step4 : 文件权设置

# chmod  ~/.ssh
# chdmo ~/.ssh/authorized_keys

注意,此时仍能通过密码进行登录

[root@node3 .ssh]# ssh root@node4
root@node4's password:
Last login: Fri Sep :: from node3
[root@node4 ~]#

3. 修改配置文件,禁止密码登录

修改配置文件 /etc/ssh/sshd_config

# 禁止使用root身份进行远程登录,建议使用普通用户身份登录[可根据实际情况]
PermitRootLogin no
# 取消密码验证登录
PasswordAuthentication no

然后重启服务即可

sudo service sshd restart

测试效果如下:

# 普通用户可以通过私钥登录
[alex@node3 ~]$ ssh alex@node4
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
[alex@node3 ~]$ ssh -i ~/.ssh/node4_id_rsa alex@node4
Last login: Sat Sep ::
[alex@node4 ~]$ exit
logout
Connection to node4 closed. [alex@node3 ~]$ su -
Password:
Last login: Sat Sep :: CST on pts/
# root无法登录

[root@node3 ~]# ssh root@node4
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
[root@node3 ~]# ssh -i ~/.ssh/node4_id_rsa root@node4
Enter passphrase for key '/root/.ssh/node4_id_rsa':
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

参考:

centos7下安全访问远程服务器的更多相关文章

  1. [转]SQLSERVER存储过程调用不同数据库的数据_存储过程中通过链接服务器访问远程服务器

    本文转自:http://blog.csdn.net/nnaabbcc/article/details/7967761 存储过程调用不同数据库的数据 在存储过程调用不同数据库的数据该如何做,比如在存储过 ...

  2. 使用nodejs和Java访问远程服务器的服务

    既然这篇文章用的是nodejs和Java访问远程服务器的服务,那么咱们先用另一门编程语言,SAP的ABAP(我日常工作使用得最多的编程语言)来开发一个服务吧. 这是我用ABAP编程语言实现服务的类:Z ...

  3. #在windows上使用ngix重定向目录访问远程服务器文件详细实例

    为了在开发环境保持于生产环境相同的访问远程服务器文件资源的目录配置,需要在开发环境(windows)在远程文件服务器使用nignx重定向文件目录,因为网上的资料大都是copy的,解释比较笼统,也没有具 ...

  4. CAS (6) —— Nginx代理模式下浏览器访问CAS服务器网络顺序图详解

    CAS (6) -- Nginx代理模式下浏览器访问CAS服务器网络顺序图详解 tomcat版本: tomcat-8.0.29 jdk版本: jdk1.8.0_65 nginx版本: nginx-1. ...

  5. 解决Centos7 下 root账号 远程连接FTP,vsftpd 提示 530 Login incorrect 问题

    原文:解决Centos7 下 root账号 远程连接FTP,vsftpd 提示 530 Login incorrect 问题 三步走: 1.vim /etc/vsftpd/user_list 注释掉 ...

  6. Mac下ssh连接远程服务器时自动断开问题

    在mac下使用securecrt通过ssh连接远程服务器时,总会一段时间没有动作后,ssh被自动断开.在windows下用xmanager貌似没有遇到过这个问题. 在网上找了解决方法如下: 客户端配置 ...

  7. CentOS7下BIND配置主从服务器和缓存服务器

    系统环境:CentOS Linux release 7.4.1708 (Core)  3.10.0-693.el7.x86_64 软件版本:bind-chroot-9.9.4-51.el7_4.1.x ...

  8. mac下ssh到远程服务器时中文乱码

    前言:mac本地的语言环境为英文,远程是支持中文的, 问题: 一开始是在iterm2下登录远程服务器更新数据库时发现中文注释不能正常显示,以为是iterms2下设置有问题,使用系统自带的termina ...

  9. LINUX的SSH下FTP到远程服务器Entering Passive Mode失败解决

    LINUX 系统FTP连接远程服务器经常出现在传输文件或者发出 ls命令时候出现 "Entering Passive Mode "然后就再也无法运作了.该工作主要是因为LINUX的 ...

随机推荐

  1. Wincc数据库连接代码生成方法

    对于数据库的连接,最简便的方法是: 在桌面新建文本文档(.TXT) 将文本后缀改为.udl文件 打开修改后缀后的文件界面如下所示(选择驱动程序): 选择连接的数据库,如下所示: 选择数据库中的一个数据 ...

  2. 第6天【egrep、bash环境配置及脚本、vim编辑器】

    bash环境配置及脚本(02)_recv bash环境配置及脚本(02)_recv bash环境配置文件: 按生效范围划分,存在两类: 全局配置: /etc/profile /etc/bashrc 个 ...

  3. vim 插件 -- omnicppcomplete

    omnicppcomplete 插件是基于ctags来实现补全的.所以,要先安装好ctags才可以使用. 下载 https://www.vim.org/scripts/script.php?scrip ...

  4. 2019-04-11-day030-网络编程并发

    内容回顾 黏包 tcp协议为了保证数据的可靠传输和传输效率 合包机制 : 连续多条短数据会合并成一条 拆包机制 : 一个过大的数据会在发出之前被拆成几个小包 tcp的黏包发生在两端: 发送端 : 合包 ...

  5. VirtualBox安装CENTOS7.3常见问题

    1 DHCP 问题无法上网解决 :sudo dhclient 2 安装宝塔面板:yum install -y wget && wget -O install.sh http://dow ...

  6. 剑指Offer 52. 正则表达式匹配 (字符串)

    题目描述 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式 ...

  7. python基础--列表、元祖、字典、集合

    列表(List) 1.列表特点 列表是可变的!! list 是一种有序的序列,可以添加.删除其中的元素,并且可以通过下标(索引)访问 数据 2.简单的常用操作 A.通过下表访问元素 print(lis ...

  8. What is the RESTful API ?

    REST 是 RepresentationalStateTransfer 的缩写,一般中文译为 “表征状态转移”,Roy Thomas Fielding 在他2000年的PhD论文中发明了这个概念.首 ...

  9. day05 字典

    今日内容(dict) 1.基本格式 2.独有方法 3.公共 4.强制转换 1.基本格式 字典(可变类型,3.6之后是有序) 帮助用户去表示一个事物的信息(事物是有多个属性) 键值不能为集合,列表,字典 ...

  10. Nginx安装详细指南

    nginx安装环境 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境.  gcc 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有g ...