linux修改ssh連接埠 以及禁止root遠程登入 (實驗機 CentOs)

來源:互聯網
上載者:User

標籤:

把ssh預設遠端連線連接埠修改為3333

1.編輯防火牆配置:

vi /etc/sysconfig/iptables

防火牆新增連接埠3333,方法如下:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3333 -j ACCEPT

具體是這樣做:

======================================================================# Firewall configuration written by system-config-firewall# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 3333 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibitedCOMMIT

之後,重啟防火牆,使配置生效:

/etc/init.d/iptables restart

或者

service iptables restart

 

防火牆策略都是寫在/etc/sysconfig/iptables檔案裡面的。若是沒有發現/etc/sysconfig/下有iptables檔案,解決方案:

1. 隨便寫一條iptables命令配置個防火牆規則。如:iptables -P OUTPUT ACCEPT。

cd /etc/sysconfig/iptables -P OUTPUT ACCEPT

2. service iptables save進行儲存

service iptables saveiptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]lscbq      htcacheclean  init             modules     network          quota_nld      samba      sendmail   sshdconsole  httpd         iptables         named       network-scripts  readonly-root  sandbox    snmpd      udevcrond    i18n          iptables-config  netconsole  networking       rsyslog        saslauthd  snmptrapd  xinetd

3. service iptables restart命令重啟:

/etc/init.d/iptables restartiptables: Flushing firewall rules:                         [  OK  ]iptables: Setting chains to policy ACCEPT: raw filter mangl[  OK  ]iptables: Unloading modules:                               [  OK  ]iptables: Applying firewall rules:                         [  OK  ]

或者

service iptables restartiptables: Flushing firewall rules:                         [  OK  ]iptables: Setting chains to policy ACCEPT: raw filter mangl[  OK  ]iptables: Unloading modules:                               [  OK  ]iptables: Applying firewall rules:                         [  OK  ]

 

2.備份ssh連接埠設定檔

cp /etc/ssh/sshd_config    /etc/ssh/sshd_config.bak

或者

cp /etc/ssh/ssh_config    /etc/ssh/ssh_config.bak

 修改ssh連接埠為3333

vi /etc/ssh/sshd_configPort 22Port 3333

把#去掉,在 Port 22 下方添加 Port 3333 。

重啟:

/etc/init.d/sshd restart

service sshd restart

用3333連接埠可以正常串連之後,再返回去重複上面的步驟。把22連接埠禁用了,以後ssh就只能用3333連接埠串連了!增強了系統的安全性。

 

3、禁止root通過ssh遠程登入

vi /etc/ssh/sshd_config

找到PermitRootLogin,將後面的yes改為no,把前面的注釋#取消,這樣root就不能遠程登入了!
可以用普通帳號登入進去,要用到root的時候使用命令su root 切換到root賬戶

 

4、限制使用者的SSH訪問

假設我們只要root,user1和user2使用者能通過SSH使用系統,向sshd_config設定檔中添加

vi /etc/ssh/sshd_configAllowUsers root user1  user2

 

5、配置空閑逾時退出時間間隔
使用者可以通過ssh登入到伺服器,你可以設定一個空閑逾時時間間隔。
開啟sshd_config設定檔,設定為如下。

vi /etc/ssh/sshd_configClientAliveInterval 600ClientAliveCountMax 0

上面的例子設定的空閑逾時時間間隔是600秒,即10分鐘,
過了這個時間後,空閑使用者將被自動踢出出去(可以理解為退出登入/登出)。

 

6、限制只有某一個IP才能遠程登入伺服器

vi /etc/hosts.deny     #在其中加入sshd:ALLvi /etc/hosts.allow    #在其中進行如下設定:sshd:192.168.1.1     #(只允許192.168.1.1這個IP遠程登入伺服器)

最後重啟ssh服務:

/etc/init.d/sshd restart

 

linux修改ssh連接埠 以及禁止root遠程登入 (實驗機 CentOs)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.