1. 程式人生 > >CentOS 7設定ssh服務自動啟動

CentOS 7設定ssh服務自動啟動

實驗環境:CentOS7 Minimal安裝,安裝過程及軟體包見http://blog.csdn.net/capricorn90/article/details/52556174
SSH的英文全稱是Secure SHell。通過使用SSH,你可以把所有傳輸的資料進行加密,這樣“中間人”這種攻擊方式就不可能實現了,而且也能夠防止DNS和IP欺騙。還有一個額外的好處就是傳輸的資料是經過壓縮的,所以可以加快傳輸的速度。SSH有很多功能,它既可以代替telnet,又可以為ftp、pop、甚至ppp提供一個安全的“通道”。

SSH在Linux中的服務是sshd,安裝openssh後才可開啟。CentOS 7 安裝後預設情況下是不啟動sshd服務,即無法通過ssh服務遠端連線。
首先檢視系統是否安裝openssh,一般情況想都是預設安裝了,

[[email protected] ~]# rpm -qa | grep ssh
libssh2-1.4.3-10.el7.x86_64
openssh-server-6.6.1p1-22.el7.x86_64
openssh-clients-6.6.1p1-22.el7.x86_64
openssh-6.6.1p1-22.el7.x86_64

如果沒有安裝可以通過yum線上安裝。

[[email protected] ~]# yum install openssh

手動設定啟動ssh服務

簡單的設定就是在命令列中啟動sshd服務。這樣做比較快捷直接,但是隻能對當前狀態有效,一旦重啟系統就丟失了該服務。

