7.17 远程管理SSH服务

⼀、搭建SSH服务
0.安装ssh服务
1.openssh
2.ssh-server
3.ssh-client
cs c lient-server
bs
系统默认安装openssh
1、关闭防⽕墙与SELinux(不关SElinux导致sshd的端⼝ ⽆法修改)
2、配置yum源
JumpServer配置外⽹YUM源 => 阿⾥云
RealServer配置本地YUM源 => 把光盘镜像作为仓库(⾃建YUM仓库)
① 挂载光盘
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
# 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
② 编写local.repo⽂件
3、openssh软件的安装
SSH服务底层的软件名称叫做openssh,open开源,ssh就是ssh服务。openssh属于C/S架构
软件,其拥有客户端与服务器端。
客户端:ssh
服务端:openssh-server
安装步骤:
检查openssh是否安装成功
# mkdir /mnt/cdrom
# mount -o ro /dev/sr0 /mnt/cdrom
# chmod +x /etc/rc.local
# echo 'mount -o ro /dev/sr0 /mnt/cdrom' >> /etc/rc.local
# cd /etc/yum.repos.d
# vim local.repo
[local]
name=local yum
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
# yum install openssh -y
# 配置⽂件
/etc/ssh/sshd_config => ssh服务的主配置⽂件
/etc/sysconfig/sshd
# 服务管理脚本
/usr/lib/systemd/system/sshd.service => systemctl start sshd
# ⽂件共享服务 提供⽂件上传下载的服务
/usr/libexec/openssh/sftp-server
# ⼆进制⽂件程序⽂件
/usr/sbin/sshd
# 公钥⽣成⼯具
/usr/sbin/sshd-keygen
# man ⼿册
/usr/share/man/man5/sshd_config.5.gz
/usr/share/man/man8/sftp-server.8.gz
/usr/share/man/man8/sshd.8.gz
# rpm -ql openssh-clients
# 客户端配置⽂件
/etc/ssh/ssh_config
# 远程 copy 命令 服务器间进⾏⽂件传输
/usr/bin/scp
# sftp 客户端 上传下载⽂件操作
/usr/bin/sftp
/usr/bin/slogin
/usr/bin/ssh
/usr/bin/ssh-add
/usr/bin/ssh-agent
/usr/bin/ssh-copy-id
/usr/bin/ssh-keyscan
# 客户端 man ⼿册
4、查看并修改ssh服务端的配置⽂件
RealServer:禁⽌root账号远程登录
在配置⽂件设置permitRootlogin no,不允许root账户远程登录可以设置其他账户远程登录。
zhangsan
5、sshd服务管理
/usr/share/man/man1/scp.1.gz
/usr/share/man/man1/sftp.1.gz
/usr/share/man/man1/slogin.1.gz
/usr/share/man/man1/ssh-add.1.gz
/usr/share/man/man1/ssh-agent.1.gz
/usr/share/man/man1/ssh-copy-id.1.gz
/usr/share/man/man1/ssh-keyscan.1.gz
/usr/share/man/man1/ssh.1.gz
/usr/share/man/man5/ssh_config.5.gz
/usr/share/man/man8/ssh-pkcs11-helper.8.gz
# man 5 sshd_config
# man 5 sshd_config
PermitRootLogin => yes or no,默认为yes 代表允许通过root账号远程登录此服务器
# vim /etc/ssh/sshd_config
38⾏ PermitRootLogin no
⼆、SSH服务任务解决⽅案
1、创建⽤户并授权
JumpServer跳板机创建⽤户并授权
第⼀步:创建⽤户与⽤户组(html前端组,tom与jerry)
第⼆步:为⽤户添加密码
# 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
# 创建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、测试⽤户权限
测试⽤户权限是否设置成功,可以结合第1步⼀起完成
3、禁⽤root登录
RealServer服务器端:
4、更改SSH默认端⼝
RealServer服务器端:
1.修改 vim /etc/ssh/sshd_config 第17⾏的# 删除,22换成9999
# vim /etc/ssh/sshd_config
38⾏ PermitRootLogin no
# vim /etc/ssh/sshd_config
17⾏ Port 99992.重启sshd服务,
setenforce 0 停⽤selinux
systemctl stop firewalls 停⽤防⽕墙
systemctl restart ssh
重启ssh服务
ssh连接服务器,如果服务端⼝是22,可以不⽤添加-p选项
如果不是22端⼝,就不许添加-p选项
s sh -p9999 -llisi 192.168.71.135
Ssh -p9999 lisi@192.168.71.135
5、重启SSH服务
# systemctl restart sshd
# systemctl reload sshd
三、SSH服务补充
1、scp命令
主要功能:⽤于Linux系统与Linux系统之间进⾏⽂件的传输(上传、下载)
上传:
# 密码中⾄少包含⼀个特殊符号
-y or –symbols
# ⽣成完全随机密码
-s or –secure
# 密码中不包含歧义字符(例如1,l,O,0)
-B or –ambiguous
# 使⽤SHA1 hash给定的⽂件作为⼀个随机种⼦
-H or –sha1=path/to/file[#seed]
# 在列中打印⽣成的密码
-C
# 不要在列中打印⽣成的密码,即⼀⾏⼀个密码
-1
# 不要使⽤任何元⾳,以避免偶然的脏话
-v or –no-vowels下载:
scp 指定端⼝ - P ⼤写
ssh 指定端⼝ -p ⼩写
使⽤的默认的22端⼝,不需要指定
scp上传也要注意⽤户的权限问题,没有权限的⽬录⽆法上传
四、SSH免密登录解决⽅案
# scp [选项] 本地⽂件路径 远程⽤户名@远程服务器的IP地址:远程⽂件存储路径
-r : 递归上传,主要针对⽂件夹
-P : 更换了SSH服务的默认端⼝必须使⽤-P选项
# scp [选项] 远程⽤户名@远程服务器的IP地址:远程⽂件路径 本地⽂件存储路径
-r : 递归上传,主要针对⽂件夹
-P : 更换了SSH服务的默认端⼝必须使⽤-P选项
# 查看当前在线⽤户
# 踢出某个账号
pkill -kill -t pts/1
五.SSH免密登录的具体实现
SSH免密的实现思路⼀共分为三个步骤(三步⾛)
第⼀步:在A主机针对某个账号(tom或jerry)⽣成公钥与私钥
第⼆步:使⽤某些⽅法把公钥发送到B主机中,然后追加到authorized_keys⽂件中
第三步:测试是否实现免密登录
  • 13
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值