1. 程式人生 > >ssh服務及服務管理命令

ssh服務及服務管理命令

控制服務

什麼是服務

類似於windows下的應用,讓使用者獲得更好的體驗

用什麼控制服務

系統初始化程序可以對服務進行相應的控制

當前系統初始化程序是什麼

systemd		##系統初始化程序
pstree		##顯示系統中的程序數

程序控制命令

ssh------->shhd 客戶端 服務端 cs client 客戶 server 企業

systemctl			##服務控制命令
systemctl	status	sshd	##檢視服務狀態,inactive(不可用),cative(可用)
systemctl	start	sshd	##開啟服務
systemctl	stop	sshd	##關閉服務
systemctl	restart	sshd	##重啟服務
systemctl	reload	sshd	##重新載入服務配置
systemctl	enable	sshd	##設定服務開機啟動
systemctl	disable	sshd	##設定服務開機不啟動
systemctl	list-units	##列出已經開啟服務當前狀態
systemctl	list-unit-files	##列出所有服務開機啟動的狀態 disable,enable,static
systemctl	list-dependencies	##列出服務的依賴
systemctl	set-default multi-user.target	##設定系統啟動級別為多使用者模式(無圖形)
systemctl	set-defaault graphical.target	##設定系統啟動級別為圖形模式

sshd服務

sshd簡介

sshd= secure shell 可以通過網路在主機中開機shell的服務

客戶端軟體 sshd

連線方式:
ssh    [email protected]	##文字模式的連結
ssh -X [email protected]	##可以在連結成功後開機圖形

注意:
	第一次連結陌生主機是要建立認證檔案
	所以會詢問是否建立,需要樹入yes
	在次連結此臺主機時,因為已經生成~/.ssh/know_hosts檔案所以不需要再次輸入yes

遠端複製: scp file [email protected]:dir ##上傳 scp [email protected]

:file dir ##下載

sshd 的key認證

生成認證KEY

[[email protected] ~]# ssh-keygen ##生成金鑰的命令 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): ##指定儲存加密字元的檔案(使用預設) 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: 86:61:e4:f1:6e:51:3a:4b:d7:3c:1b:2f:e8:3f:b0:5d

[email protected] The key’s randomart image is: ±-[ RSA 2048]----+ | o . | | o o o o | | + * . = | | . = = . = | | . S . o . | | o … .E | | .+ . | | …o | | … | ±----------------+

加密服務

[[email protected] .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] ##加密sshd服務 The authenticity of host ‘172.25.254.200 (172.25.254.200)’ 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 [email protected]’s password:

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘[email protected]’” and check to make sure that only the key(s) you wanted were added.

[[email protected] .ssh]# ls authorized_keys id_rsa id_rsa.pub known_hosts ^ 此檔案出現表示加密完成

分發鑰匙

scp /root/.ssh/id_rsa [email protected]:/root/.ssh/

測試

在客戶主機中(172.25.254.100) ssh [email protected] ##連線時發現直接登陸不需要root登陸系統的密碼認證 在這裡插入圖片描述 在這裡插入圖片描述 在這裡插入圖片描述 在這裡插入圖片描述 在這裡插入圖片描述

sshd的安全設定

	vim /etc/ssh/sshd_config		##用vim修改sshd的安全設定
	systemctl restart sshd.service		##重新讀取設定過的資訊

78 PasswordAuthentication yes|no ##是否允許使用者通過登陸系統的密碼做sshd的認證
48 PermitRootLogin yes|no	 ##是否允許root使用者通過sshd服務的認證
52 Allowusers student westos	 ##設定使用者白名單,白名單出現預設不再名單中的使用者不能使用sshd
53 Denyusers	westos		 ##設定使用者黑名單,黑名單出現預設不再名單中的使用者可以使用sshd

新增sshd登陸登陸資訊

vim /etc/motd		##檔案內容就是登陸後顯示的資訊

使用者的登陸審計

1.w		##檢視正在使用當前系統的使用者
	-f	##檢視使用來源
	-i	##顯示IP
	/var/run/utmp	
2.last		##檢視使用過並退出的使用者資訊
	/var/log/wtmp
3.lastb		##試圖登陸但沒成功的使用者
	/var/log/btmp