刚开始学linux,正好协会里有一台服务器,为方便以后管理,查了一些资料学习下远程连接服务器。以下是学习总结。
1、安装ssh,一般系统默认会有的,如果没有的话需要自己安装。
apt-get install openssh-server openssh-client
apt-get install ssh
如果在执行第一条命令时,报如下错误
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
openssh-server : Depends: openssh-client (= 1:7.2p2-4ubuntu2.8)
Depends: openssh-sftp-server but it is not going to be installed
Recommends: ssh-import-id but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
原因是 openssh-server
依赖于 openssh-client
安装提示中的版本,请执行如下命令
apt-get install openssh-client=1:7.2p2-4ubuntu2.8
然后再安装 openssh-server
。
2、输入sudo ps -e |grep ssh 查看是否启动服务,没有的话输入下面命令启动
/etc/init.d/ssh start
看到下图中的样子就是已经启动
3、查看配置文件是否允许root远程访问,一般系统默认不允许root远程访问。
vim /etc/ssh/sshd_config
将PermitRootLogin prohibit-password 这一行注释掉,并在下边添加一行PermitRootLogin yes
4、重启ssh-server服务
/etc/init.d/ssh restart
5、开启防火墙
ufw enable
PS:关闭防火墙
ufw disable
6、ssh默认通过22端口连接,所以要开启22端口
ufw allow 22
PS:关闭端口
ufw deny 22
7、设置开机自起,打开/etc/rc.local ,将 /etc/init.d/ssh start 加入
8、接下来就可以远程访问刚设置好的服务器了。
ssh root@192.* * * . * * * .* * *
接下来会提示是否确认,输入yes就行,然后输入密码
root@sd:~# ssh root@192.* * * . * * * .* * *
The authenticity of host '192.* * * . * * * .* * * (192.* * * . * * * .* * )’ can’t be established.
ECDSA key fingerprint is SHA256:X39QsW/lO1kGEcAnmfsZZKb2+JPP8DmvXsjLDs7dI6g.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192. * * . * * * .* * ’ (ECDSA) to the list of known hosts.
root@192. * * . * * * .* * *'s password:
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.13.0-36-generic i686)
输入然后就可以远程访问了
9、退出远程访问直接exit或者quit就可以啦。
10、卸载ssh,之前用apt-get remove ssh openssh-server openssh-client来卸载在重新安装时,总是会有一个报错,如下图
后来用下边命令卸载openssh-server后再次安装没有问题
apt-get purge openssh-server(注意没有remove)
apt-get remove ssh openssh-client
11、结束,优秀。