新装的系统如果是最小化安装,可能不会安装 openssh-server,无法ssh远程连接
1.查看否安装SSH服务
yum list installed | grep openssh-server
rpm -qa | grep ssh
rpm -qa|grep xxx 命令
-qa代表query,a代表all;
rpm -q ----查询一个包是否被安装
rpm -qa ----列出所有被安装的rpm package
| ----| 表示管道,上一条命令的输出,作为下一条命令参数(输入)。
grep ----用于查找文件里符合条件的字符串。
xxx ----要查询的名称
若没有,则安装装
yum install openssh-server
2. sshd服务配置
vi /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy 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 //SSH默认端口为22,在此处也可以修改为其他端口
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
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
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
PermitRootLogin yes //允许root用户登录
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
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
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# 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
service sshd start //开启ssh服务
service sshd restart //重启ssh服务
ss -ntp | grep 22
ps -e | grep sshd //检查 sshd 服务是否已经开启
netstat -an | grep 22 //检查 22 号端口是否开启监听 或 netstat -anlpt | grep 22
systemctl status sshd //检查 sshd服务状态
systemctl enable sshd.service //设置开机自启动
systemctl list-unit-files | grep sshd //查看是否开启sshd 服务自启动
[root@localhost ~]# systemctl list-unit-files | grep sshd
anaconda-sshd.service static
sshd-keygen.service static
sshd.service enabled
sshd@.service static
sshd.socket disabled
[root@localhost ~]#
ps -ef|grep ssh //查看ssh服务进程命令
chkconfig sshd on //设置ssh服务为开机启动命令
chkconfig sshd off //设置ssh服务禁止开机启动命令
service sshd stop //停止ssh服务命令
netstat -antp | grep sshd //查看ssh服务22端口是否启动命令