爲什麼需要遠程到Linux系統呢?遠程到Linux系統是在機房管理中最經常使用了,因爲Linux服務器一般都是在機房的機櫃中,可能會配有KVM作爲本地連接,但是日常維護服務器每修改一個參數都要跑到相應的服務器前使用KVM連接,一方面效率低,而且長時間在機房裏對自身的健康都有一定的影響。所以日常維護的時候就使用遠程的方式登錄linux系統啦。一般登錄的方式有Telnet,SSH,VNC等。
1.telnet服務
Telnet是網絡中遠程登錄服務的標準協議和主要方式。它爲用戶提供了在本地計算機上完成遠程主機工作的能力。在終端使用者的電腦上使用telnet程序,用它連接到服務器,就像直接在服務器的控制檯上輸入一樣,可以在本地就能控制服務器。雖然Telnet較爲簡單實用也很方便,但是在格外注重安全的現代網絡技術中,Telnet並不被重用。原因在於Telnet是一個明文傳送協議,它將用戶的所有內容,包括用戶名和密碼都明文在互聯網上傳送,具有一定的安全隱患,因此許多服務器都會選擇禁用Telnet服務。RHEL7上也是默認沒有安裝Telnet服務的,telnet對應的服務名稱是xinetd。
1.1安裝服務
安裝xinetd服務和telnet-server服務。
[root@shijie Desktop]# yum install xinetd telnet-server -y
以下省略...
1.2重啓服務
[root@shijie Desktop]# systemctl start telnet.socket
[root@shijie Desktop]# systemctl start xinetd
[root@shijie Desktop]# systemctl enable telnet.socket
ln -s '/usr/lib/systemd/system/telnet.socket' '/etc/systemd/system/sockets.target.wants/telnet.socket'
[root@shijie Desktop]# systemctl enable xinetd
1.3客戶端訪問
linux客戶端,安裝telnet客戶端,默認情況下telnet不能遠程root用戶,安全性考慮也不建議直接telnet到root下。
[root@shijie2 Desktop]# yum install telnet
[root@shijie2 Desktop]# telnet 192.168.10.10
Trying 192.168.10.10...
Connected to 192.168.10.10.
Escape character is '^]'.
Kernel 3.10.0-123.el7.x86_64 on an x86_64
shijie login: shijie
Password:
Last login: Tue May 26 13:14:48 from ::ffff:192.168.10.30
[shijie@shijie ~]$
windows客戶端,可以先安裝telnet客戶端,也可以使用SecureCRT,xshell,putty等工具。我使用SecureCRT爲例。
點擊快速連接
協議選擇telnet,輸入遠程的IP地址,端口號默認23。
點擊連接後輸入需要連接的非root用戶。
2.SSH
SSH 是較可靠,專爲遠程登錄會話和其他網絡服務提供安全性的協議。相對對telnet來說,SSH傳輸過程都是加密的,可以有效防止遠程管理過程中的信息泄露問題。RHEL7上SSH協議是默認開啓的,只要有客戶端就可以直接SSH遠程到服務器。
2.1客戶端遠程
linux客戶端上,使用ssh ip登錄可以直接登錄到服務器的root用戶,使用ssh 用戶名@ip可以登錄到指定用戶。
[root@shijie2 Desktop]# ssh 192.168.10.10
[email protected]'s password:
Last login: Wed May 27 07:11:43 2020 from 192.168.10.20
[root@shijie ~]#
windows客戶端SSH登錄使用SecureCRT爲例,選擇快速連接。
協議選擇SSH2,輸入需要遠程的IP,端口默認22,輸入需要遠程的用戶名。
輸入用戶的密碼
點擊確定之後就可以進入遠程的服務器了。
2.2SSH免密登錄
在客戶端上生成祕鑰對
[root@shijie2 Desktop]# 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:
9e:6a:24:a6:2d:a8:8e:e7:41:2a:ca:76:54:1e:9e:a5 root@shijie2
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| o . |
| . + = S |
| o .oE.. . |
|o.o+ o o |
|*.+o. .. |
|B=o. .. |
+-----------------+
將生成的祕鑰對複製到服務器上。
[root@shijie2 Desktop]# ssh-copy-id 192.168.10.10
/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 '192.168.10.10'"
and check to make sure that only the key(s) you wanted were added.
服務器上修改配置文件,將PasswordAuthentication參數改爲no。
[root@shijie ~]# vim /etc/ssh/sshd_config
以上省略...
76 #PasswordAuthentication yes
77 #PermitEmptyPasswords no
78 PasswordAuthentication no
以下省略...
[root@shijie ~]# systemctl restart sshd
再次嘗試在客戶端中登錄服務器,就不需要密碼登錄了。
[root@shijie2 Desktop]# ssh 192.168.10.10
Last login: Wed May 27 08:16:53 2020 from 192.168.10.20
[root@shijie ~]#
這樣修改其他客戶端就無法使用用戶名密碼登錄了,可以單獨將PasswordAuthentication參數前面的#號去掉,這樣既可以使用免密登錄,又可以使用用戶名密碼登錄了。
2.3SSH自定義安全設置
a.修改默認端口,使用ssh ip -p 端口號登錄。
13 # If you want to change the port on a SELinux system, you have to tell
14 # SELinux about this change.
15 # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
16 #
17 Port 2222
18 #AddressFamily any
b.限制客戶端登錄IP,這樣只有允許的IP才能登錄了。
[root@shijie ~]# vim /etc/hosts.allow
sshd:192.168.10.20,192.168.20.*:allow
sshd:all:deny
c.如果服務器存在多個ip,只允許從指定ip登錄,修改配置文件ListenAddress參數。
[root@shijie ~]# vim /etc/ssh/sshd_config
ListenAddress 192.168.10.10 默認0.0.0.0
3.VNCserver
VNC是遠程管理工具,可以在客戶端使用界面化遠程管理服務器。服務器安裝VNCserveer。
[root@shijie Desktop]# yum install vnc-server -y
編輯VNC配置文件,舊版本配置文件在/etc/sysconfig/vncservers。現在更換位置到 /lib/systemd/system/[email protected],要配置文件就先複製在@後面加上冒號數字代表幾號窗口,例vncserver@:2.service。將
部分修改成需要登錄的用戶名。
[root@shijie Desktop]# cd /lib/systemd/system/
[root@shijie system]# cp [email protected] vncserver@:2.service
[root@shijie system]# vim vncserver@:2.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
# ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
# PIDFile=/home/<USER>/.vnc/%H%i.pid
ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/home/root/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
設置VNC遠程連接的密碼。
[root@shijie system]# vncpasswd
Password:
Verify:
開啓VNC服務
[root@shijie system]# vncserver :2
客戶端訪問,先安裝vncviewer。
[root@shijie2 Desktop]# yum install -y vnc
[root@shijie2 Desktop]# vncviewer 192.168.10.10:2