use SSH to connect to Raspberry Pi methods All In One
use SSH to connect to Raspberry Pi methods All In One
Raspberry Pi
user@hostname
# 无需知道 Raspberry Pi 的 IP Address, 即可使用 SSH 连接 👍
$ ssh pi@raspberrypi.local
$ ssh eric@raspberrypi.local
# username@hostname ✅
$ ssh eric@rpi4b.local
https://www.cnblogs.com/xgqfrms/p/17482487.html
user@hostip
# 需要使用 Wi-Fi Router / Nmap 等扫描工具获取 IP Address 👎
$ ssh pi@192.168.18.135
hostname
pi@raspberrypi:~ $ hostname -h
Usage: hostname [-b] {hostname|-F file} set host name (from file)
hostname [-a|-A|-d|-f|-i|-I|-s|-y] display formatted name
hostname display host name
{yp,nis,}domainname {nisdomain|-F file} set NIS domain name (from file)
{yp,nis,}domainname display NIS domain name
dnsdomainname display dns domain name
hostname -V|--version|-h|--help print info and exit
Program name:
{yp,nis,}domainname=hostname -y
dnsdomainname=hostname -d
Program options:
-a, --alias alias names
-A, --all-fqdns all long host names (FQDNs)
-b, --boot set default hostname if none available
-d, --domain DNS domain name
-f, --fqdn, --long long host name (FQDN)
-F, --file read host name or NIS domain name from given file
-i, --ip-address addresses for the host name
-I, --all-ip-addresses all addresses for the host
-s, --short short host name
-y, --yp, --nis NIS/YP domain name
Description:
This command can get or set the host name or the NIS domain name. You can
also get the DNS domain or the FQDN (fully qualified domain name).
Unless you are using bind or NIS for host lookups you can change the
FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
part of the FQDN) in the /etc/hosts file.
pi@raspberrypi:~ $ hostname -I
192.168.18.135 fd80:eae6:1258:0:d1c5:2a7f:abad:cbb5
pi@raspberrypi:~ $ hostname -i
127.0.1.1
pi@raspberrypi:~ $ hostname
raspberrypi
pi@raspberrypi:~ $ hostname -d
pi@raspberrypi:~ $ hostname -a
pi@raspberrypi:~ $
# macOS
➜ make git:(main) hostname
xgqfrms-mm.local
$ hostname -d
local
$ hostname -s
xgqfrms-mm
$ hostname -f
xgqfrms-mm.local
➜ make git:(main) man hostname
➜ make git:(main) hostname -h
hostname: illegal option -- h
usage: hostname [-f] [-s | -d] [name-of-host]
HOSTNAME(1) General Commands Manual HOSTNAME(1)
NAME
hostname – set or print name of current host system
SYNOPSIS
hostname [-f] [-s | -d] [name-of-host]
DESCRIPTION
The hostname utility prints the name of the current host.
The super-user can set the hostname by supplying an argument.
To keep the hostname between reboots, run ‘scutil --set HostName name-of-host’.
Options:
-f Include domain information in the printed name. This is the default behavior.
-s Trim off any domain information from the printed name.
-d Only print domain information.
EXAMPLES
Set the host name of the machine and check the result:
$ hostname beastie.localdomain.org
$ hostname
beastie.localdomain.org
Do not show domain information:
$ hostname -s
beastie
Show only domain information:
$ hostname -d
localdomain.org
SEE ALSO
gethostname(3), scutil(8)
HISTORY
The hostname command appeared in 4.2BSD.
macOS 13.1 October 5, 2020 macOS 13.1
errors ❌
# fix hostname domain lost error ❌
$ ssh pi@raspberrypi.local
$ sudo vim /etc/hosts
$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1 raspberrypi
# .local 不好使❌
# 192.168.18.135 raspberrypi.local
# github
20.205.243.166 github.com
185.199.108.133 raw.githubusercontent.com
solutions ✅
mDNS
multicast 组播
Resolving raspberrypi.local
with mDNS
On Raspberry Pi OS, multicast DNS
is supported out-of-the-box by the Avahi
service.
https://www.raspberrypi.com/documentation/computers/remote-access.html#ip-address
ip6-localne
t &ip6-mcastprefix
ff00::0 ip6-localnet
ff00::0 ip6-mcastprefix
丢失了
ip6-localnet
&ip6-mcastprefix
✅
$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
# fix 丢失 hostname.local ✅
ff00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1 raspberrypi
# github
20.205.243.166 github.com
185.199.108.133 raw.githubusercontent.com
Avahi
$ sudo apt-get update
$ sudo apt-get upgrade
# mDNS
$ sudo apt-get install avahi-daemon
https://github.com/lathiat/avahi/issues
bonjour
https://www.apple.com/macosx/features/bonjour/
$ avahi-daemon --help
avahi-daemon [options]
-h --help Show this help
-D --daemonize Daemonize after startup (implies -s)
-s --syslog Write log messages to syslog(3) instead of STDERR
-k --kill Kill a running daemon
-r --reload Request a running daemon to reload static services
-c --check Return 0 if a daemon is already running
-V --version Show version
-f --file=FILE Load the specified configuration file instead of
/etc/avahi/avahi-daemon.conf
--no-rlimits Don't enforce resource limits
--no-drop-root Don't drop privileges
--no-chroot Don't chroot()
--no-proc-title Don't modify process title
--debug Increase verbosity
$ cat /etc/avahi/avahi-daemon.conf
# This file is part of avahi.
#
# avahi is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# avahi is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with avahi; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
# See avahi-daemon.conf(5) for more information on this configuration
# file!
[server]
#host-name=foo
#domain-name=local
#browse-domains=0pointer.de, zeroconf.org
use-ipv4=yes
use-ipv6=yes
#allow-interfaces=eth0
#deny-interfaces=eth1
#check-response-ttl=no
#use-iff-running=no
#enable-dbus=yes
#disallow-other-stacks=no
#allow-point-to-point=no
#cache-entries-max=4096
#clients-max=4096
#objects-per-client-max=1024
#entries-per-entry-group-max=32
ratelimit-interval-usec=1000000
ratelimit-burst=1000
[wide-area]
enable-wide-area=yes
[publish]
#disable-publishing=no
#disable-user-service-publishing=no
#add-service-cookie=no
#publish-addresses=yes
publish-hinfo=no
publish-workstation=no
#publish-domain=yes
#publish-dns-servers=192.168.50.1, 192.168.50.2
#publish-resolv-conf-dns-servers=yes
#publish-aaaa-on-ipv4=yes
#publish-a-on-ipv6=no
[reflector]
#enable-reflector=no
#reflect-ipv=no
#reflect-filters=_airplay._tcp.local,_raop._tcp.local
[rlimits]
#rlimit-as=
#rlimit-core=0
#rlimit-data=8388608
#rlimit-fsize=0
#rlimit-nofile=768
#rlimit-stack=8388608
#rlimit-nproc=3
开启 hostname 到 domain-name 的映射关系 ❓
[server]
host-name=raspberrypi
domain-name=local
#browse-domains=0pointer.de, zeroconf.org
.local
pi@raspberrypi:~ $ cat .local
cat: .local: 是一个目录
pi@raspberrypi:~ $ ls .local
lib share
pi@raspberrypi:~ $ ls -alth .local
总用量 16K
drwxr-xr-x 22 pi pi 4.0K 5月 23 22:59 ..
drwxr-xr-x 5 pi pi 4.0K 5月 3 17:45 share
drwxr-xr-x 4 pi pi 4.0K 4月 12 23:59 .
drwxr-xr-x 3 pi pi 4.0K 4月 12 23:59 lib
pi@raspberrypi:~ $
demos
Raspberry Pi change hostname
eric@rpi4b:~ $ sudo raspi-config
eric@rpi4b:~ $ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
# fix hostname lost ❓
ff00::0 ip6-localnet
ff00::0 ip6-mcastprefix
#
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1 rpi4b
# fix github 443 ❌
185.199.108.133 raw.githubusercontent.com
# fix: git repo
20.205.243.166 github.com
164.90.255.4 install.pi-hole.net
eric@rpi4b:~ $ cat /etc/hostname
rpi4b
eric@rpi4b:~ $ hostname -I
192.168.18.168
https://www.tomshardware.com/how-to/raspberry-pi-change-hostname
refs
https://www.raspberrypi.com/documentation/computers/remote-access.html
https://www.howtogeek.com/167190/how-and-why-to-assign-the-.local-domain-to-your-raspberry-pi/
https://www.howtogeek.com/167195/how-to-change-your-raspberry-pi-or-other-linux-devices-hostname/
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17426433.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2022-05-23 SwiftUI App 适配 iPad All In One
2022-05-23 js Number literal All In One
2020-05-23 C++ 语言从入门到放弃 All In One
2020-05-23 Chrome DevTools & performance & keywords
2019-05-23 chrome devtools dark mode
2019-05-23 how to write a node cli tool
2019-05-23 SEO & JSON-LD & structured-data