您好,登錄后才能下訂單哦!
這篇文章主要為大家詳細介紹了linux的遠程訪問及控制,文中示例代碼介紹的非常詳細,零基礎也能參考此文章,感興趣的小伙伴們可以參考一下。
linux運維管理的時候,一般都是通過遠程方式管理,當需要從一個工作站管理數以百計的服務器主機時,遠程維護的方式將更占優勢。
OpenSSH
#Port 22 //端口號
#AddressFamily any
#ListenAddress 0.0.0.0 //ipv4監聽地址
#ListenAddress :: //ipv6監聽地址
#LoginGraceTime 2m //會話時間
#PermitRootLogin yes //是否進制root登錄
#StrictModes yes //是否驗證訪問權限
#MaxAuthTries 6 //驗證次數6次
#MaxSessions 10 //訪問的最大鏈接數
#PubkeyAuthentication yes //是否驗證公鑰
[root@tast01 ~]# vim /etc/ssh/sshd_config //進入編輯服務器配置文件信息
Port 22 //開啟端口
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
:wq //保存退出
[root@tast01 ~]# systemctl restart sshd //重啟SSH服務
[root@tast02 ~]# ssh root@192.168.144.133 //使用SSH服務登錄tast01服務器
The authenticity of host '192.168.144.133 (192.168.144.133)' can't be established.
ECDSA key fingerprint is SHA256:B8IsZOFG7FbtVkIK+dMILmo0iA4OEIeVGY0GnnCbXhk.
ECDSA key fingerprint is MD5:c2:d8:09:17:de:6e:ec:07:06:1b:ac:b6:1e:bd:62:09.
Are you sure you want to continue connecting (yes/no)? yes //詢問是否建立會話
Warning: Permanently added '192.168.144.133' (ECDSA) to the list of known hosts.
root@192.168.144.133's password: //輸入密碼
Last login: Mon Sep 9 13:59:09 2019
[root@tast01 ~]# //成功登錄tast01
[root@tast01 ~]# exit //退出
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# //回到tast02端口
[root@tast01 ~]# vim /etc/ssh/sshd_config //進入編輯主配置文件
#LoginGraceTime 2m
PermitRootLogin no //開啟是否啟用禁用root登錄,更改yes為no,禁止root用戶登錄
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
:wq //保存退出
[root@tast01 ~]# systemctl restart sshd //重啟服務
[root@tast01 ~]# useradd siti //創建siti普通用戶
[root@tast01 ~]# passwd siti //設置用戶密碼
更改用戶 siti 的密碼 。
新的 密碼:
無效的密碼: 密碼少于 8 個字符
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
[root@tast01 ~]# id siti //查看新建用戶siti信息
uid=1001(siti) gid=1001(siti) 組=1001(siti)
[root@tast01 ~]# id sun //查看用戶sun信息
uid=1000(sun) gid=1000(sun) 組=1000(sun),10(wheel)
[root@tast02 ~]# ssh root@192.168.144.133 //使用SSH服務登錄tast01服務器root用戶
root@192.168.144.133's password: //輸入密碼登錄
Permission denied, please try again. //拒絕登錄root
root@192.168.144.133's password:
Permission denied, please try again.
root@192.168.144.133's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). //嘗試輸入密碼三次后彈出,拒絕登錄
[root@tast02 ~]# ssh siti@192.168.144.133 //使用SSH服務登錄siti用戶
siti@192.168.144.133's password:
[siti@tast01 ~]$ //成功登錄tast01服務器siti用戶
[siti@tast01 ~]$ su - root //再siti用戶下使用su切換root用戶
]密碼: //輸入密碼
上一次登錄:一 9月 9 15:16:00 CST 2019從 192.168.144.135pts/1 上
最后一次失敗的登錄:一 9月 9 15:33:03 CST 2019從 192.168.144.135ssh:notty 上
最有一次成功登錄后有 3 次失敗的登錄嘗試。
[root@tast01 ~]# //成功登錄root用戶。
[root@tast01 ~]# exit //退出
登出
[siti@tast01 ~]$ exit //退出
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# //回到tast02用戶
[root@tast01 ~]# vim /etc/pam.d/su //進入編輯pam配置文件
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
auth required pam_wheel.so use_uid //開啟pam認證
auth substack system-auth
auth include postlogin
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
account include system-auth
password include system-auth
session include system-auth
session include postlogin
session optional pam_xauth.so
~
~
~
:wq //保存退出
[root@tast02 ~]# ssh siti@192.168.144.133 //登錄siti用戶
siti@192.168.144.133's password: //輸入密碼
Last failed login: Mon Sep 9 16:09:32 CST 2019 from 192.168.144.135 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Sep 9 15:47:20 2019 from 192.168.144.135
[siti@tast01 ~]$ su - root //登錄siti用戶,并切換root用戶
密碼: //輸入密碼
su: 拒絕權限 //權限拒絕,無法切換
[siti@tast01 ~]$
[siti@tast01 ~]$ su - sun //切換sun用戶
密碼: //輸入密碼
su: 拒絕權限 //權限拒絕,無法切換
[siti@tast01 ~]$
[root@tast01 ~]# vim /etc/ssh/sshd_config //進入服務器配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6 //開啟密碼驗證次數
#MaxSessions 10
:wq //保存退出
[root@tast02 ~]# ssh sun@192.168.144.133 //登錄sun用戶
sun@192.168.144.133's password: //輸入錯誤密碼
Permission denied, please try again. //1次輸錯,拒絕登錄
sun@192.168.144.133's password: //輸入錯誤密碼
Permission denied, please try again. //2次輸錯,拒絕登錄
sun@192.168.144.133's password: //輸入錯誤密碼
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). //3次輸入錯誤直接登出
[root@tast02 ~]# ssh -o NumberofPasswordPrompts=8 sun@192.168.144.133 //使用命令提高密碼輸入次數
sun@192.168.144.133's password:
Permission denied, please try again.
sun@192.168.144.133's password:
Permission denied, please try again.
sun@192.168.144.133's password:
Permission denied, please try again.
sun@192.168.144.133's password:
Permission denied, please try again.
sun@192.168.144.133's password:
Permission denied, please try again.
sun@192.168.144.133's password:
Received disconnect from 192.168.144.133 port 22:2: Too many authentication failures
Authentication failed. //輸入密碼6次后彈出,設設置生效
[root@tast01 ~]# vim /etc/ssh/sshd_config //進入編輯ssh服務端配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
AllowUsers sun@192.168.144.135 stii //在此處添加條目,僅允許IP地址為192.168.144.135客戶機登錄sun用戶
僅允許客戶端登錄stii用戶
#PubkeyAuthentication yes
:wq //保存退出
[root@tast01 ~]# useradd stii //添加stii用戶
[root@tast01 ~]# passwd stii //設置stii用戶密碼
更改用戶 stii 的密碼 。
新的 密碼:
無效的密碼: 密碼少于 8 個字符
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
[root@tast01 ~]# systemctl restart sshd //重啟ssh服務
[root@tast02 ~]# ssh sun@192.168.144.133 //在tast02客戶端中登錄服務器sun用戶
sun@192.168.144.133's password: //輸入密碼
Last failed login: Mon Sep 9 17:24:32 CST 2019 from 192.168.144.135 on ssh:notty
There were 6 failed login attempts since the last successful login.
Last login: Mon Sep 9 17:21:47 2019 from 192.168.144.133
[sun@tast01 ~]$ //成功登錄
[sun@tast01 ~]$ exit //退出用戶
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# ssh siti@192.168.144.133 //使用ssh登錄服務器siti用戶
siti@192.168.144.133's password: //輸入密碼
Permission denied, please try again. //拒絕登錄
[root@tast02 ~]# ssh stii@192.168.144.133 //登錄stii用戶
stii@192.168.144.133's password: //輸入密碼
[stii@tast01 ~]$ //成功登錄
[root@tast03 ~]# ssh sun@192.168.144.133 //tast03客戶機使用ssh服務登錄服務器sun用戶
The authenticity of host '192.168.144.133 (192.168.144.133)' can't be established.
ECDSA key fingerprint is SHA256:B8IsZOFG7FbtVkIK+dMILmo0iA4OEIeVGY0GnnCbXhk.
ECDSA key fingerprint is MD5:c2:d8:09:17:de:6e:ec:07:06:1b:ac:b6:1e:bd:62:09.
Are you sure you want to continue connecting (yes/no)? yes //詢問是否建立會話,輸入yes確定建立會話
Warning: Permanently added '192.168.144.133' (ECDSA) to the list of known hosts.
sun@192.168.144.133's password: //輸入密碼
Permission denied, please try again. //拒絕登錄
[root@tast03 ~]# ssh siti@192.168.144.133 //tast03客戶機使用ssh服務登錄服務器siti用戶
siti@192.168.144.133's password: //輸入密碼
Permission denied, please try again. //拒絕登錄
[root@tast03 ~]# ssh stii@192.168.144.133 //tast03客戶機使用ssh服務登錄服務器stii用戶
stii@192.168.144.133's password: //輸入密碼
Last login: Mon Sep 9 21:55:49 2019 from 192.168.144.135
[stii@tast01 ~]$ //成功登錄
[root@tast01 ~]# vim /etc/ssh/sshd_config //編輯ssh服務器配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
DenyUsers sun@192.168.144.135 stii //刪除僅允許條目,添加拒絕條目
#PubkeyAuthentication yes
:wq //保存退出
[root@tast01 ~]# systemctl restart sshd //重啟ssh服務
[root@tast02 ~]# ssh sun@192.168.144.133 //在tast02客戶端中登錄服務器sun用戶
sun@192.168.144.133's password: //輸入密碼
Permission denied, please try again. //拒絕登錄
[root@tast02 ~]# ssh stii@192.168.144.133 //在tast02客戶端中登錄服務器stii用戶
stii@192.168.144.133's password: //輸入密碼
Permission denied, please try again. //拒絕登錄
[root@tast02 ~]# ssh siti@192.168.144.133 //在tast02客戶端中登錄服務器siti用戶
siti@192.168.144.133's password: //輸入密碼
Last failed login: Mon Sep 9 22:02:00 CST 2019 from 192.168.144.132 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Mon Sep 9 21:53:53 2019 from 192.168.144.135
[siti@tast01 ~]$ //成功登錄
[root@tast03 ~]# ssh stii@192.168.144.133 //tast03客戶機使用ssh服務登錄服務器stii用戶
stii@192.168.144.133's password: //輸入密碼
Permission denied, please try again. //拒絕登錄
[root@tast03 ~]# ssh sun@192.168.144.133 //tast03客戶機使用ssh服務登錄服務器sun用戶
sun@192.168.144.133's password: //輸入密碼
Last failed login: Mon Sep 9 22:30:55 CST 2019 from 192.168.144.135 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Sep 9 22:24:51 2019 from 192.168.144.133
[sun@tast01 ~]$ //成功登錄
[root@tast03 ~]# ssh siti@192.168.144.133 //tast03客戶機使用ssh服務登錄服務器siti用戶
siti@192.168.144.133's password: //輸入密碼
Last login: Mon Sep 9 22:32:16 2019 from 192.168.144.135
[siti@tast01 ~]$ //成功登錄
[root@tast01 ~]# vim /etc/ssh/sshd_config //編輯ssh配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
DenyUsers sun@192.168.144.135 stii
PubkeyAuthentication yes //開啟密鑰對驗證功能
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys //密鑰存放位置
:wq //保存退出
[root@tast02 ~]# useradd siaa //在tast02客戶機中創建用戶
[root@tast02 ~]# passwd siaa //設置用戶目錄
更改用戶 siaa 的密碼 。
新的 密碼:
無效的密碼: 密碼少于 8 個字符
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
[root@tast02 ~]# su - siaa //切換至用戶siaa
[siaa@tast02 ~]$ ssh-keygen -t ecdsa //制作ecdsa類型密鑰
Generating public/private ecdsa key pair.
Enter file in which to save the key (/home/siaa/.ssh/id_ecdsa): //密鑰存放位置,保持不變,直接回車
Created directory '/home/siaa/.ssh'.
Enter passphrase (empty for no passphrase): //輸入要設置的密碼
Enter same passphrase again: //再次輸入密碼
Your identification has been saved in /home/siaa/.ssh/id_ecdsa.
Your public key has been saved in /home/siaa/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:5mTvLU19q7uUUXECnEmNldB3S4gUiNZdvm1zupFUf0Y siaa@tast02
The key's randomart image is:
+---[ECDSA 256]---+
| o +=B@+o.|
| o o o*.+o=|
| . ..oE|
| ++.| //生成ecdsa密鑰
| S +.+=|
| = . ..=+=|
| . .o o+..|
| ...o + |
| ...+= |
+----[SHA256]-----+
[siaa@tast02 ~]$ ls -a //查看用戶家目錄隱藏文件
. .. .bash_logout .bash_profile .bashrc .cache .config .mozilla .ssh
[siaa@tast02 ~]$ cd .ssh //進入生成的.ssh目錄
[siaa@tast02 .ssh]$ ls //查看目錄內容
id_ecdsa id_ecdsa.pub //生成的私鑰與公鑰文件
[siaa@tast02 .ssh]$ ssh-copy-id -i id_ecdsa.pub siti@192.168.144.133 //指定生成的公鑰文件推送到服務器siti用戶
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_ecdsa.pub"
The authenticity of host '192.168.144.133 (192.168.144.133)' can't be established.
ECDSA key fingerprint is SHA256:B8IsZOFG7FbtVkIK+dMILmo0iA4OEIeVGY0GnnCbXhk.
ECDSA key fingerprint is MD5:c2:d8:09:17:de:6e:ec:07:06:1b:ac:b6:1e:bd:62:09.
Are you sure you want to continue connecting (yes/no)? yes //詢問是推送,輸入yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
siti@192.168.144.133's password: //輸入服務器siti用戶密碼
Number of key(s) added: 1 //成功添加文件
Now try logging into the machine, with: "ssh 'siti@192.168.144.133'"
and check to make sure that only the key(s) you wanted were added.
[siaa@tast02 .ssh]$ ls //查看目錄信息
id_ecdsa id_ecdsa.pub known_hosts //創建文件Known_hosts
[siaa@tast02 .ssh]$ vim known_hosts //查看文件信息
192.168.144.133 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC6sBj5BEqQkEIXTdcRDCzDlQRfhaoaY7OvyWzxcNxt+n6ZjbA1PSYK2SeTW3MAhUZOry7T6gNDFL7YyfMfXOGo= //成功將ecdsa生成的密鑰推送給服務器
[root@tast01 ~]# cd /home/siti //進入siti家目錄
[root@tast01 siti]# ls -a //查看隱藏文件
. .bash_history .bash_profile .cache .mozilla
.. .bash_logout .bashrc .config .ssh
[root@tast01 siti]# cd .ssh //進入添加的.ssh目錄
[root@tast01 .ssh]# ls //查看信息
authorized_keys
[root@tast01 .ssh]# cat authorized_keys //查看信息內容
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBD6B4elJHibp7lYDfogSfd7krTUPyKzvLHZNk75GTm1oibrA0aMirgtwxxfUEOi+9+ZGU2V0C3+zH6vQpjvvPoo= siaa@tast02 //siaa@tast02的ecdsa加密文件
[siaa@tast02 .ssh]$ whoami //使用命令查看當前登錄用戶
siaa //確定當前登錄用戶為siaa
[siaa@tast02 .ssh]$ ssh siti@192.168.144.133 //使用ssh服務登錄服務器siti用戶
Enter passphrase for key '/home/siaa/.ssh/id_ecdsa': //輸入設置的ecdsa密碼
Last login: Mon Sep 9 22:37:19 2019 from 192.168.144.132
[siti@tast01 ~]$ //成功登錄服務器siti用戶
[siti@tast01 ~]$ exit //退出當前用戶
登出
Connection to 192.168.144.133 closed.
[siaa@tast02 .ssh]$ ssh-agent bash //回到tast02中siaa用戶,使用命令代理bash環境
[siaa@tast02 .ssh]$ ssh-add //使用命令添加驗證密碼
Enter passphrase for /home/siaa/.ssh/id_ecdsa: //輸入驗證密碼
Identity added: /home/siaa/.ssh/id_ecdsa (/home/siaa/.ssh/id_ecdsa) //成功添加密碼
[siaa@tast02 .ssh]$ ssh siti@192.168.144.133 //登錄服務器siti用戶
Last login: Mon Sep 9 23:31:28 2019 from 192.168.144.135
[siti@tast01 ~]$ //成功登錄,免密碼驗證
[root@tast01 ~]# vim /etc/ssh/sshd_config
...//省略部分內容...
# Authentication:
#LoginGraceTime 2m
PermitRootLogin yes //開啟登錄root用戶權限
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
:wq //保存退出
[root@tast01 ~]# systemctl restart sshd //重啟SSH服務
[root@tast02 ~]# ssh root@192.168.144.133 //使用ssh服務登錄服務器root用戶
root@192.168.144.133's password: //輸入用戶密碼
Last login: Wed Sep 11 22:56:28 2019 from 192.168.144.135
[root@tast01 ~]# //成功登錄
[root@tast01 ~]# exit //退出
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# cd /opt/ //進入opt目錄
[root@tast02 opt]# ls //查看
rh
[root@tast02 opt]# echo "this is ssh-client" > ssh_client.txt //創建.txt文件
[root@tast02 opt]# mkdir -p tast/si11 //遞歸創建tast目錄并在tast目錄下創建si11目錄
[root@tast02 opt]# ls //查看
rh ssh_client.txt tast //成功創建文件與目錄
[root@tast02 opt]# scp ssh_client.txt root@192.168.144.133:/home/ //將創建的.txt文件推送到服務器root用戶home目錄下
root@192.168.144.133's password: //輸入密碼
ssh_client.txt 100% 19 6.0KB/s 00:00 //成功推送
[root@tast01 ~]# ls /home/ //查看home目錄下文件
ssh_client.txt sun //成功添加文件
[root@tast01 ~]# cat /home/ssh_client.txt //查看文件內容
this is ssh-client //顯示文件內容
[root@tast02 opt]# scp -r tast/ root@192.168.144.133:/home/ //推送文件夾
root@192.168.144.133's password: //輸入密碼
[root@tast02 opt]# //推送成功
[root@tast01 ~]# ls /home/ //查看home目錄
ssh_client.txt sun tast //顯示推送的文件夾
[root@tast01 ~]# ls /home/tast/ //查看文件夾內容
si11 //顯示創建的si11目錄
[root@tast02 opt]# ls //查看信息
rh ssh_client.txt tast //顯示內容
[root@tast02 opt]# rm -rf ssh_client.txt //刪除txt文件
[root@tast02 opt]# rm -rf tast/ //刪除文件夾
[root@tast02 opt]# ls //查看
rh //成功刪除
[root@tast02 opt]# sftp root@192.168.144.133 //使用sftp命令登錄tast01服務器root用戶
root@192.168.144.133's password: //輸入密碼
Connected to 192.168.144.133.
sftp> ls //成功登錄并查看目錄信息
anaconda-ks.cfg initial-setup-ks.cfg 下載 公共
圖片 文檔 桌面 模板 //此時在root用戶家目錄下
視頻 音樂
sftp> cd /home/ //進入home目錄
sftp> ls //查看
ssh_client.txt sun tast //顯示內容
sftp> get ssh_client.txt //使用get命令下載txt文件
Fetching /home/ssh_client.txt to ssh_client.txt
/home/ssh_client.txt 100% 19 19.3KB/s 00:00
sftp> bye //退出
[root@tast02 opt]# ls //查看目錄下是否有內容
rh ssh_client.txt //成功下載
[root@tast02 opt]# mv ssh_client.txt ssh_server.txt //更改文件名稱
[root@tast02 opt]# ls //查看
rh ssh_server.txt //已更改
[root@tast02 opt]# sftp root@192.168.144.133 //使用sftp命令登錄tast01root用戶
root@192.168.144.133's password: //輸入密碼
Connected to 192.168.144.133.
sftp> cd /home/ //進入home目錄
sftp> ls //查看內容
ssh_client.txt sun tast
sftp> put ssh_server.txt //將文件上傳至tast01服務器home目錄中
Uploading ssh_server.txt to /home/ssh_server.txt
ssh_server.txt 100% 19 15.6KB/s 00:00
sftp> bye //退出
[root@tast02 opt]#
[root@tast01 ~]# ls /home/ //查看home目錄內容
ssh_client.txt ssh_server.txt sun tast //成功上傳文件
關于linux的遠程訪問及控制就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果喜歡這篇文章,不如把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。