一. 前言
写在前面:如果你安装的cent6.8镜像是live版,请尽快换成bin版本,live版重启所有配置都会恢复默认,谨记谨记,这可是血的教训啊!!!
我用的cent镜像文件 ==> “CentOS-6.8-x86_64-bin-DVD1.iso” && “CentOS-7-x86_64-Everything-1611.iso”
如果你注意到了上面我写的,你可能不会遇到这个问题,但我还是记录一下,需要的童鞋可以参考。
进入正题==> 要用xshell远程连接我们的Cent服务器,需要满足两个条件:
1. 关闭Cent防火墙/放行22端口(shell默认通过22端口进行访问);
2. 启动Cent服务端的ssh服务。
注:所有指令是在root用户下进行的;“关闭防火墙”和“放行22端口”二选一
二. CentOS6.8设置
- 关闭防火墙
- 查看防火墙状态:
service iptables status
- 关闭防火墙:
service iptables stop
- 永久关闭防火墙(不建议,关闭后可在需要时打开):
chkconfig iptables off
- 放行22端口
- 检查iptables文件是否存在:
cd /etc/sysconfig/
ls
- 如果不存在:
①. 任意运行一条iptables防火墙规则配置命令:iptables -P OUTPUT ACCEPT
②. 对iptables服务进行保存:service iptables save
③. 重启iptables服务:service iptables restart
- 放行22端口:
编辑iptables文件:vi /etc/sysconfig/iptables
增加这条规则,保存退出:-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
重启iptables服务:service iptables restart
设置iptables自启动:chkconfig iptables on
- 启动Cent的ssh服务
- 查看ssh状态:
service sshd status
- 如果ssh服务未启动,执行启动ssh服务:
service sshd start
- 设置sshd服务开机启动:
chkconfig sshd on
- 关闭防火墙并启动ssh服务后
通过ssh localhost
命令测试一下本机通过ssh方式登录 - 利用xshell远程登录
三. CentOS7设置
注:1.cent7的服务启停相关指令有所改变;
2.CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下
- 关闭防火墙
- 停止firewall:
systemctl stop firewalld.service
- 屏蔽firewall:
systemctl mask firewalld.service
- 禁止firewall开机启动:
systemctl disable firewalld.service
- 放行22端口
- 安装iptables服务:
yum -y install iptables-services
- 编辑iptables文件:
vi /etc/sysconfig/iptables
增加这条规则,保存退出:-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
- 重启iptables服务:
systemctl restart iptables.service
- 设置iptables自启动:
systemctl enable iptables.service
- 启动Cent的ssh服务
- 查看ssh状态:
systemctl status sshd.service
- 如果ssh服务未启动,执行启动ssh服务:
systemctl start sshd.service
注:这个命令开启ssh服务是暂时的,下次系统重启,不会启动ssh服务 - 设置sshd服务开机启动:
systemctl enable sshd.service
- 关闭防火墙并启动ssh服务后
通过ssh localhost
命令测试一下本机通过ssh方式登录 - 利用xshell远程登录