一、安装ssh
1、安装相关rpm包
1、检查是否安装:
rpm -qa|grep ssh
2、查询ssh服务相关安装包:
yum search ssh
3、安装ssh安装包:
yum install -y openssh
2、sshd服务启停命令
1、查看ssh服务运行状态:
systemctl status sshd.service
2、启动ssh服务:
systemctl start sshd.service
3、停止ssh服务:
systemctl stop sshd.service
4、重启ssh服务:
systemctl restart sshd.service
二、修改端口号
查询当前程序运行端口号:
netstat -anpl |grep sshd
注意:
首先要检查防火墙状态,因为防火墙规则在默认情况下只允 “22” 端口通过,添加新的端口号需要在防火墙中放行和修改ssh配置文件才能使用
(一)、防火墙设置
1、检查防火墙是否运行:
firewall-cmd --state
running
#若防火墙在running状态需要在规则中放行,若未开防火墙则直接修改ssh配置文件即可
2、在防火墙规则中放行端口号:
iptables -I INPUT -p tcp --dport 1022 -j ACCEPT
#1022为要修改的端口号
3、重新加载防火墙并保持状态信息:
firewall-cmd --reload
4、在防火墙规则中移除该端口
(1)、首先查看该端口规则的行号,(命令如下):
iptables -nL --line-number
#输出如下
#1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:1022
#2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
#3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
#4 INPUT_direct all -- 0.0.0.0/0 0.0.0.0/0
#5 INPUT_ZONES_SOURCE all -- 0.0.0.0/0 0.0.0.0/0
#6 INPUT_ZONES all -- 0.0.0.0/0 0.0.0.0/0
#7 DROP all -- 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
#8 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-#prohibited
(2)、移除第一条规则,(命令如下):
iptables -D INPUT 1
#1为防火墙规则中的行号,就是“1022”端口规则所在行
(二)、selinux设置
1、检查selinux是否开启:
getenforce
或:
/usr/sbin/sestatus -v
#若selinux在 “Permissive” 状态,则需要将新加的端口号添加到selinux内,允许ssh服务通过此端口登录到服务器
#若selinux为 “Disable”状态,则直接修改ssh配置文件即可
2、将端口添加到selinux中
semanage port -a -t ssh_port_t -p tcp 1022
#1022为添加的端口号
其实该命令在"/etc/ssh/sshd_config"sshd配置文件中就有说明:部分内容如下
# If you want to change the port on a SELinux system, you have to tell #如果你想在SELinux系统上更改端口,你必须告诉它
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#如果你想在SELinux系统上更改端口,你必须告诉它
# SELinux关于这个变化。
3、使用semanage命令查看在selinux中ssh使用的端口号
#若:(-bash: semanage: command not found)
#使用yum命令安装:yum install policycoreutils-python
semanage port -l|grep ssh
#输出如下:
ssh_port_t tcp 1022, 22
4、将selinux中新添加的端口移除
semanage port -d -t ssh_port_t -p tcp 1022
#再次查询输出如下输出如下:
ssh_port_t tcp 22
(三)、ssh配置文件设置
1、编辑ssh配置文件:
vi /etc/ssh/sshd_config
-----------------------------------------------------------------------------------------
2、"i"进入插入模式,去掉"Port"前面的注释'#',将"22"修改为新的端口号,如下:
# 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 1022 #ssh服务默认的端口号为 “22”,并且最大端口号不能超过 “65535”
Port 2022 #或者像这样再添加一个新的端口号,这样通过这两个端口都可以访问服务器
...
修改完毕后摁"ESC"键 输入":wq"保存退出
-----------------------------------------------------------------------------------------
3、重启ssh服务:
systemctl restart sshd.service
-----------------------------------------------------------------------------------------
4、查看ssh服务运行状态:
systemctl status sshd.service
* sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-09-06 09:25:45 CST; 11s ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 13692 (sshd)
CGroup: /system.slice/sshd.service
`-13692 /usr/sbin/sshd -D
Sep 06 09:25:45 centos01 systemd[1]: Starting OpenSSH server daemon...
Sep 06 09:25:45 centos01 sshd[13692]: Server listening on 0.0.0.0 port 1022.
Sep 06 09:25:45 centos01 sshd[13692]: Server listening on :: port 1022.
Sep 06 09:25:45 centos01 systemd[1]: Started OpenSSH server daemon.
-----------------------------------------------------------------------------------------
此时ssh服务端口号已经修改完毕,再通过ssh连接到该服务器就需要使用新的端口号