1.查看Linux IP地址
[root@localhost ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.233.131 netmask 255.255.255.0 broadcast 192.168.233.255
inet6 fe80::19e6:52ea:7ad4:f3c7 prefixlen 64 scopeid 0x20
ether 00:0c:29:f0:e8:bd txqueuelen 1000 (Ethernet)
RX packets 586 bytes 46166 (45.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 202 bytes 25339 (24.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2.查看是否开启SSH服务
[root@localhost ~]# ssh
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
[-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]
3.查看Windows是否开启SSH服务
C:\Users\qianz>ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
[-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
[-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
[-i identity_file] [-J [user@]host[:port]] [-L address]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-Q query_option] [-R address] [-S ctl_path] [-W host:port]
[-w local_tun[:remote_tun]] destination [command]
4.登录
C:\Users\qianz>ssh root@192.168.233.131
root@192.168.233.131's password:
Last login: Tue Dec 24 23:39:34 2019 from 192.168.233.1
命令解析:
ssh 用户名@IP地址 -p端口号
ssh服务的默认端口为22,可以通过修改其配置文件来设定端口,设定后需要重启服务
以下命令需要用root权限
1. 添加开放对外的端口(8080):
firewall-cmd --zone=public --add-port=8080/tcp --permanent
返回success为成功
命令含义:
--zone #作用域
--add-port=8080/tcp #添加端口,格式为:端口/通讯协议
--permanent #永久生效
2.重启防火墙:
systemctl restart firewalld.service
3.查看已经对外开放的端口:
firewall-cmd --list-ports
4.其他相关命令
关闭防火墙:
systemctl stop firewalld.service
查看监听(Listen)的端口:
netstat -lntp
检查端口被哪个进程占用:
netstat -lnp|grep 8080
删除端口配置:
firewall-cmd --zone=public --remove-port=80/tcp --permanent
[root@localhost ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success /*开启8080端口用于tcp服务,success表示开启成功*/
[root@localhost ~]# systemctl restart firewalld.service /*重启防火墙强*/
[root@localhost ~]# firewall-cmd --list-ports /*查看已对外开放的端口*/
8080/tcp
[root@localhost ~]#
[root@localhost ~]# firewall-cmd --zone=public --remove-port=8080/tcp --permanent
success /*关闭开放的8080端口*/
[root@localhost ~]# firewall-cmd --list-ports/*再次查看开放的端口情况*/
8080/tcp /*咦,怎么还开着?*/
[root@localhost ~]# systemctl restart firewalld.service
[root@localhost ~]# firewall-cmd --list-ports
[root@localhost ~]# /*原来是忘记重启防火墙了*/
C:\Users\qianz>ssh root@192.168.233.131 -p8080
ssh: connect to host 192.168.233.131 port 8080: Connection refused
[root@localhost ~]# firewall-cmd --zone=public --add-port=8080/ssh --permanent
Error: INVALID_PROTOCOL: 'ssh' not in {'tcp'|'udp'|'sctp'|'dccp'}
请不要直接对22端口进行修改。之所以先设置成两个端口,测试成功后再关闭一个端口,是为了防止在修改配置文件及网络调试过程中,万一出现新端口无法连接的情况下,还能通过22端口进行登录调试。