阿里云CentOS服务器更改ssh端口

背景

自从搞了个博客网站后,老是有人想通过ssh登陆我的服务器。就下边这些IP(第三列)全都是。

[test@aliyun ~]# lastb
rooasdad ssh:notty    106.37.187.193   Wed Jul  8 11:20 - 11:20  (00:00)    
rooasdad ssh:notty    106.37.187.193   Wed Jul  8 11:20 - 11:20  (00:00)    
admin    ssh:notty    109.94.52.93     Wed Jul  8 11:16 - 11:16  (00:00)    
admin    ssh:notty    109.94.52.93     Wed Jul  8 11:16 - 11:16  (00:00)    
admin    ssh:notty    113.173.16.100   Wed Jul  8 11:16 - 11:16  (00:00)    
admin    ssh:notty    113.173.16.100   Wed Jul  8 11:16 - 11:16  (00:00)    
root     ssh:notty    106.37.187.193   Wed Jul  8 11:10 - 11:10  (00:00)    
root     ssh:notty    106.37.187.193   Wed Jul  8 11:09 - 11:09  (00:00)    
guest    ssh:notty    189.143.95.100   Wed Jul  8 02:38 - 02:38  (00:00)    
guest    ssh:notty    189.143.95.100   Wed Jul  8 02:38 - 02:38  (00:00)    
cirros   ssh:notty    96.28.161.249    Tue Jul  7 08:51 - 08:51  (00:00)    
cirros   ssh:notty    96.28.161.249    Tue Jul  7 08:51 - 08:51  (00:00)    
ethos    ssh:notty    96.28.161.249    Tue Jul  7 08:51 - 08:51  (00:00)    
ethos    ssh:notty    96.28.161.249    Tue Jul  7 08:51 - 08:51  (00:00)    
volumio  ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
volumio  ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
apache   ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
apache   ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
admin    ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
admin    ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
admin    ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
admin    ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
admin    ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
admin    ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
root     ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
admin    ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
admin    ssh:notty    96.28.161.249    Tue Jul  7 08:50 - 08:50  (00:00)    
admin    ssh:notty    42.60.17.192     Tue Jul  7 04:54 - 04:54  (00:00)    
admin    ssh:notty    42.60.17.192     Tue Jul  7 04:54 - 04:54  (00:00)    
admin    ssh:notty    216.200.107.94   Tue Jul  7 04:54 - 04:54  (00:00)    
admin    ssh:notty    216.200.107.94   Tue Jul  7 04:54 - 04:54  (00:00)    
administ ssh:notty    14.173.245.250   Mon Jul  6 18:22 - 18:22  (00:00)    
administ ssh:notty    14.173.245.250   Mon Jul  6 18:21 - 18:21  (00:00)    
root     ssh:notty    103.252.167.93   Mon Jul  6 16:42 - 16:42  (00:00)    
pi       ssh:notty    79.209.219.193   Sun Jul  5 22:23 - 22:23  (00:00)    
pi       ssh:notty    79.209.219.193   Sun Jul  5 22:23 - 22:23  (00:00)    
pi       ssh:notty    79.209.219.193   Sun Jul  5 22:23 - 22:23  (00:00)    
pi       ssh:notty    79.209.219.193   Sun Jul  5 22:23 - 22:23  (00:00)    
Administ ssh:notty    123.19.79.20     Sun Jul  5 10:56 - 10:56  (00:00)    
Administ ssh:notty    123.19.79.20     Sun Jul  5 10:56 - 10:56  (00:00)    
admin    ssh:notty    186.179.100.22   Sat Jul  4 16:11 - 16:11  (00:00)    
admin    ssh:notty    186.179.100.22   Sat Jul  4 16:11 - 16:11  (00:00)    
admin    ssh:notty    14.231.248.113   Sat Jul  4 16:11 - 16:11  (00:00)    
admin    ssh:notty    14.231.248.113   Sat Jul  4 16:10 - 16:10  (00:00)    
avanthi  ssh:notty    36.84.56.81      Sat Jul  4 06:27 - 06:27  (00:00)    
avanthi  ssh:notty    36.84.56.81      Sat Jul  4 06:27 - 06:27  (00:00)    
root     ssh:notty    219.147.74.48    Sat Jul  4 01:41 - 01:41  (00:00)    
ping     ssh:notty    219.147.74.48    Sat Jul  4 01:37 - 01:37  (00:00)    
ping     ssh:notty    219.147.74.48    Sat Jul  4 01:37 - 01:37  (00:00)    
laury    ssh:notty    219.147.74.48    Sat Jul  4 01:34 - 01:34  (00:00)    
laury    ssh:notty    219.147.74.48    Sat Jul  4 01:34 - 01:34  (00:00)    
alan     ssh:notty    219.147.74.48    Sat Jul  4 01:30 - 01:30  (00:00)    
alan     ssh:notty    219.147.74.48    Sat Jul  4 01:30 - 01:30  (00:00)    
idc      ssh:notty    219.147.74.48    Sat Jul  4 01:26 - 01:26  (00:00)    
idc      ssh:notty    219.147.74.48    Sat Jul  4 01:26 - 01:26  (00:00)    
origin   ssh:notty    219.147.74.48    Sat Jul  4 01:22 - 01:22  (00:00)    
origin   ssh:notty    219.147.74.48    Sat Jul  4 01:22 - 01:22  (00:00)    
ju       ssh:notty    219.147.74.48    Sat Jul  4 01:18 - 01:18  (00:00)    
ju       ssh:notty    219.147.74.48    Sat Jul  4 01:18 - 01:18  (00:00)    
root     ssh:notty    219.147.74.48    Sat Jul  4 01:14 - 01:14  (00:00)    
ftpuser  ssh:notty    219.147.74.48    Sat Jul  4 01:10 - 01:10  (00:00)    
ftpuser  ssh:notty    219.147.74.48    Sat Jul  4 01:10 - 01:10  (00:00)    
friedric ssh:notty    219.147.74.48    Sat Jul  4 01:06 - 01:06  (00:00)    
friedric ssh:notty    219.147.74.48    Sat Jul  4 01:06 - 01:06  (00:00)    
nagios   ssh:notty    219.147.74.48    Sat Jul  4 01:02 - 01:02  (00:00)    
nagios   ssh:notty    219.147.74.48    Sat Jul  4 01:02 - 01:02  (00:00)    
wizard   ssh:notty    219.147.74.48    Sat Jul  4 00:58 - 00:58  (00:00)    
wizard   ssh:notty    219.147.74.48    Sat Jul  4 00:58 - 00:58  (00:00)    
yangyang ssh:notty    219.147.74.48    Sat Jul  4 00:54 - 00:54  (00:00)    
yangyang ssh:notty    219.147.74.48    Sat Jul  4 00:54 - 00:54  (00:00)    
root     ssh:notty    219.147.74.48    Sat Jul  4 00:50 - 00:50  (00:00)    
bgs      ssh:notty    219.147.74.48    Sat Jul  4 00:46 - 00:46  (00:00)    
bgs      ssh:notty    219.147.74.48    Sat Jul  4 00:46 - 00:46  (00:00)    
root     ssh:notty    219.147.74.48    Sat Jul  4 00:41 - 00:41  (00:00)    
gamer    ssh:notty    219.147.74.48    Sat Jul  4 00:37 - 00:37  (00:00)    
gamer    ssh:notty    219.147.74.48    Sat Jul  4 00:37 - 00:37  (00:00)    
lu       ssh:notty    219.147.74.48    Sat Jul  4 00:32 - 00:32  (00:00)    
lu       ssh:notty    219.147.74.48    Sat Jul  4 00:32 - 00:32  (00:00)    
root     ssh:notty    219.147.74.48    Sat Jul  4 00:28 - 00:28  (00:00)    
backup   ssh:notty    219.147.74.48    Sat Jul  4 00:23 - 00:23  (00:00)    
backup   ssh:notty    219.147.74.48    Sat Jul  4 00:23 - 00:23  (00:00)    
auditori ssh:notty    219.147.74.48    Sat Jul  4 00:19 - 00:19  (00:00)    
auditori ssh:notty    219.147.74.48    Sat Jul  4 00:19 - 00:19  (00:00)    
www      ssh:notty    219.147.74.48    Sat Jul  4 00:14 - 00:14  (00:00)    
www      ssh:notty    219.147.74.48    Sat Jul  4 00:14 - 00:14  (00:00)    
root     ssh:notty    219.147.74.48    Sat Jul  4 00:10 - 00:10  (00:00)    
anpr     ssh:notty    219.147.74.48    Sat Jul  4 00:05 - 00:05  (00:00)    
anpr     ssh:notty    219.147.74.48    Sat Jul  4 00:05 - 00:05  (00:00)    
beta     ssh:notty    219.147.74.48    Sat Jul  4 00:01 - 00:01  (00:00)    
beta     ssh:notty    219.147.74.48    Sat Jul  4 00:01 - 00:01  (00:00)    
postgres ssh:notty    219.147.74.48    Fri Jul  3 23:56 - 23:56  (00:00)    
postgres ssh:notty    219.147.74.48    Fri Jul  3 23:56 - 23:56  (00:00)    
root     ssh:notty    219.147.74.48    Fri Jul  3 23:51 - 23:51  (00:00)    
root     ssh:notty    219.147.74.48    Fri Jul  3 23:47 - 23:47  (00:00)    
demo     ssh:notty    219.147.74.48    Fri Jul  3 23:42 - 23:42  (00:00)    
demo     ssh:notty    219.147.74.48    Fri Jul  3 23:42 - 23:42  (00:00)    
root     ssh:notty    219.147.74.48    Fri Jul  3 23:38 - 23:38  (00:00)    
keith    ssh:notty    219.147.74.48    Fri Jul  3 23:33 - 23:33  (00:00)    
keith    ssh:notty    219.147.74.48    Fri Jul  3 23:33 - 23:33  (00:00)    
vmail    ssh:notty    219.147.74.48    Fri Jul  3 23:28 - 23:28  (00:00)    
vmail    ssh:notty    219.147.74.48    Fri Jul  3 23:28 - 23:28  (00:00)    
User     ssh:notty    219.147.74.48    Fri Jul  3 23:23 - 23:23  (00:00)    
User     ssh:notty    219.147.74.48    Fri Jul  3 23:23 - 23:23  (00:00)    
shen     ssh:notty    219.147.74.48    Fri Jul  3 23:18 - 23:18  (00:00)    
shen     ssh:notty    219.147.74.48    Fri Jul  3 23:18 - 23:18  (00:00)    
zhangwei ssh:notty    219.147.74.48    Fri Jul  3 23:02 - 23:02  (00:00)    
zhangwei ssh:notty    219.147.74.48    Fri Jul  3 23:02 - 23:02  (00:00)    
backup   ssh:notty    111.229.194.38   Fri Jul  3 16:03 - 16:03  (00:00)    
backup   ssh:notty    111.229.194.38   Fri Jul  3 16:03 - 16:03  (00:00)    
admina   ssh:notty    27.74.168.23     Fri Jul  3 15:13 - 15:13  (00:00)    
admina   ssh:notty    27.74.168.23     Fri Jul  3 15:13 - 15:13  (00:00)    
scott    ssh:notty    14.204.145.108   Fri Jul  3 13:37 - 13:37  (00:00)    
scott    ssh:notty    14.204.145.108   Fri Jul  3 13:37 - 13:37  (00:00)    
sftp     ssh:notty    14.204.145.108   Fri Jul  3 13:33 - 13:33  (00:00)    
sftp     ssh:notty    14.204.145.108   Fri Jul  3 13:33 - 13:33  (00:00)    
m1       ssh:notty    14.204.145.108   Fri Jul  3 13:30 - 13:30  (00:00)    
m1       ssh:notty    14.204.145.108   Fri Jul  3 13:30 - 13:30  (00:00)    
martha   ssh:notty    14.204.145.108   Fri Jul  3 13:27 - 13:27  (00:00)    
martha   ssh:notty    14.204.145.108   Fri Jul  3 13:27 - 13:27  (00:00)    
kea      ssh:notty    14.204.145.108   Fri Jul  3 13:23 - 13:23  (00:00)    
kea      ssh:notty    14.204.145.108   Fri Jul  3 13:23 - 13:23  (00:00)    
user6    ssh:notty    14.204.145.108   Fri Jul  3 13:20 - 13:20  (00:00)    
user6    ssh:notty    14.204.145.108   Fri Jul  3 13:20 - 13:20  (00:00)    
zlj      ssh:notty    14.204.145.108   Fri Jul  3 13:17 - 13:17  (00:00)    
zlj      ssh:notty    14.204.145.108   Fri Jul  3 13:17 - 13:17  (00:00)    
lkh      ssh:notty    14.204.145.108   Fri Jul  3 13:13 - 13:13  (00:00)    
lkh      ssh:notty    14.204.145.108   Fri Jul  3 13:13 - 13:13  (00:00)    
mhj      ssh:notty    14.204.145.108   Fri Jul  3 13:10 - 13:10  (00:00)    
mhj      ssh:notty    14.204.145.108   Fri Jul  3 13:10 - 13:10  (00:00)    
mms      ssh:notty    14.204.145.108   Fri Jul  3 13:07 - 13:07  (00:00)    
mms      ssh:notty    14.204.145.108   Fri Jul  3 13:07 - 13:07  (00:00)    
vps      ssh:notty    14.204.145.108   Fri Jul  3 13:04 - 13:04  (00:00)    
vps      ssh:notty    14.204.145.108   Fri Jul  3 13:04 - 13:04  (00:00)    
bserver  ssh:notty    14.204.145.108   Fri Jul  3 13:00 - 13:00  (00:00)    
bserver  ssh:notty    14.204.145.108   Fri Jul  3 13:00 - 13:00  (00:00)    
zilong   ssh:notty    14.204.145.108   Fri Jul  3 12:57 - 12:57  (00:00)    
zilong   ssh:notty    14.204.145.108   Fri Jul  3 12:57 - 12:57  (00:00)    
test     ssh:notty    14.204.145.108   Fri Jul  3 12:54 - 12:54  (00:00)    
test     ssh:notty    14.204.145.108   Fri Jul  3 12:54 - 12:54  (00:00)    
kim      ssh:notty    14.204.145.108   Fri Jul  3 12:51 - 12:51  (00:00)    
kim      ssh:notty    14.204.145.108   Fri Jul  3 12:51 - 12:51  (00:00)    
jake     ssh:notty    14.204.145.108   Fri Jul  3 12:47 - 12:47  (00:00)    
jake     ssh:notty    14.204.145.108   Fri Jul  3 12:47 - 12:47  (00:00)    
tftpd    ssh:notty    14.204.145.108   Fri Jul  3 12:44 - 12:44  (00:00)    
tftpd    ssh:notty    14.204.145.108   Fri Jul  3 12:44 - 12:44  (00:00)    
ubuntu   ssh:notty    14.204.145.108   Fri Jul  3 12:41 - 12:41  (00:00)    
ubuntu   ssh:notty    14.204.145.108   Fri Jul  3 12:41 - 12:41  (00:00)    
root     ssh:notty    14.204.145.108   Fri Jul  3 12:38 - 12:38  (00:00)    
adminuse ssh:notty    14.204.145.108   Fri Jul  3 12:35 - 12:35  (00:00)    
adminuse ssh:notty    14.204.145.108   Fri Jul  3 12:35 - 12:35  (00:00)    
samp     ssh:notty    14.204.145.108   Fri Jul  3 12:32 - 12:32  (00:00)    
samp     ssh:notty    14.204.145.108   Fri Jul  3 12:32 - 12:32  (00:00)    
pulse    ssh:notty    14.204.145.108   Fri Jul  3 12:28 - 12:28  (00:00)    
pulse    ssh:notty    14.204.145.108   Fri Jul  3 12:28 - 12:28  (00:00)    
root     ssh:notty    14.204.145.108   Fri Jul  3 12:25 - 12:25  (00:00)    
muan     ssh:notty    14.204.145.108   Fri Jul  3 12:22 - 12:22  (00:00)    
muan     ssh:notty    14.204.145.108   Fri Jul  3 12:22 - 12:22  (00:00)    
gengjiao ssh:notty    14.204.145.108   Fri Jul  3 12:19 - 12:19  (00:00)    
gengjiao ssh:notty    14.204.145.108   Fri Jul  3 12:19 - 12:19  (00:00)    
rc       ssh:notty    14.204.145.108   Fri Jul  3 12:16 - 12:16  (00:00)    
rc       ssh:notty    14.204.145.108   Fri Jul  3 12:16 - 12:16  (00:00)    
zabbix   ssh:notty    14.204.145.108   Fri Jul  3 12:13 - 12:13  (00:00)    
zabbix   ssh:notty    14.204.145.108   Fri Jul  3 12:13 - 12:13  (00:00)    
test02   ssh:notty    14.204.145.108   Fri Jul  3 12:10 - 12:10  (00:00)    
test02   ssh:notty    14.204.145.108   Fri Jul  3 12:10 - 12:10  (00:00)    
root     ssh:notty    14.204.145.108   Fri Jul  3 12:07 - 12:07  (00:00)    
123      ssh:notty    14.204.145.108   Fri Jul  3 12:04 - 12:04  (00:00)    
123      ssh:notty    14.204.145.108   Fri Jul  3 12:04 - 12:04  (00:00)    
upload   ssh:notty    14.204.145.108   Fri Jul  3 12:01 - 12:01  (00:00)    
upload   ssh:notty    14.204.145.108   Fri Jul  3 12:01 - 12:01  (00:00)    
root     ssh:notty    14.204.145.108   Fri Jul  3 11:58 - 11:58  (00:00)    
root     ssh:notty    14.204.145.108   Fri Jul  3 11:55 - 11:55  (00:00)    
midgear  ssh:notty    14.204.145.108   Fri Jul  3 11:52 - 11:52  (00:00)    
midgear  ssh:notty    14.204.145.108   Fri Jul  3 11:52 - 11:52  (00:00)    
matthew  ssh:notty    14.204.145.108   Fri Jul  3 11:49 - 11:49  (00:00)    
matthew  ssh:notty    14.204.145.108   Fri Jul  3 11:49 - 11:49  (00:00)    
francesc ssh:notty    14.204.145.108   Fri Jul  3 11:46 - 11:46  (00:00)    
francesc ssh:notty    14.204.145.108   Fri Jul  3 11:46 - 11:46  (00:00)    
root     ssh:notty    14.204.145.108   Fri Jul  3 11:43 - 11:43  (00:00)    
lynn     ssh:notty    14.204.145.108   Fri Jul  3 11:40 - 11:40  (00:00)    
lynn     ssh:notty    14.204.145.108   Fri Jul  3 11:40 - 11:40  (00:00)    
root     ssh:notty    14.204.145.108   Fri Jul  3 11:37 - 11:37  (00:00)    
friends  ssh:notty    14.204.145.108   Fri Jul  3 11:34 - 11:34  (00:00)    
friends  ssh:notty    14.204.145.108   Fri Jul  3 11:34 - 11:34  (00:00)    
root     ssh:notty    14.204.145.108   Fri Jul  3 11:31 - 11:31  (00:00)    
csgoserv ssh:notty    14.204.145.108   Fri Jul  3 11:28 - 11:28  (00:00)    
csgoserv ssh:notty    14.204.145.108   Fri Jul  3 11:28 - 11:28  (00:00)    
taniac   ssh:notty    14.204.145.108   Fri Jul  3 11:25 - 11:25  (00:00)    
taniac   ssh:notty    14.204.145.108   Fri Jul  3 11:25 - 11:25  (00:00)    
ftp-user ssh:notty    14.204.145.108   Fri Jul  3 11:22 - 11:22  (00:00)    
ftp-user ssh:notty    14.204.145.108   Fri Jul  3 11:22 - 11:22  (00:00)    
oracle   ssh:notty    14.204.145.108   Fri Jul  3 11:19 - 11:19  (00:00)    
oracle   ssh:notty    14.204.145.108   Fri Jul  3 11:19 - 11:19  (00:00)    
lei      ssh:notty    14.204.145.108   Fri Jul  3 11:16 - 11:16  (00:00)    
lei      ssh:notty    14.204.145.108   Fri Jul  3 11:15 - 11:15  (00:00)    
root     ssh:notty    14.204.145.108   Fri Jul  3 11:12 - 11:12  (00:00)    
root     ssh:notty    14.204.145.108   Fri Jul  3 11:09 - 11:09  (00:00)    
bounce   ssh:notty    14.204.145.108   Fri Jul  3 10:51 - 10:51  (00:00)    
bounce   ssh:notty    14.204.145.108   Fri Jul  3 10:51 - 10:51  (00:00)    
root     ssh:notty    187.26.174.7     Fri Jul  3 10:24 - 10:24  (00:00)    
admin    ssh:notty    125.166.158.82   Fri Jul  3 10:01 - 10:01  (00:00)    
admin    ssh:notty    125.166.158.82   Fri Jul  3 10:01 - 10:01  (00:00)    
admin    ssh:notty    14.226.247.114   Fri Jul  3 09:49 - 09:49  (00:00)    
admin    ssh:notty    14.226.247.114   Fri Jul  3 09:49 - 09:49  (00:00)    
admin    ssh:notty    186.226.169.11   Fri Jul  3 09:49 - 09:49  (00:00)    
admin    ssh:notty    186.226.169.11   Fri Jul  3 09:49 - 09:49  (00:00)    
root     ssh:notty    81.60.34.217     Thu Jul  2 22:32 - 22:32  (00:00)    
noc      ssh:notty    118.71.191.93    Thu Jul  2 15:53 - 15:53  (00:00)    
noc      ssh:notty    118.71.191.93    Thu Jul  2 15:53 - 15:53  (00:00)    
root     ssh:notty    186.13.143.216   Thu Jul  2 09:49 - 09:49  (00:00)    
admina   ssh:notty    14.182.202.159   Wed Jul  1 12:12 - 12:12  (00:00)    
admina   ssh:notty    14.182.202.159   Wed Jul  1 12:12 - 12:12  (00:00)    
sniffer  ssh:notty    190.245.105.20   Wed Jul  1 08:50 - 08:50  (00:00)    
sniffer  ssh:notty    190.245.105.20   Wed Jul  1 08:50 - 08:50  (00:00)

也不知道是何居心,所以干脆就把端口给改了,一了百了。

操作步骤

  1. 去阿里云控制台配置安全组规则,新增一套规则,端口使用10086(端口号随意,尽量1024以上)。
  2. 登陆服务器,编辑文件/etc/ssh/sshd_config
vim /etc/ssh/sshd_config

取消 #Port 22 的注释,在下一行添加你需要修改的新端口Port 10086。这里不删除22 端口是为了防止修改后新端口无法访问,造成无法用 ssh 连接服务器。修改后保存并退出。

  1. 重启sshd服务:
systemctl restart sshd
  1. 关闭防火墙
# Centos7 使用以下命令
systemctl stop firewalld.service
# 永久禁用防火墙
systemctl disable firewalld.service
  1. 用 ss 命令检查 ssh 监听的端口是否有10086
ss -tnlp | grep sshd

10086这个端口的话,就继续编辑/etc/ssh/sshd_config文件,将之前的22端口号注释掉,然后保存,再次重启sshd服务就可以了。

  1. 使用ssh连接服务器
ssh -p 10086 用户名名@服务器IP或域名