【linux】之SSH远程管理服务
加密算法
☆ 對稱加密算法(DES)
☆ 非對稱加密算法(RSA)
☆ 對稱加密與非對稱加密區(qū)別
- 對稱加密
- 使用同一個密鑰進行加密和解密,密鑰容易泄露
- 加密速度快,效率高,數(shù)據(jù)傳輸速度快,安全性較低
- 非對稱加密
- 使用不同的密鑰(公鑰和私鑰)進行加密和解密
- 加密速度遠遠慢于對稱加密,數(shù)據(jù)傳輸速度慢,安全性較高
SSH基于用戶名密碼的認證原理
基本語法:
# ssh [選項] 遠程服務器的用戶名@遠程服務器的IP地址 選項說明: -p:指定ssh服務的端口號,默認為22案例:通過JumpServer與RealServer遠程連接,了解SSH基于用戶名密碼的認證原理
JumpServer:
# ssh root@10.1.1.38認證原理:
SSH服務搭建
1、所有服務的搭建思路
- 關閉防火墻和selinux(實驗環(huán)境都先關閉掉)
- 配置yum源(公網(wǎng)源或者本地源)
- 軟件安裝和檢查
- 了解并修改配置文件(核心)
- 啟動服務檢查運行狀態(tài)并設置開機自啟動
2、搭建SSH服務器(JumpServer與RealServer)
第一步:關閉防火墻與SELinux
# systemctl stop firewalld # systemctl disable firewalld# setenforce 0 # vim /etc/selinux/config SELINUX=disabled第二步:配置YUM源
JumpServer配置外網(wǎng)YUM源 => 阿里云
RealServer配置本地YUM源 => 把光盤鏡像作為倉庫(自建YUM倉庫)
① 掛載光盤
# 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② 編寫local.repo文件
# cd /etc/yum.repos.d # vim local.repo [local] name=local yum baseurl=file:///mnt/cdrom enabled=1 gpgcheck=03、靜態(tài)IP配置
☆ JumpServer網(wǎng)卡配置
給JumpServer配置兩張網(wǎng)卡(NAT模式 + 僅主機模式)
添加僅主機模式網(wǎng)卡:
重啟network網(wǎng)絡,然后使用ifconfig獲取僅主機模式的網(wǎng)卡信息。
由上圖可知,僅主機模式的網(wǎng)卡為ens37,NAT模式網(wǎng)卡為ens33。遇到一個問題,ens37這張網(wǎng)卡沒有配置文件:
添加ens37配置文件:
# cp ifcfg-ens33 ifcfg-ens37 # vim ifcfg-ens37 TYPE="Ethernet" BOOTPROTO="none" IPADDR=11.1.1.10 NETMASK=255.255.255.0 NAME="ens37" DEVICE="ens37" ONBOOT="yes"重啟計算機網(wǎng)絡:
# systemctl restart network☆ 關閉NetworkManager
Linux圖形化界面中的網(wǎng)絡管理器,有些時候我們設置了靜態(tài)IP。但是重啟網(wǎng)絡后,其并沒有生效或者和你設置的IP地址不一致,很可能是由于NetworkManager工具的影響。
# systemctl stop NetworkManager # systemctl disable NetworkManager☆ RealServer網(wǎng)卡配置
把NAT模式的網(wǎng)卡更改為僅主機模式,然后設置一個靜態(tài)IP地址。
由于現(xiàn)在只有一張網(wǎng)卡,所以僅主機模式對應的網(wǎng)卡為ens33,配置:
設置完成后,重啟計算機網(wǎng)絡
# systemctl stop NetworkManager # systemctl disable NetworkManager# systemctl restart network4、搭建SSH服務
關閉防火墻與SELinux
# 關閉firewalld防火墻 # 臨時關閉 systemctl stop firewalld # 關閉開機自啟動 systemctl disable firewalld# 關閉selinux # 臨時關閉 setenforce 0 # 修改配置文件 永久關閉 vim /etc/selinux/config SELINUX=disabledopenssh軟件的安裝
SSH服務底層的軟件名稱叫做openssh,open開源,ssh就是ssh服務。openssh屬于C/S架構軟件,其擁有客戶端與服務器端。
客戶端:ssh
服務端:openssh-server
安裝步驟:
# yum install openssh -y檢查openssh是否安裝成功
# rpm -qa |grep openssh 或 # yum list installed |grep openssh獲取openssh生成的文件列表
# rpm -ql openssh-server# 配置文件 /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手冊 /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查看并修改ssh服務端的配置文件
# man 5 sshd_configRealServer:禁止root賬號遠程登錄
# man 5 sshd_config PermitRootLogin => yes or no,默認為yes 代表允許通過root賬號遠程登錄此服務器 # vim /etc/ssh/sshd_config 38行 PermitRootLogin nosshd服務管理
# systemctl restart sshd => 重啟 # systemctl status sshd => 狀態(tài) # systemctl stop sshd => 停止 # systemctl start sshd => 啟動# systemctl enable sshd => 開機自啟動 # systemctl disable sshd => 開機不自啟# ps -ef |grep sshd => 進程 或 # netstat -tnlp |grep sshd => 端口 或 # ss -naltp |grep sshdSSH服務任務解決方案
1、創(chuàng)建用戶并授權
JumpServer跳板機創(chuàng)建用戶并授權
第一步:創(chuàng)建用戶與用戶組(html前端組,tom與jerry)
# 創(chuàng)建html前端組 # groupadd html# 創(chuàng)建組內(nèi)用戶tom與jerry # useradd -g html tom # useradd -g html jerry第二步:為用戶添加密碼
# echo 123456 |passwd --stdin tom # echo 123456 |passwd --stdin jerry第三步:為開發(fā)人員創(chuàng)建數(shù)據(jù)目錄并且設置相應的權限
① 創(chuàng)建用戶的數(shù)據(jù)目錄:
# mkdir -p /code/html => 前端組 # ll -d /code/html drwxr-xr-x. 2 root root 6 May 24 10:36 /code/html② 更改目錄的文件所屬組(更改為html,代表html組內(nèi)成員可以對這個目錄進行管理)
# 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/html2、測試用戶權限
測試用戶權限是否設置成功,可以結合第1步一起完成
3、禁用root登錄
RealServer服務器端:
# vim /etc/ssh/sshd_config PermitRootLogin no4、更改SSH默認端口
RealServer服務器端:
# vim /etc/ssh/sshd_config 17行 Port 37125、重啟SSH服務
# systemctl restart sshd 或 # systemctl reload sshdrestart與reload的本質(zhì)區(qū)別:
① restart其實相當于stop然后在start
② reload不停止現(xiàn)有業(yè)務,只是重新加載sshd對應的配置文件
6、在RealServer創(chuàng)建一個code賬號
# useradd code # echo 123456 |passwd --stdin code測試:在JumpServer遠程連接RealServer
# ssh -p 3721 code@11.1.1.1007、SSH客戶端不驗證指紋
第一次連接遠程服務器時:
The authenticity of host '11.1.1.100 (11.1.1.100)' can't be established. ECDSA key fingerprint is SHA256:Y/cQNWWkX15o2MsJ5HOQBI2m8S33qIA+x3zys8J4pOY. ECDSA key fingerprint is MD5:76:61:86:8b:d5:ee:bf:9c:60:e6:12:fa:f6:f0:74:36. Are you sure you want to continue connecting (yes/no)?yes Warning: Permanently added '11.1.1.100' (ECDSA) to the list of known hosts.如果我們不想驗證指紋,可以通過更改SSH客戶端的配置文件
JumpServer:
# vim /etc/ssh/ssh_config 35行 StrictHostKeyChecking no8、用專業(yè)工具pwgen生成用戶密碼
在實際生產(chǎn)環(huán)境中,其用戶密碼一定不要手工設置,建議使用專業(yè)的密碼生成工具如pwgen。
① 安裝隨機密碼生成工具pwgen
② 使用pwgen工具生成隨機密碼
③ 給賬號code設置密碼
第一步:創(chuàng)建code開發(fā)者賬號
# useradd code第二步:配置EPEL源,安裝pwgen工具
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # yum clean all # yum makecache第三步:安裝pwgen密碼生成工具
# yum install pwgen -y第四步:使用pwgen生成隨機密碼
# pwgen -cnBs1 10 1擴展:pwgen密碼生成器的使用
# pwgen --help # 用法: pwgen 選項參數(shù) 長度 生成個數(shù) Usage: pwgen [ OPTIONS ] [ pw_length ] [ num_pw ]# 密碼中至少包含一個大寫字母 -c or –capitalize# 密碼中不包含大寫字母 -A or –no-capitalize# 密碼中至少包含一個數(shù)字 -n or –numerals# 密碼中不包含數(shù)字 -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-vowelsSSH服務補充
1、scp命令
主要功能:用于Linux系統(tǒng)與Linux系統(tǒng)之間進行文件的傳輸(上傳、下載)
上傳:
# scp [選項] 本地文件路徑 遠程用戶名@遠程服務器的IP地址:遠程文件存儲路徑 -r : 遞歸上傳,主要針對文件夾 -P : 更換了SSH服務的默認端口必須使用-P選項下載:
# scp [選項] 遠程用戶名@遠程服務器的IP地址:遠程文件路徑 本地文件存儲路徑 -r : 遞歸上傳,主要針對文件夾 -P : 更換了SSH服務的默認端口必須使用-P選項2、踢出用戶
# 查看當前在線用戶 w # 踢出某個賬號 pkill -kill -t pts/1四、SSH免密登錄解決方案
SSH免密登錄的具體實現(xiàn)
SSH免密的實現(xiàn)思路一共分為三個步驟(三步走)
第一步:在A主機針對某個賬號(tom或jerry)生成公鑰與私鑰
第二步:使用某些方法把公鑰發(fā)送到B主機中,然后追加到authorized_keys文件中
第三步:測試是否實現(xiàn)免密登錄
☆ 方法一:比較常用(tom)
① 在A主機針對某個賬號生成公鑰與私鑰
# ssh-keygen注:如果不想一路確認,可以在ssh-keygen -P “”,直接生成公私鑰
② 使用ssh-copy-id把公鑰文件中的內(nèi)容傳輸?shù)椒掌鞫说膥/.ssh/authorized_keys文件中
# ssh-copy-id -p 3712 code@11.1.1.100 code@11.1.1.100's password:123456③ 在JumpServer客戶端測試免密登錄是否成功
# ssh -p 3721 code@11.1.1.100☆ 方法二:集群常用(jerry)
① 生成公鑰與私鑰
# ssh-keygen② 把id_rsa.pub文件,scp到RealServer服務器端
# scp -P 3721 ~/.ssh/id_rsa.pub code@11.1.1.100:/home/code/③ 在RealServer服務器端,把id_rsa.pub文件中的內(nèi)容追加到~/.ssh/authorized_keys文件中
# cd ~ # cat id_rsa.pub >> ~/.ssh/authorized_keys注意事項:以上配置也比較簡單,但是實際應用時要注意文件的權限
RealServer: ~/.ssh : 700 ~/.ssh/authorized_keys : 600④ 測試免密是否成功
ssh -p 3721 code.1.1.100總結
以上是生活随笔為你收集整理的【linux】之SSH远程管理服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sysaux表空间清理
- 下一篇: linux 其他常用命令