Linux之sshd服務


---恢復內容開始---

 

---恢復內容結束---

一、linux中對服務管理與控制(以sshd為例)

1.什么是服務

  可以用來給客戶提供相關操作,對自己沒有什么好處

2.用什么控制服務
系統初始化進程可以對服務進行相應的控制

3.當前系統初始化進程是什么
   systemd    ##系統初始化進程
   pstree     ##顯示系統中的進程樹

4.進程控制命令
  ssh -------->sshd(ssh用為客戶端,主要進行服務器端的連接;sshd用為服務器端)

  systemctl              ##服務控制命令

  systemctl start sshd   ##開啟服務

  systemctl stop sshd    ##關閉服務

  systemctl restart sshd ##重啟服務

  systemctl reload sshd  ##重新加載服務配置

  systemctl enable sshd  ##設定服務開機啟動

  systemctl disable sshd ##設定服務開機不啟動

  systemctl list-units   ##列出已經開啟服務當前狀態

  systemctl list-dependencies ##列出服務的倚賴

  systemctl set-default multi-user.target ##設定系統啟動級別為多用戶模式(無圖形)

  systemctl set-default graphical.target  ##設定系統啟動級別為圖形模式

  systemctl status sshd  ##查看服務狀態,inactive(不可用),active(可用) 

  注意:也可以使用init 5使界面圖形化當使用關閉sshd關閉服務的時候 ,sshd狀態會發生改變(此時狀態為dead),此時不可以進行連接 

  •    使用開啟命令的時候,狀態為running

        

  •   默認情況下,sshd服務是開機時自啟動的,也可以進行設置。以下設置sshd為開機不是自動啟動  

    重啟后,不可以使用ssh服務

    [kiosk@foundation77 Desktop]$ ssh root@172.25.254.177
    ssh: connect to host 172.25.254.177 port 22: Connection refused
  •   使用命令設置sshd服務開機為自啟動(開機后就可以直接進行登)

      

  •   使用命令systemctl list-unit-files 來列出所有服務開機啟動的狀態 (disable,enable,static)  
       

  若想對外開啟服務,可以使用systemctl start ssh來開啟服務接口。

  對於服務來說,是不可以關閉的;若修改了配置文件,只能通過systemctl restart  sshd進行重新啟動。

二、sshd服務

1.sshd介紹
     sshd為secure shell的簡稱;可以通過網絡在主機中開機shell的服務

 連接方式(在客戶端):ssh username@ip  #文本模式
                    ssh -X username@ip  #可以在鏈接成功后開啟圖形界面

 注意:
    第一次鏈接陌生主機是要建立認證文件,然后會詢問是否建立,需要輸入yes
    再次鏈接此台主機時,因為已經生成~/.ssh/know_hosts文件所以不需要再次輸入yes

 遠程復制:  格式 scp file root@id:dir(文件的上傳)

                scp root@if:/dir file(文件的下載)

  •  示例:把177主機下/mnt/file1文件上傳到 172.25.254.97主機的/root/Desktop/目錄下:  

   [root@localhost mnt]# ls niu/
   file1  file2  file3  file4  file5
   [root@localhost mnt]# scp niu/file1 root@172.25.254.97:/root/Desktop/
   file1                                         100%    0     0.0KB/s   00:00

   此時可以在97主機下的桌面上看到file1: 

   [root@localhost ~]# cd /root/Desktop/
   [root@localhost Desktop]# ls
   file1
  •    示例:把97主機桌面下的file文件下載到177主機的/mnt/目錄下:
   [root@localhost ~]# scp root@172.25.254.97:/root/Desktop/file /mnt/
    file                                          100%    0     0.0KB/s   00:00

   此時可以在177主機上/mnt/目錄下可以看到file文件

   [root@localhost ~]# ls /mnt/
   file  niu  root@172.25.254.97

2.sshd 的key認證

【1】生成認證KEY

  生成密鑰的命令:ssh-keygen

[root@localhost ~]# rm -rf .ssh/
[root@localhost ~]# ls -a
.                .bash_logout   .config    Downloads      Music     Templates
..               .bash_profile  .cshrc     .esd_auth      Pictures  Videos
anaconda-ks.cfg  .bashrc        Desktop    .ICEauthority  Public    .viminfo
.bash_history    .cache         Documents  .local         .tcshrc
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
8c:23:ee:39:11:6b:e6:af:a3:76:b1:00:a5:6e:d1:d3 root@localhost
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|  .              |
| o. .            |
|o. o.E o         |
|... ooo S        |
| o..*. .         |
|.  =.+           |
|  ..*.           |
| ..o+=.          |
+-----------------+

 

【2】加密服務

使用命令:ssh-copy-id -i /root/.ssh/id_rsa.pub  root@id

[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub
[root@localhost .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.97
The authenticity of host '172.25.254.97 (172.25.254.97)' can't be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
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: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.25.254.97's password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'root@172.25.254.97'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts

(此時authorized_keys文件,生成代表97主機加密成功;id-rsa為鑰匙,id_rsa.pub為鎖) 

【3】分發鑰匙

使用命令: scp /root/.ssh/id_rsa root@id:/root/.ssh/

[root@localhost .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@localhost .ssh]# scp id_rsa root@172.25.254.177:/root/.ssh/
The authenticity of host '172.25.254.177 (172.25.254.177)' can't be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.177' (ECDSA) to the list of known hosts.
root@172.25.254.177's password:
id_rsa                                        100% 1679     1.6KB/s   00:00 

**在177主機下進行驗證:

[root@localhost ~]# ls .ssh/
id_rsa  known_hosts

【4】測試
在客戶主機中(172.25.254.177)輸入命令:ssh root@172.25.254.97

[root@localhost ~]# ssh root@172.25.254.97
Last login: Wed Jul 25 23:10:43 2018
此時不需要進行root用戶的登陸,直接連接成功   

3.sshd的安全設定
      PasswordAuthentication yes|no ##是否允許用戶通過登陸系統的密碼做sshd的認證,(在78行也可登錄其他用戶密碼)
      PermitRootLogin yes|no ##是否允許root用戶通過sshd服務的認證(48行)
      Allowusers student westos ##設定用戶白名單,白名單出現默認不再名單中的用戶不能使用sshd
      Denyusers westos ##設定用戶黑名單,黑名單出現默認不再名單中的用戶可以使用sshd

      注意:在服務端修改文件的配置:vim /etc/ssh/sshd_config
           配置完成之后要重啟服務:systemctl restart sshd.service

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM