实验主机
server01.yt.com | 10.1.1.3 | NAT |
server02.yt.com | 10.1.1.4 | NAT |
一、搭建SSH服务
1.所有主机关闭防火墙与SELinux
# 关闭firewalld防火墙
# 临时关闭
# systemctl stop firewalld
# 关闭开机自启动
# systemctl disable firewalld
# 关闭selinux
# 临时关闭
# setenforce 0
# 修改配置文件 永久关闭
# vim /etc/selinux/config
SELINUX=disabled
2.配置YUM源
配置外网YUM源 (阿里云)
# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# yum clean all
# yum makecache
3.OPENSSH软件的安装
# 安装
yum install openssh -y
# 检查openssh是否安装成功
rpm -qa |grep openssh
# 获取openssh生成的文件列表
rpm -ql openssh-server
4.修改服务端配置文件
# 禁止通过root账号远程登录此服务器
# vim /etc/ssh/sshd_config
38行 PermitRootLogin no
5.SSH服务管理
# systemctl restart sshd => 重启
# systemctl status sshd => 状态
# systemctl stop sshd => 停止
# systemctl start sshd => 启动
# systemctl enable sshd => 开机自启动
# systemctl disable sshd => 开机不自启
# ps -ef |grep sshd => 进程
或
# netstat -tnlp |grep sshd => 端口
或
# ss -naltp |grep sshd
二、SSH服务任务解决方案
1.server01上创建用户并授权
第一步:创建用户与用户组(html前端组,tom与jerry)
# 创建html前端组
# groupadd html
# 创建组内用户tom与jerry
# useradd -g html tom
# useradd -g html jerry
第二步:为用户添加密码
# echo 123456 |passwd --stdin tom
# echo 123456 |passwd --stdin jerry
第三步:为开发人员创建数据目录并且设置相应的权限
① 创建用户的数据目录:
# mkdir -p /code/html => 前端组
# ll -d /code/html
drwxr-xr-x. 2 root root 6 May 24 10:36 /code/html
② 更改目录的文件所属组(更改为html,代表html组内成员可以对这个目录进行管理)
# chgrp -R html /code/html
drwxr-xr-x. 2 root html 6 May 24 10:36 /code/html
# chmod -R g+w /code/html
drwxrwxr-x. 2 root html 6 May 24 10:36 /code/html
③ 添加粘滞位权限,防止误删除操作
# chmod 1770 /code/html
drwxrwx--T. 2 root html 6 May 24 10:36 /code/html
2.更改SSH默认端口
server01上修改默认端口
# vim /etc/ssh/sshd_config
17行 Port 3712
3.重启SSH服务
# systemctl restart sshd
或
# systemctl reload sshd
4.在server02创建一个code账号
# useradd code
# echo 123456 |passwd --stdin code
测试:在server01远程连接server02
# ssh -p 3721 code@10.1.1.4
5.SSH客户端不验证指纹
如果我们不想验证指纹,可以通过更改SSH客户端的配置文件。
server01上配置
# vim /etc/ssh/ssh_config
35行 StrictHostKeyChecking no
6.SSH免密登录的具体实现
SSH免密的实现思路一共分为三个步骤(三步走)
第一步:在A主机针对某个账号(tom或jerry)生成公钥与私钥
第二步:使用某些方法把公钥发送到B主机中,然后追加到authorized_keys文件中
第三步:测试是否实现免密登录
方法一:
① 在server01针对某个账号生成公钥与私钥
# ssh-keygen
② 使用ssh-copy-id把公钥文件中的内容传输到服务器端的~/.ssh/authorized_keys文件中
# ssh-copy-id -p 3712 code@10.1.1.4
code@10.1.1.4's password:123456
方法二:
① 生成公钥与私钥
# ssh-keygen
② 把id_rsa.pub文件,scp到RealServer服务器端
# scp -P 3721 ~/.ssh/id_rsa.pub code@11.1.1.100:/home/code/
③ 在server02中把id_rsa.pub文件中的内容追加到~/.ssh/authorized_keys文件中
# cd ~
# cat id_rsa.pub >> ~/.ssh/authorized_keys