1. 程式人生 > >linux --- 控制服務以及遠端連線

linux --- 控制服務以及遠端連線

控制服務

1.用什麼控制服務
	系統初始化程序可以對服務進行相應的控制
2.當前系統初始化程序是什麼
	systemd		---> 系統初始化程序
	pstree		---> 顯示系統中的程序樹
3.程序控制命令
	ssh 	    -------->   sshd
	client(客戶埠)	      server(服務埠)
4.systemctl -------->服務控制命令
	systemctl 	status	sshd	##檢視服務狀態,inactive(不可用),active(可用)
	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-default graphical.target	##設定系統啟動級別為圖形模式

遠端連線(需要密碼登陸)

ssh 遠端連結

ssh 對方使用者名稱@對方主機ip地址  ---> 遠端連結虛擬機器
1.nm-connection-editor ---> 圖形介面更改ip地址
2.ip 地址檢視 
	ip addr show  ----> 建議使用
	ifconfig 		---> 不建議使用
3.連線方式
	ssh	[email protected]	文字模式的連線
	ssh	-X [email protected]	可以在連線成功後開啟圖形
	cheese			 ---> 連線開啟對方電腦的攝像頭
注意:第一次連線陌生主機時要建立認證檔案;會詢問是否建立連線,需要輸入yes;下次不再需要
      再次連線此主機時,因為已經生成~/.ssh/know_hosts
4.遠端複製
	scp	file	
[email protected]
:dir #上傳(dir為絕對路徑) scp [email protected]:file dir #下載(file為絕對路徑) scp -rp [email protected]:dir dir #下載(dir為絕對路徑)

遠端連線(不需要密碼登陸)

ssh的key認證

#client (客戶埠)
[[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:
e4:94:45:27:62:b9:94:66:42:87:09:6f:2d:67:2d:b4 
[email protected]
The key's randomart image is: +--[ RSA 2048]----+ | .o.o=++ . | | .+=** o | | +=E.. | | . B.. | | S | | | | | | | | | +-----------------+ [[email protected] ~]# cd /root/.ssh/ [[email protected] .ssh]# ls ---> 檢視金鑰是否生成 id_rsa #(私鑰) id_rsa.pub #(公鑰) known_hosts [[email protected] .ssh]# ssh-copy-id -i id_rsa.pub [email protected] --->  將公鑰給主機ip為113的root使用者,加密root使用者 The authenticity of host '172.25.254.113 (172.25.254.113)' can't be established. ECDSA key fingerprint is 65:4d:ac:8a:c9:58:82:b5:0c:91:c4:ef:a5:e6:f6:65. 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 [[email protected] .ssh]# scp id_rsa [email protected]:/root/.ssh/ #分發鑰匙,將主機ip為113的私鑰分發給ip為213的主機 [email protected]'s password: id_rsa 100% 1675 1.6KB/s 00:00
#server(服務埠)
[[email protected] ~]# cd /root/.ssh/
[[email protected] .ssh]# \ls
id_rsa	known_hosts
[[email protected] .ssh]# ssh [email protected] #連線時發現直接登陸不需要輸入ip113 root密碼
Last login: Wed Oct  3 00:25:55 2018 from 172.25.254.13
[[email protected] ~]# logout
Connection to 172.25.254.113 closed.
#修改主機名稱更改
[[email protected] ~]$ hostnamectl set-hostname hao  #更改主機名稱為hao
[[email protected] ~]$ hostname		      #檢視主機名稱