[[email protected] ~]# systemctl start sshd
[[email protected] ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2016-09-16 16:18:24 CST; 6h ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 1031 (sshd)
CGroup: /system.slice/sshd.service
└─1031 /usr/sbin/sshd -D
Sep 16 16:18:24 localhost.localdomain systemd[1]: Starting OpenSSH server daemon…
Sep 16 16:18:25 localhost.localdomain sshd[1031]: Server listening on 0.0.0.0 port 22.
Sep 16 16:18:25 localhost.localdomain sshd[1031]: Server listening on :: port 22.
Sep 16 18:18:14 localhost.localdomain systemd[1]: Started OpenSSH server daemon.
Sep 16 18:29:41 localhost.localdomain sshd[11847]: Accepted password for root from 192.168.92.1 port 55149 ssh2
Sep 16 18:37:11 localhost sshd[12969]: Address 192.168.92.1 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Sep 16 18:37:12 localhost sshd[12969]: Accepted password for root from 192.168.92.1 port 55391 ssh2
Sep 16 22:09:59 localhost sshd[15252]: Address 192.168.92.1 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Sep 16 22:10:02 localhost sshd[15252]: Accepted password for root from 192.168.92.1 port 64452 ssh2
Sep 16 22:22:08 localhost systemd[1]: Started OpenSSH server daemon.

設定自動啟動ssh服務

1、systemclt設定自動啟動

通過systemctl命令可以將sshd服務加到開機自啟動列表裡。實現開機自動啟動sshd服務。

[[email protected] ~]# systemctl enable sshd

2、修改ssh監聽埠

在sshd_config檔案中存放了埠、控制策略等資訊。

[[email protected] ~]# vi /etc/ssh/sshd_config

#       $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# The default requires explicit activation of protocol 1
#Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Ciphers and keying
#RekeyLimit default none

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#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

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
UsePrivilegeSeparation sandbox          # Default for new installations.
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server

首先修改埠,埠設定為自定義埠,即1024之後的埠,這裡設定為8090。

port 8090

禁止空密碼使用者登入。

PermitEmptyPasswords no

開啟密碼登入授權(預設即開啟)

PasswordAuthentication yes

禁止root賬戶使用ssh登入,這種設定通常用於網際網路伺服器,防止提權後用root賬戶登入搞破壞。

PermitRootLogin no

注意其中關於port的提示文字

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER

修改埠的時候需要新增到防火牆的控制中,否則無法使用ssh連線。

[[email protected] ~]# semanage port -l | grep ssh #檢視當前ssh服務監聽的埠
ssh_port_t tcp 22
[[email protected] ~]# semanage port -a -t ssh_port_t -p tcp 8090 #增加監聽埠8090

[[email protected] ~]# semanage port -l | grep ssh
ssh_port_t tcp 8090,22

semanage只是埠工具,修改防火牆只能使用firewall-cmd

[[email protected] ssh]# yum provides firewall-cmd #查詢防火牆工具所在的包
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirrors.btte.net
* updates: mirrors.btte.net
firewalld-0.3.9-14.el7.noarch : A firewall daemon with D-BUS interface providing a dynamic firewall
Repo : base
Matched from:
Filename : /usr/bin/firewall-cmd

[[email protected] ssh]# yum -y install firewalld #安裝防火牆工具

[[email protected] ssh]# systemctl start firewalld #啟動防火牆服務

[[email protected] ssh]# systemctl status firewalld #檢視防火牆狀態
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2016-09-17 04:22:15 CST; 15s ago
Main PID: 16979 (firewalld)
CGroup: /system.slice/firewalld.service
└─16979 /usr/bin/python -Es /usr/sbin/firewalld –nofork –nopid
Sep 17 04:22:14 localhost systemd[1]: Starting firewalld - dynamic firewall daemon…
Sep 17 04:22:15 localhost systemd[1]: Started firewalld - dynamic firewall daemon.
[[email protected] ssh]# firewall-cmd –zone=public –add-port=8090/tcp –permanent #防火牆中允許8090埠通過
success

[[email protected] ssh]# semanage port -m -t ssh_port_t -p tcp 8090 #將ssh服務修改為8090埠
[[email protected] ssh]# firewall-cmd –zone=public –remove-port=22/tcp –permanent #刪除22埠
success
[[email protected] ssh]# firewall-cmd –reload #重新載入防火牆服務配置
success

相關推薦

CentOS 7設定ssh服務自動啟動

實驗環境:CentOS7 Minimal安裝,安裝過程及軟體包見http://blog.csdn.net/capricorn90/article/details/52556174 SSH的英文全稱是Secure SHell。通過使用SSH,你可以把所有傳輸的資

Linux(CentOS)下設定nginx開機自動啟動和chkconfig管理

Nginx 是一個很強大的高效能Web和反向代理伺服器。雖然使用命令列可以對nginx進行各種操作,比如啟動等,但是還是根據不太方便。下面介紹在linux下安裝後,如何設定開機自啟動。首先,在linux系統的/etc/init.d/目錄下建立nginx檔案,使用如下命令:vi

CentOS 7設定開機啟動服務,新增自定義系統服務

CentOS 7設定開機啟動服務 建立服務檔案 儲存目錄 設定開機自啟動 其他命令 1.建立服務檔案 檔案路徑 vim /usr/lib/systemd/system/nginx.service 服務檔案內容 1.nginx.servi

阿裏雲CentOS 7.2 MySQL服務啟動失敗的解決思路

阿裏雲 centos 7.2 mysql服務啟動失敗的解決思路阿裏雲 CentOS 7.2 MySQL服務啟動失敗的解決思路前言 :昨天剛剛搭建好的MySQL讓老大看了一下,經過測試已經完成任務。但是今天早晨來的時候發現服務器被關了,此時我的心情崩潰的,但是我非常冷靜的解決了MySQL問題。如下:啟動MySQ

nginx設定成開機自動啟動服務

1、nginx的安裝目錄 /usr/local/nginx 啟動nginx cd /usr/local/nginx/sbin./nginx 更改配置重啟nginxcd /usr/local/nginx/sbin./nginx -s reload 2、nginx設定成開機自動啟動服務 1)在/etc/

CentOS 7 設定 svn 開機啟動

出處 安裝好 svn 服務後,預設是沒有隨系統啟動自動啟動的, CentOS 7 的 /etc/rc.d/rc.local 是沒有執行許可權的, 系統建議建立 systemd service 啟動服務 於是檢視 systemd 裡 svn 的配置檔案 /lib/systemd/system/

CentOS 7下mysqld服務啟動失敗終極解決方案

啟動mysqld服務失敗,報錯如下: [[email protected] ~]# service mysqld start Starting mysqld (via systemctl): Job for mysqld.service failed because the con

