搭建SSH服务

1.搭建SSH服务

1)安装nodejs服务

nodejs是一个运行环境,和javajdk运行环境是一样的
[root@tdr ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@tdr ~]# yum clean all
[root@tdr ~]# yum makecache
[root@tdr ~]# yum -y install epel-release.noarch                 //必须先把原先的epel卸载掉,否则找不到nodejs
[root@tdr ~]# yum list | grep nodejs
[root@tdr ~]# yum -y install nodejs.x86_64

2)简易服务器环境

安装包管理器 npm node package manager
[root@tdr ~]# yum -y install npm
[root@tdr ~]# npm -v
8.19.4
[root@tdr ~]# npm config set registry https://registry.npmmirror.com           //npm设置国内镜像
[root@tdr ~]# npm install @vue/cli -g                                                   //安装vue的脚手架软件,-g是全局安装
[root@tdr ~]# find / -name "vue"
[root@tdr ~]# /usr/local/bin/vue -V
@vue/cli 5.0.8
[root@tdr ~]# 
使用vue脚手架创建一个前端项目 
在当前目录创建vuehtml000目录,程序员就可以在这个文件中开发前端页面 
[root@tdr ~]# /usr/local/bin/vue create vuehtml000
[root@tdr ~]# tree vuehtml000/                      //用树状显示
启动前端项目
[root@tdr ~]# cd vuehtml000/
[root@tdr vuehtml000]# npm run server
DONE  Compiled successfully in 202ms                                                          10:27:19


  App running at:
  - Local:   http://localhost:8080/ 
  - Network: http://192.168.1.100:8080/

浏览器访问http://192.168.1.100:8080/                         //记得关闭防火墙
[root@tdr vuehtml000]# npm run build                     //构建html000的静态页面
将这些静态部署到nginx的html
[root@tdr vuehtm1000]# ls ./dist/
css  favicon.ico  index.html  js
[root@tdr vuehtml000]# find / -name "html" -type d                        //找到nginx的html
[root@tdr vuehtml000]# cp -R ./dist/* /usr/share/nginx/html/
[root@tdr vuehtml000]# nginx 
[root@tdr vuehtml000]# nginx -s reload                                         //重启
浏览器访问http://192.168.1.100

非对称加密   ras
有两条密码本
公钥            用于加密            保障不是明文传输
私钥            用于解密            将加密的文件解析成明文

[root@tdr vuehtm1000]# netstat -lntup|grep sshd               //查看端口占用
tcp        0      0 0.0.0.0:22              0.0.0.0:*

知道错了没:

[root@tdr vuehtm1000]# netstat -lntup|grep sshd               //查看端口占用
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1134/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      1134/sshd           


3)远程管理SSH服务

安装SSH服务:openssh、ssh-server、ssh-client
[root@tdr ~]# ssh -l root -p 22 192.168.1.125
root@192.168.1.125's password: 
Last login: Wed Jul 17 11:32:25 2024 from 192.168.1.1
[root@tds ~]# 

(1)关闭防火墙以及selinux(selinux不关改不了端口)

[root@tdr ~]# systemctl stop friewalld
[root@tdr ~]# systemctl disable firewalld
[root@tdr ~]# setenforce 0
[root@tds ~]# systemctl stop firewalld
[root@tds ~]# systemctl disable firewalld
[root@tds ~]# setenforce 0

(2)下载openssh、ssh-server、ssh-client

[root@tds ~]# yum -y install openssh
[root@tds ~]# yum -y install openssh-server
[root@tds ~]# yum -y install openssh-clients

(3)开启、关闭

[root@tds ~]# systemctl start sshd         //启动
[root@tds ~]# systemctl stop sshd         //关闭
[root@tds ~]# systemctl restart sshd                     //重启
[root@tds ~]# systemctl disable sshd                      //开机不自启

(4)远程连接另一台主机

[root@tdr ~]# ssh -l root -p 22 192.168.1.125
root@192.168.1.125's password: 
Last login: Wed Jul 17 11:32:25 2024 from 192.168.1.1
[root@tds ~]# 

(5)修改配置文件,不允许root账户远程登陆

