實驗環(huán)境:CentOS7 Minimal安裝,安裝過程及軟件包見http://blog.csdn.net/capricorn90/article/details/52556174
SSH的英文全稱是Secure SHell。通過使用SSH,你可以把所有傳輸?shù)臄?shù)據(jù)進行加密,這樣“中間人”這種攻擊方式就不可能實現(xiàn)了,而且也能夠防止DNS和IP欺騙。還有一個額外的好處就是傳輸?shù)臄?shù)據(jù)是經(jīng)過壓縮的,所以可以加快傳輸?shù)乃俣?。SSH有很多功能,它既可以代替telnet,又可以為ftp、pop、甚至ppp提供一個安全的“通道”。
SSH在Linux中的服務是sshd,安裝openssh后才可開啟。CentOS 7 安裝后默認情況下是不啟動sshd服務,即無法通過ssh服務遠程連接。
首先查看系統(tǒng)是否安裝openssh,一般情況想都是默認安裝了,
[root@localhost ~]# 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在線安裝。
[root@localhost ~]# yum install openssh
簡單的設置就是在命令行中啟動sshd服務。這樣做比較快捷直接,但是只能對當前狀態(tài)有效,一旦重啟系統(tǒng)就丟失了該服務。
[root@localhost ~]# systemctl start sshd
[root@localhost ~]# 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.
通過systemctl命令可以將sshd服務加到開機自啟動列表里。實現(xiàn)開機自動啟動sshd服務。
[root@localhost ~]# systemctl enable sshd
在sshd_config文件中存放了端口、控制策略等信息。
[root@localhost ~]# 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 2HostKey /etc/ssh/ssh_host_rsa_key#HostKey /etc/ssh/ssh_host_dsa_keyHostKey /etc/ssh/ssh_host_ecdsa_keyHostKey /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 AUTHSyslogFacility 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_keysAuthorizedKeysFile .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 noPasswordAuthentication yes# Change to no to disable s/key passwords#ChallengeResponseAuthentication yesChallengeResponseAuthentication no# Kerberos options#KerberosAuthentication no#KerberosOrLocalPasswd yes#KerberosTicketCleanup yes#KerberosGetAFSToken no#KerberosUseKuserok yes# GSSAPI optionsGSSAPIAuthentication yesGSSAPICleanupCredentials 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 noX11Forwarding yes#X11DisplayOffset 10#X11UseLocalhost yes#PermitTTY yes#PrintMotd yes#PrintLastLog yes#TCPKeepAlive yes#UseLogin noUsePrivilegeSeparation 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 variablesAcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGESAcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENTAcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGEAcceptEnv XMODIFIERS# override default of no subsystemsSubsystem 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登錄,這種設置通常用于互聯(lián)網(wǎng)服務器,防止提權后用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連接。
[root@localhost ~]# semanage port -l | grep ssh #查看當前ssh服務監(jiān)聽的端口
ssh_port_t tcp 22
[root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 8090 #增加監(jiān)聽端口8090[root@localhost ~]# semanage port -l | grep ssh
ssh_port_t tcp 8090,22
semanage只是端口工具,修改防火墻只能使用firewall-cmd
[root@localhost 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[root@localhost ssh]# yum -y install firewalld #安裝防火墻工具
[root@localhost ssh]# systemctl start firewalld #啟動防火墻服務
[root@localhost ssh]# systemctl status firewalld #查看防火墻狀態(tài)
● 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.
[root@localhost ssh]# firewall-cmd –zone=public –add-port=8090/tcp –permanent #防火墻中允許8090端口通過
success[root@localhost ssh]# semanage port -m -t ssh_port_t -p tcp 8090 #將ssh服務修改為8090端口
[root@localhost ssh]# firewall-cmd –zone=public –remove-port=22/tcp –permanent #刪除22端口
success
[root@localhost ssh]# firewall-cmd –reload #重新加載防火墻服務配置
success