Win10 wsl linux子系統ssh服務啟動設定

折騰了一晚上Win10 wsl linux子系統ssh服務自啟動設定,包括github和stackoverflow的很多帖子都已經過時,嘗試了數個vbs+bat指令碼,甚至powershell指令碼等等,都不能成功,最後還是知乎的一個帖子解決了,但是原帖也還是有些小問題。傳送門:WSL

CentOS 7下MySQL服務啟動失敗的解決思路

今天,啟動MySQL伺服器失敗,如下所示: [[email protected] ~]# /etc/init.d/mysqld start Starting mysqld (via systemctl): Job for mysqld.service fail

Centos 7 安裝、配置並啟動SSH遠端訪問

1.檢視SSH是否安裝:[[email protected] ~]# rpm -qa | grep ssh上圖說明了 centos 7 預設安裝了SSH包2.安裝缺失的包、並配置SSH:[[e

centos 7的/etc/rc.local啟動服務無法突破ulimit -n檔案數限制

linux很多系統優化配置問題,一定是運維過生產環境系統才能體會到問題的坑,因為生產環境才會有各種各樣的奇葩事情發生,比如如下的情況,ulimit -n檔案數明明設定了,可是在線上環境就是提示開啟檔案數過多。具體情況如下:[root@mcuhome]# ulimit  -n1

[CentOS 7系列]系統服務管理

cron chkconfig 1、任務計劃cron是一個linux下的定時執行工具(相當於windows下的scheduled task),可以在無需人工幹預的情況下定時地運行任務task。cron服務提供crontab命令來設定cron服務的。▎常用參數:命令作用crontab -u設定某個用戶的c

Centos 7設定默認運行級別

centos 7設定默認運行級別Centos 7設定默認運行級別Centos7 中不再是使用 /etc/inittab 來配置運行級別,而是使用 systemd 來進行管理[[email protected]/* */ ~]# ll /lib/systemd/system/runlevel3.ta

CentOS 6和CentOS 7管理系統服務的區別

service systemctl 管理系統服務CentOS 6CentOS 7(firewalld.service可簡寫成firewalldhttpd.service可簡寫成httpd)關閉防火墻# service iptables stop# systemctl stop firewalld.se

CentOS 7 安裝vsftpd 服務

reserve 安裝 warn 用戶名 local save list 希望 image 在CentOS7上安裝ftp服務器用於保存服務端上傳的圖片。 1、CentOS卸載vsftpd的方法 如果服務器上已經安裝了vsftpd服務,配置出錯需要卸載vsftpd服務。 1.1

Tomcat 服務自動啟動

查看 輸入 tomcat-7 lin sys clas ini fig cannot 一、配置/etc/rc.local rc.local是linux啟動init之後執行的腳本。 sudo vi /etc/rc.local 添加如下一行 /home/myuser/soft

centos 7 安裝ntp服務

每天 entos 0.20.2 detail ntp服務器 onf cst /usr centos 7 yum install ntp ntpdate -y 第二步 查找時間同步服務器 http://www.pool.ntp.org/zone/asia 第三步

CentOS 7 搭建 VPN 服務

rect 寫入 服務器 ets 添加 cto ubuntu 成功 工作區 檢查是否支持 若你使用XEN架構的VPS,下面的步驟不用執行 檢測PPP是否開啟: cat /dev/ppp 開啟成功的標誌:cat: /dev/ppp: No such file or direct

CentOS 7安裝並設置啟動圖形桌面

意思 set group emd install have 如果 運行 pow 服務器端有時沒有安裝圖形桌面,采用下面的步驟安裝gnome桌面,並能夠啟動後進入圖形桌面 1、安裝圖形環境 #yum grouplist#yum groupinstall ‘GNOME Des

CentOS 7系統關閉yum自動下載更新

默認 load lan centos 7 title 關閉 centos pda 文件 安裝CentOS 7後,系統yum自動更新狀態默認為開啟,若禁止系統自動更新需要手動關閉。 1.進入yum目錄 [root@localhost ~]$ cd /etc/yum 2.編輯y