CentOS7 sshd

http://blog.csdn.net/capricorn90/article/details/52578856

实验环境: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,一般情况想都是默认安装了,

[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

手动设置启动ssh服务

简单的设置就是在命令行中启动sshd服务。这样做比较快捷直接,但是只能对当前状态有效,一旦重启系统就丢失了该服务。

[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.

设置自动启动ssh服务

1、systemclt设置自动启动

通过systemctl命令可以将sshd服务加到开机自启动列表里。实现开机自动启动sshd服务。

[root@localhost ~]# systemctl enable sshd

2、修改ssh监听端口

在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 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连接。

[root@localhost ~]# semanage port -l | grep ssh #查看当前ssh服务监听的端口
ssh_port_t tcp 22
[root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 8090 #增加监听端口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 #查看防火墙状态
● 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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353