设置静态 IP 地址无法连接?

设置静态 IP 地址无法连接?

我正在按照教程为我的远程备份服务器设置静态 IP 地址。(https://www.techrepublic.com/article/how-to-configure-a-static-ip-address-in-ubuntu-server-18-04/

我已遵循以下说明:

network:
        renderer: networkd
        ethernets:
            enp0s25:
                dhcp4: no
                addresses: [192.168.111.27/24]
                gateway4: 192.168.1.1
                nameservers:
                         addresses: [192.168.1.1,8.8.8.8]
        version: 2

但是现在我无法连接到我的服务器,并且必须从我进行修改之前制作的旧副本中恢复其网络计划。

自定义 SSH 配置:

Host Scilab
  HostName 192.168.43.245
  Port 45834
  IdentityFile ~/.ssh/LesserArkKey

当我尝试使用时,ssh Scilab 我得到了:ssh: connect to host 192.168.43.245 port 45834: Connection refused。这很不寻常,因为这以前有效(我有一个自定义的 ssh 配置)。我将当前的 ssh 配置更改为新的 IP 地址(以前是 192.168.1.144)

我做错了什么,如何将 IP 地址设置为静态而不是 DCHP?

编辑0:需要澄清的是,当服务器使用默认 Netplan 时,基于服务器密钥的登录工作正常。它ssh Scilab要求输入加密密钥,我输入密码,然后一切就都连接上了。只有当我尝试使用新的 Netplan 时,它才会出错。然后什么都不起作用。

这些命令也全部失败:

sarah@LesserArk:~$ ssh -p 45834 -i .ssh/LesserArkKey 192.168.111.27
ssh: connect to host 192.168.111.27 port 45834: Connection refused
sarah@LesserArk:~$ ssh -p 24 -i .ssh/LesserArkKey 192.168.111.27
ssh: connect to host 192.168.111.27 port 24: Connection refused
sarah@LesserArk:~$ ssh -i .ssh/LesserArkKey 192.168.111.27
ssh: connect to host 192.168.111.27 port 22: Connection refused

netplan 更改后的服务器: 服务器 ifconfig服务器 ifconfig

SSHD 配置:

#   $OpenBSD: sshd_config,v 1.101 2017/03/14 07:19:07 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Port 45834
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
#PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
MaxStartups 2
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
Subsystem   sftp    /usr/lib/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#   X11Forwarding no
#   AllowTcpForwarding no
#   PermitTTY no
#   ForceCommand cvs server

Protocol 2

编辑1: 以下是命令的输出 cat /etc/hosts::

127.0.0.1       localhost.localdomain   localhost
::1             localhost6.localdomain6 localhost6

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

cat /etc/nsswitch.conf | grep "hosts:"hosts: files dns

`networkctl status`:

●        State: routable
       Address: 192.168.111.27 on enp0s25
                fe80::225:64ff:feaf:9fc8 on enp0s25
           DNS: 192.168.1.1
                8.8.8.8

ls -l /etc/resolv.conflrwxrwxrwx 1 root root 39 Feb 14 09:49 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

编辑2:在 /etc/hosts 中:

127.0.0.1       localhost.localdomain   localhost
::1             localhost6.localdomain6 localhost6
192.168.111.27  scilab_comp_0

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

输出hostnamescilab_comp_0

编辑3:我用我的电脑(不是服务器)制作了一个简短的视频,详细地展示了正在发生的事情: https://www.youtube.com/watch?v=rqQGas4fs_A&feature=youtu.be

在此处制作了 IP 地址冲突的简短视频:https://youtu.be/P2rXWvdOM7k 出口

编辑4:输出telnet 192.168.111.27 45834

sarah@scilab_comp_0:~$ telnet 192.168.111.27 45834
Trying 192.168.111.27...
Connected to 192.168.111.27.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
Connection closed by foreign host.

经过进一步挖掘,我发现该服务器有 2 个 IP 地址。如下所示ip a

2: enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:25:64:af:9f:c8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.27/24 brd 192.168.111.255 scope global enp0s25
       valid_lft forever preferred_lft forever
    inet 192.168.1.142/24 brd 192.168.1.255 scope global dynamic enp0s25
       valid_lft 78971sec preferred_lft 78971sec
    inet6 fe80::225:64ff:feaf:9fc8/64 scope link 
       valid_lft forever preferred_lft forever

如您所见,一个是动态的,一个是静态的。但我不确定静态的是否有效,或者如何摆脱动态的(因为我将 yaml 恢复为原始配置,因此我暂时可以从我的计算机远程访问它)。鉴于我们的配置文件指出我们只需要 DCHP,那么静态 IP 地址来自哪里?

另外,我验证了只有50-cloud-init.yaml对配置有影响。我将 .DISABLED 前缀添加到我们创建的另一个 yaml 文件中,因为它似乎没有任何效果。

编辑4:更好的测试

我找到了一种更好的方法来测试服务器是否能够连接。我有两个终端窗口登录到服务器,其中一个只运行一个循环while true; do ip a; ping -c3 google.com; date; sleep 10; done。另一个使用sudo netplan trynetplan 将 IP 地址设置为静态。

这些是结果: - 每次 IP 地址变为静态时(在我在另一个终端上按下“sudo netplan try”的回车键后,ping 失败:

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: enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:25:64:af:9f:c8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.27/24 brd 192.168.111.255 scope global enp0s25
       valid_lft forever preferred_lft forever
    inet6 fe80::225:64ff:feaf:9fc8/64 scope link 
       valid_lft forever preferred_lft forever
ping: google.com: Temporary failure in name resolution

并且每次它返回使用 DCHP IP 地址(之前在它有 2 个 IP 地址时提出)时,它都会返回并将信息报告给我的 PC 的 SSH shell。

答案1

在服务器的静态 IP netplan 配置中,您请求的 IP 地址与网关子网中的 IP 地址不同。尽管 netplan 可以将该 IP 分配给您的设备,但网关将无法与其通信。

要修复此问题,请请求通过 DHCP 分配的同一子网中的 IP 地址。因此,如果 DHCP 分配的地址是192.168.1.15,请像这样设置您的 netplan yaml 文件:

network:
    renderer: networkd
    ethernets:
        enp0s25:
            dhcp4: no
            addresses: [192.168.1.111/24]
            gateway4: 192.168.1.1
            nameservers:
                     addresses: [192.168.1.1,8.8.8.8]

如果您不确定网关 IP,请在通过 DHCP 建立良好连接时发出以下命令:

ip route 

系统将会做出如下响应

 default via 192.168.1.1 dev netdev01 ....

在此输出中,网关通过字段进行识别default via

为了简单起见,并避免通过 DNS 和本地 SSH 配置出现问题,您可以使用服务器的文字 IP 地址发出客户端 SSH 请求:

ssh [email protected]

这是一个备份服务器,因此您与服务器的大部分通信很可能都是脚本化的,因此唯一的功能使用主机名的原因是服务器的 IP 是非常可能会改变。

此外,为简单起见,除非有充分理由不这样做,否则我会将 ssh 密钥生成为默认值~/.ssh/id_rsa.pub。这使得请求的编码更加干净和简单。只有当我想将密钥存储在本地家庭之外时,或者出于某种我无法想象的原因,我想为不同的主机保留不同的密钥时,我才会将公钥放入命名文件中。

相关内容