[root@tdr ~]# vim /etc/ssh/sshd_config 
[root@tdr ~]# systemctl restart sshd
38    PermitRootLogin no                     //去掉注释,yes改为no
去另一台主机
[root@tds ~]# ssh -l root -p 22 192.168.1.100         //用root用户远程登陆
root@192.168.1.100's password: 
Permission denied, please try again.
root@192.168.1.100's password: 
Permission denied, please try again.
root@192.168.1.100's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).              //未登陆成功
[root@tds ~]# 
[root@tds ~]# ssh -l tdr -p 22 192.168.1.100
tdr@192.168.1.100's password: 
Last login: Wed Jul 17 14:40:40 2024 from 192.168.1.125         //远程登陆100主机tdr用户成功
[tdr@tdr ~]$ exit         //退出登录

知道错了没:
[tdr@tdr ~]$ exit         //退出登陆

(6)创建两个用户

[root@tds ~]# useradd zhangsan
[root@tds ~]# useradd lisi
[root@tds ~]# passwd zhangsan       //添加密码
[root@tds ~]# passwd lisi
[root@tds ~]# mkdir /code/               //创建用户数据目录
[root@tds ~]# groupadd code                  //创建附属组
[root@tds ~]# usermod -g code zhangsan             //将用户添加到组
[root@tds ~]# usermod -g code lisi 
[root@tds ~]# chgrp -R code /code/              //将code所属组修改为code组
[root@tds ~]# chmod -R g+w /code/          //给属组增加写的权限


回到原来的主机
[root@tds ~]# vim /etc/ssh/sshd_config
17 Port 9999                                 //去掉注释,修改端口为9999
[root@tds ~]# systemctl restart sshd                    //重启ssh服务

去另一台主机
ssh连接服务器,如果服务端⼝是22,可以不⽤添加-p选项
如果不是22端⼝,就不许添加-p选项
ssh -p 9999 -l lisi 192.168.71.135    
或ssh -p 9999 lisi@192.168.71.135
[root@tds ~]# ssh -l tdr -p 9999 192.168.1.100
tdr@192.168.1.100's password: 
Last login: Wed Jul 17 14:50:55 2024 from 192.168.1.125
[tdr@tdr ~]$                                             //连接成功
[tdr@tdr ~]$ exit
登出
Connection to 192.168.1.100 closed.
[root@tds ~]# 

(7)⽤专业⼯具pwgen⽣成⽤户密码

[root@tdr ~]# yum -y install pwgen
[root@tdr ~]# pwgen -cnBs1 10 1

(8)扩展:pwgen密码⽣成器的使⽤

⽤法: pwgen 选项参数 ⻓度 ⽣成个数
Usage: pwgen [ OPTIONS ] [ pw_length ] [ num_pw ]
# 密码中⾄少包含⼀个⼤写字⺟
-c or –capitalize
# 密码中不包含⼤写字⺟
-A or –no-capitalize
# 密码中⾄少包含⼀个数字
-n or –numerals
# 密码中不包含数字
-0 or –no-numerals
# 密码中⾄少包含⼀个特殊符号
-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


2.SSH服务补充

1)scp命令

主要功能:⽤于Linux系统与Linux系统之间进⾏⽂件的传输(上传、下载)
上传:
 scp [选项] 本地⽂件路径 远程⽤户名@远程服务器的IP地址:远程⽂件存储路径
-r : 递归上传,主要针对⽂件夹
-P : 更换了SSH服务的默认端⼝必须使⽤-P选项
下载:
scp [选项] 远程⽤户名@远程服务器的IP地址:远程⽂件路径 本地⽂件存储路径
-r : 递归上传,主要针对⽂件夹
-P : 更换了SSH服务的默认端⼝必须使⽤-P选项

2)踢出用户

[root@tds ~]# w                             //查看当前在线用户
 17:18:35 up  3:52,  3 users,  loa

知道错了没:
2)踢出用户

[root@tds ~]# w                             //查看当前在线用户
 17:18:35 up  3:52,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      14:39    2:37m  0.02s  0.02s -bash
root     pts/2    192.168.1.1      13:52    3.00s  0.14s  0.13s ssh -l root -p 22 192.168.1.125
root     pts/3    tds              14:46    3.00s  0.21s  0.02s w
[root@tds ~]# pkill -kill -t pts/1                //踢出某个账号


3.ssh免密登录

[zhangsan@y root]$ ssh-keygen //按三次回车
[zhangsan@y ~]$ ls ./.ssh/ 
id_rsa  id_rsa.pub  known_hosts
[zhangsan@y ~]$ ssh-copy-id root@192.168.1.20
id_rsa:保存私钥
id_rsa.pub:保存公钥
authorized_keys:保存已授权的客户端公钥
known_hosts:保存已认证的远程主机公钥
 

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值