Ubuntu安装SSH服务
Posted Alone_lll
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu安装SSH服务相关的知识,希望对你有一定的参考价值。
目录
注:安装前需要先将“源”配置好(以下演示为root账号)
0、更新软件源
apt-get install -y update
1、安装openssl:
apt-get install -y update apt-get install -y openssh-server
2、启动ssh服务并配置开机自启
systemctl enable ssh #开机启动
systemctl restart ssh #重启ssh
systemctl status ssh #查看ssh状态
3、配置防火墙
注:也可以直接将防火墙关掉并禁止自启(不推荐此方式)
apt-get install -y firewalld #安装firewalld防火墙
systemctl stop firewalld #关闭防火墙
systemctl disabled firewalld #禁止防火墙自启
3.1 配置防火墙ssh通过及22端口
firewall-cmd --permanent --add-port=22/tcp #允许22端口通过
firewall-cmd --permanent --add-service=ssh #允许ssh服务通过
systemctl reload firewalld #刷新防火墙规则
firewall-cmd --list-all #查看防火墙所有规则
4、尝试使用SSH登录
查询Server IP地址
- ifconfig
- 若提示未安装需要“apt-get install -y net-tools”安装
root@alone:~# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.70 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::54ca:4141:8163:edcc prefixlen 64 scopeid 0x20<link>
ether 00:50:56:8c:ba:ab txqueuelen 1000 (以太网)
RX packets 5141 bytes 2567997 (2.5 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1318 bytes 154200 (154.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (本地环回)
RX packets 196 bytes 18395 (18.3 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 196 bytes 18395 (18.3 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- ip add
root@alone:~# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:8c:ba:ab brd ff:ff:ff:ff:ff:ff
altname enp3s0
inet 192.168.0.70/24 brd 192.168.0.255 scope global dynamic noprefixroute ens160
valid_lft 1306sec preferred_lft 1306sec
inet6 fe80::54ca:4141:8163:edcc/64 scope link noprefixroute
valid_lft forever preferred_lft forever
4.1 ROOT登录时出现权限问题
PS C:\\Users\\Alone> ssh root@192.168.0.70
root@192.168.0.70's password:
Permission denied, please try again.
4.1.1 修改ssh配置文件,设置允许root登录
注:找到“PermitRootLogin”修改或新增一行“PermitRootLogin yes”
root@alone:~# vim /etc/ssh/sshd_config
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
4.1.2 保存退出,重启ssh服务
方式一:
root@alone:~# /etc/init.d/ssh restart
Restarting ssh (via systemctl): ssh.service.
方式二:
root@alone:~# systemctl restart ssh
4.1.3 查看ssh服务状态
root@alone:~# systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2022-10-11 14:05:26 CST; 9min ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 3686 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 3687 (sshd)
Tasks: 1 (limit: 19119)
Memory: 4.0M
CPU: 130ms
CGroup: /system.slice/ssh.service
└─3687 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
10月 11 14:05:26 alone systemd[1]: Starting OpenBSD Secure Shell server...
10月 11 14:05:26 alone sshd[3687]: Server listening on 0.0.0.0 port 22.
10月 11 14:05:26 alone sshd[3687]: Server listening on :: port 22.
10月 11 14:05:26 alone systemd[1]: Started OpenBSD Secure Shell server.
10月 11 14:05:48 alone sshd[3689]: Accepted password for root from 192.168.2.124 port 53758 ssh2
10月 11 14:05:48 alone sshd[3689]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
4.1.4 查看ssh详细进程信息
root@alone:~# ps -ef | grep ssh
root 3687 1 0 14:05 ? 00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root 3689 3687 0 14:05 ? 00:00:00 sshd: root@pts/1
root 3764 3725 0 14:16 pts/1 00:00:00 grep --color=auto ssh
5 重新登录SSH即可
PS C:\\Users\\Alone> ssh root@192.168.0.70
root@192.168.0.70's password:
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-50-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
0 更新可以立即应用。
Last login: Tue Oct 11 14:03:02 2022 from 192.168.2.124
root@alone:~#
如何开启ubuntu的SSH服务
参考技术A SSH分客户端openssh-client和openssh-server 如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo apt-get install openssh-client),如果要使本机开放SSH服务就需要安装openssh-server sudo apt-get install openssh-server 然后确认sshserver是否启动了: ps -e |grep ssh 如果看到sshd那说明ssh-server已经启动了。 如果没有则可以这样启动:sudo /etc/init.d/ssh start 或者 service ssh start ssh-server配置文件位于/etc/ssh/sshd_config,在这里可以定义SSH的服务端口,默认端口是22,你可以自己定义成其他端口号,如222。 然后重启SSH服务: sudo /etc/init.d/ssh stop sudo /etc/init.d/ssh start 然后使用以下方式登陆SSH: ssh username@192.168.1.112 username为192.168.1.112 机器上的用户,需要输入密码。 SSH分客户端openssh-client和openssh-server如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo apt-get install openssh-client)查看SSH 是否安装 dpkg -l | grep sshcrifan@crifan-ubuntu10$ dpkg -l | grep sshii openssh-client 1:5.5p1-4ubuntu5 secure shell (SSH) client, for secure access to remote machinesii ssh-askpass-gnome 1:5.5p1-4ubuntu4 interactive X program to prompt users for a passphrase for ssh-addubuntu安装openssh-server1 .在线安装方式 sudo apt-get install openssh-server2 . 下载文件方式安装 ( 需先下载安装文件 openssh-server_5.5p1-4ubuntu4_i386.deb) sudo dpkg -i openssh-server_5.5p1-4ubuntu4_i386.deb然后确认sshserver是否启动了:ps -e |grep ssh如果看到sshd那说明ssh-server已经启动了。如果没有则可以这样启动:sudo /etc/init.d/ssh startssh-server配置文件位于/ etc/ssh/sshd_config在这里可以定义SSH的服务端口,默认端口是22,你可以自己定义成其他端口号,如222。然后重启SSH服务:sudo /etc/init.d/ssh stopsudo /etc/init.d/ssh start然后使用以下方式登陆SSH:ssh tuns@192.168.0.100 tuns为192.168.0.100机器上的用户,需要输入密码。断开连接:exit 安装出现问题 sudo apt-get -y updateNow we can install the package again:sudo apt-get install -y sshand upgrade the entire system (optional):sudo apt-get -y dist-upgrade以上是关于Ubuntu安装SSH服务的主要内容,如果未能解决你的问题,请参考以下文章