該操做在新系統部署前設置完畢。。。。shell
1、設置普通用戶ssh遠程端口登陸(好比xshell)
vim
假設該用戶爲rms,具體操做以下windows
#yum install openssh openssh-devel -y安全
# systemctl start sshd.service服務器
# systemctl enable sshd.service
dom
一、編輯/etc/ssh/sshd_config這個文件ssh
1.一、修改默認訪問端口tcp
#Port 22ide
Port 2200測試
# sed -i '/^#Port/s/#//g' /etc/ssh/sshd_config
# sed -i '/^Port/s/22/2200/g' /etc/ssh/sshd_config
# grep -i '^Port' /etc/ssh/sshd_config
1.二、設置容許登陸ssh服務器的用戶
添加遠程用戶
# useradd rms
# echo "wXEN**********" | passwd --stdin rms
#vim /etc/ssh/sshd_config
AllowUsers rms
或
# echo "AllowUsers rms" >> /etc/ssh/sshd_config
查看結果
# grep -i 'AllowUsers' /etc/ssh/sshd_config
1.三、禁止root登陸
#PermitRootLogin yes
PermitRootLogin no
# sed -i '/^#PermitRootLogin/s/#//g' /etc/ssh/sshd_config
# sed -i '/^PermitRootLogin/s/yes/no/g' /etc/ssh/sshd_config
# grep -i 'PermitRootLogin' /etc/ssh/sshd_config
1.四、修改最大登陸嘗試次數(可選)
#MaxAuthTries 6
1.五、防火牆放行
# firewall-cmd --permanent --add-port=2200/tcp --zone=public
# firewall-cmd --permanent --add-port=2200/udp --zone=public
success
# firewall-cmd --reload
success
1.六、重啓sshd服務,liunx遠程鏈接測試
# systemctl restart sshd.service
# ssh -p 2200 rms@ip地址
2、ssh祕鑰認證方式(公鑰與私鑰)
一、禁用密碼驗證,開啓祕鑰認真
1.一、無id_rsa密碼配置:
普通用戶或管理員用戶生成ssh的祕鑰:以普通用戶rms爲例
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/rms/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/rms/.ssh/id_rsa.
Your public key has been saved in /home/rms/.ssh/id_rsa.pub.
The key fingerprint is:
8b:c2:97:52:b8:e0:97:cc:84:54:ef:2e:5c:6b:1b:bb rms@ecloud
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
| . |
| . . |
| . . |
| . . o |
| o o + S |
| . B * + . |
| . % O . |
| . * + |
| E. |
+-----------------+
# cp /home/rms/.ssh/id_rsa /home/rms/
# chmod +755 /home/rms/id_rsa
客戶端下載並備份後,便可刪除。。。(用於相似於xshell添加祕鑰遠程鏈接登陸)
# rm -rf /home/rms/id_rsa
導入認證文件authorized_keys:
# cat /home/rms/.ssh/id_rsa.pub >>/home/rmsxcp/.ssh/authorized_keys
# cat /home/rms/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPKpeVAQGyExheRIagOF469Z14ZAHD0MJx/KV3Np31Pergi+dGGPYE8vR3S6myWD9SseAa0Umq0QbQNFW9kBhliI61q5iwGj0iwKe25XI4+tKez7ajToTYccHakqiCIfhdQPsBdePY+8gshJETbECxsNac0SAjHlqT2hfsaYcN2mu2PdBGmS8/1Ldi/oY889LLMY69bwnXwSpaBZHbGyABKPyGp/LNLjNYRQx+H9e3Iiswyb7kXdSQCzclCimH50Xe3omDZhQla3LUGUPrK8daNOuN rms@ecloud
sshd配置文件設置
vim /etc/ssh/sshd_config
修改下面幾處:
RSAAuthentication yes
PubkeyAuthentication yes #啓用PublicKey認證。
AuthorizedKeysFile .ssh/authorized_keys #PublicKey文件路徑。
PasswordAuthentication no #不適用密碼認證登陸。
1.二、、有id_rsa密碼配置:
普通用戶或管理員用戶生成ssh的祕鑰:
$ ssh-keygen -t rsa -N efWRfB
Generating public/private rsa key pair.
Enter file in which to save the key (/home/rms/.ssh/id_rsa): /home/rms/.ssh/rkey
Created directory '/home/rms/.ssh'.
Your identification has been saved in /home/rms/.ssh/rmskey.
Your public key has been saved in /home/rms/.ssh/rmskey.pub.
The key fingerprint is:
8b:35:68:2e:6b:e3:21:c8:3b:73:1f:17:1a:82:d6:84 rms@ecloud
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
| |
| . |
| E . |
| + . |
| o o .o.S |
|o. .ooo.o |
|... ooo.. |
| o.oo+o |
| .+o+o |
+-----------------+
追加祕鑰到authorized_keys文件裏面(注意是雙向右的方向符哦>>)
$ cat /home/rms/.ssh/rkey.pub >>/home/rms/.ssh/authorized_keys
$ ls /home/rms/.ssh/
authorized_keys rkey rkey.pub
備份公私祕鑰:
# mv /home/rms/.ssh/rmskey* /root/.ssh/
# cat /home/rms/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPKpeVAQGyExheRIagOF469Z14ZAHD0MJx/KV3Np31Pergi+dGGPYE8vR3S6myWD9SseAa0Umq0QbQNFW9kBhliI61q5iwGj0iwKe25XI4+tKez7ajToTYccHakqiCIfhdQPsBdePY+8gshJETbECxsNac0SAjHlqT2hfsaYcN2mu2PdBGmS8/1Ldi/oY889LLMY69bwnXwSpaBZHbGyABKPyGp/LNLjNYRQx+H9e3Iiswyb7kXdSQCzclC rms@ecloud
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDTQPMKsc9Ika4aIq1yONvT/6GNMiOe2qpPmwCmqPrelmQCiEFJbvC5lbTts0JtK8u+4EkFSi7NJg8qotGGZmpD7USm7aS50jsIgcmtr8DUuTXdQcNmkEyQnEiC5NR7pDyYxPa2IQEPJI8+31uSHMtaz1swa9h3A/FTjY8hlmHL6lWkRvdazsClpxc0UvQzHJ6PYxzWm2tComACcifUDP95PNkJzsfEcyNRBESHedZ7sbXHKoqeKDF2d9pOyT82DZHXet rms@ecloud
vim /etc/ssh/sshd_config
修改下面幾處:
RSAAuthentication yes
PubkeyAuthentication yes #啓用PublicKey認證。
AuthorizedKeysFile .ssh/authorized_keys #PublicKey文件路徑。
PasswordAuthentication no #不適用密碼認證登陸。
命令操做以下
# sed -i '/^#RSAAuthentication/s/#//g' /etc/ssh/sshd_config
# grep -i 'RSAAuthentication' /etc/ssh/sshd_config
RSAAuthentication yes
# sed -i '/^#PubkeyAuthentication/s/#//g' /etc/ssh/sshd_config
# grep -i 'PubkeyAuthentication' /etc/ssh/sshd_config
PubkeyAuthentication yes
# grep -i 'AuthorizedKeysFile' /etc/ssh/sshd_config
AuthorizedKeysFile .ssh/authorized_keys #默認
# PAM authentication, then enable this but set PasswordAuthentication
# sed -i '/^PasswordAuthentication/s/yes/no/g' /etc/ssh/sshd_config
# grep -i '^PasswordAuthentication' /etc/ssh/sshd_config
PasswordAuthentication no
# systemctl restart sshd
1步驟配置完成後,進行下面2步驟的操做
下載Linux生成的私鑰,導入相應的ssh客戶端。
二、、windows使用xshell、xftp的操做
導入id_rsa:(有密碼就填密碼,空密碼保留空便可)
注意:一點要保存好id_rsa文件,防止下次的從新導入,同時,也要保存建立祕鑰時候的密碼。。。。。
xftp的配置同理:
經過密碼能夠正常登錄後,咱們須要保留私有密碼到安全的地方,並在服務器端刪除,防止別人拷貝並利用,以及下次ssh從新導入。(必定要備份後,再刪除)
# ll /home/rms/.ssh/
總用量 20
-rw-r--r--. 1 root root 790 3月 17 22:24 authorized_keys
-rw-------. 1 rms rms 1675 3月 17 21:21 id_rsa
-rw-r--r--. 1 rms rms 395 3月 17 21:21 id_rsa.pub
-rw-------. 1 rms rms 1766 3月 17 21:59 keyblj
-rw-r--r--. 1 rms rms 395 3月 17 21:59 keyblj.pub
# rm -rf /home/rms/.ssh/id_rsa
# rm -rf /home/rms/.ssh/keyblj
# ll /home/rms/.ssh/
總用量 12
-rw-r--r--. 1 root root 790 3月 17 22:24 authorized_keys
-rw-r--r--. 1 rms rms 395 3月 17 21:21 id_rsa.pub
-rw-r--r--. 1 rms rms 395 3月 17 21:59 keyblj.pub
(4)注意:若是你是經過普通用戶登錄的話,authorized_keys文件必須具有讀權限,即644,不然,ssh遠程登錄不了的;若是是root用的話,能夠修改成600權限,或400的權限。。
普通用戶:
# chmod 644 /home/rms/.ssh/authorized_keys
root用戶:
# chmod 600 /home/rms/.ssh/authorized_keys