注:本文为 “Headless Linux” 相关文章合辑。
机翻未校。
How to Install and Set Up Headless Linux Server
如何安装和设置无头 Linux 服务器
Winnie Ondara Last Updated: January 31, 2023
A vast majority of Linux users are familiar with a Linux desktop PC which provides a graphical environment with which you can interact with the system. However, unlike a Linux desktop, a headless server is an operating system that runs without a GUI (Graphical User Environment).
大多数 Linux 用户都熟悉带有图形界面的 Linux 桌面电脑,用户可以通过图形环境与系统进行交互。然而,与 Linux 桌面不同,无头服务器是一种没有图形用户界面的操作系统。
It is installed on hardware that is not connected to any peripheral device such as a monitor, keyboard, or mouse during its operation (although these are required during the initial setup).
在运行过程中,它安装在未连接任何外设(如显示器、键盘或鼠标)的硬件上(尽管在初始设置期间需要这些设备)。
In most cases, headless servers are remotely accessed and administered on the command line via the SSH protocol using SSH clients.
在大多数情况下,无头服务器通过 SSH 协议使用 SSH 客户端 在命令行上进行远程访问和管理。
The main goal of headless servers is to provide access to crucial services such as web and database services to clients or users. Headless servers are mostly found in data centers and run enterprise or production workloads.
无头服务器的主要目标是为客户端或用户提供对关键服务(如 Web 和数据库服务)的访问权限。无头服务器主要出现在数据中心,用于运行企业或生产工作负载。
Pros of a Headless Linux System
无头Linux系统的优点
Graphical installations are usually associated with high resource overhead. Applications such as the Firefox browser, and GIMP are resource-intensive programs and end up hogging lots of system memory and CPU.
图形安装通常与高资源开销相关联。像 Firefox 浏览器和 GIMP 这样的应用程序是资源密集型程序,会占用大量系统内存和 CPU。
This impacts the performance of critical production workloads or applications which have to make do with the few resources that are available.
这会影响关键生产工作负载或应用程序的性能,因为它们只能使用有限的资源。
Since there are no graphical components on headless servers, most of the resources are accorded to critical applications which results in optimized performance.
由于无头服务器上没有图形组件,大多数资源都被分配给关键应用程序,从而实现了优化的性能。
Headless servers ensure critical enterprise workloads run smoothly since there is no competition for system resources with other non-critical applications. It’s, therefore, efficient to host your applications on a headless server since there are no fancy graphical applications that will impact performance.
无头服务器确保关键企业工作负载顺利运行,因为它们不会与其他非关键应用程序争夺系统资源。因此,在无头服务器上托管应用程序是高效的,因为没有华丽的图形应用程序会影响性能。
Installation of a Headless Linux Server
安装无头 Linux 服务器
The installation of a headless Linux server is similar to installing a Linux OS with a graphical environment. Most Linux distributions such as Ubuntu, Rocky Linux, AlmaLinux, and Debian provide a minimal ISO installation image which has a small memory footprint and provides only the essential tools to install the operating system.
无头 Linux 服务器的安装过程与安装带有图形环境的 Linux 操作系统类似。大多数 Linux 发行版,如 Ubuntu、Rocky Linux、AlmaLinux 和 Debian,都提供了一个最小化的 ISO 安装镜像,该镜像占用内存小,仅提供安装操作系统的基本工具。
This is different from a full DVD ISO which is usually quite large – mostly gigabytes in memory – and provides all the graphical tools and utilities.
这与通常很大的完整 DVD ISO 不同,后者通常占用数 GB 的内存,并提供所有的图形工具和实用程序。
Download Linux ISO Image
下载 Linux ISO 映像
For this guide, we will demonstrate how you can get started with installing the Debian 11 server (Debian Bullseye). So, head over to the Debian Download Page and grab the net installer ISO which is roughly 400MB in size.
在本指南中,我们将展示如何开始安装 Debian 11 服务器(Debian Bullseye)。因此,请前往 Debian 下载页面,下载大约 400MB 的网络安装程序 ISO。
Also, note you can use any Linux distribution ISO image to set up a headless server.
请注意,您可以使用任何 Linux 发行版的 ISO 镜像来设置无头服务器。
Create a Bootable Linux USB Drive
创建可启动的 Linux USB 驱动器
Once you have downloaded the ISO image, the next step is to burn the ISO to a USB drive which will be used as the bootable medium.
下载 ISO 镜像后,下一步是将 ISO 镜像刻录到 USB 驱动器,该驱动器将用作可引导介质。
You can create a LiveUSB medium using USB creator tools or you can also do this on the command line using the dd command as shown.
您可以使用 USB 创建工具 创建 LiveUSB 介质,也可以使用命令行中的 dd 命令来完成此操作。
$ dd if=~/Downloads/debian-11.6.0-amd64-netinst.iso of=/dev/sdx bs=4M status=progress
Installing Linux Headless Server Using USB Drive
使用 USB 驱动器安装 Linux 无头服务器
With the bootable USB drive in place, plug it into the bare metal server which should, at first, be connected to a monitor and keyboard for the initial setup.
准备好可引导 USB 驱动器后,将其插入裸金属服务器,该服务器最初应连接显示器和键盘以进行初始设置。
Power on the server and navigate to the BIOS settings. Ensure that the USB drive is first on the boot order priority. Save and changes and continue booting.
打开服务器电源并进入 BIOS 设置。确保 USB 驱动器是启动顺序中的第一优先级。保存更改并继续启动。
Once the system powers on, you will be presented with a list of menu options. Here, we will go with Graphical Install which provides a graphical installation method to make work easier.
系统启动后,您将看到一个菜单选项列表。在这里,我们将选择 图形安装,它提供了一种图形化安装方法,使工作更轻松。
Debian 图形安装
The next steps will require you to make a couple of selections. These include choosing the installation language, timezone, keyboard layout, hostname, creating a regular user account and root account as well as partitioning the hard drive.
接下来的步骤需要您进行一些选择,包括选择安装语言、时区、键盘布局、主机名、创建普通用户账户和根账户,以及分区硬盘。
Lastly, you will be presented with a ‘Software Selection’ step that provides a wide array of desktop environments to choose from.
最后,您将看到一个“软件选择”步骤,提供多种桌面环境供您选择。
But since our mission is to install a headless server, we will only select the ‘SSH Server’ and ‘Standard system utilities’ options and click ‘Continue’. This ensures that we only install the necessary packages required for the headless server to run.
但由于我们的目标是安装无头服务器,因此我们只会选择“SSH 服务器”和“标准系统工具”选项,然后点击“继续”。这确保我们只安装无头服务器运行所需的必要软件包。
Debian 软件选择
From here onwards, proceed with the Debian installation till the end and reboot the system.
从这里开始,继续完成 Debian 的安装,直到最后重启系统。
Debian Post-Installation
Debian 安装后
Upon rebooting your server you will see the following screen. The first option is already pre-select, so simply press ENTER.
重新启动服务器后,您将看到以下屏幕。第一个选项已经被预选,因此只需按 ENTER。
启动 Debian Linux
On the login screen, provide your username and password and press ENTER on both occasions to access the system. Evidently, there’s no graphical environment, and going forward, access to the system will be on the command line via the SSH protocol.
在登录屏幕上,输入用户名和密码,然后两次按 ENTER 以访问系统。显然,没有图形环境,今后将通过 SSH 协议在命令行上访问系统。
Debian 登录
Testing Remote Connection to Headless Server
测试与无头服务器的远程连接
Speaking of SSH, we need to confirm if the service is running. To verify this, switch to the root user
说到 SSH,我们需要确认服务是否正在运行。为此,请切换到 root 用户
$ su –
Then run the following command to check the running status of SSH.
然后运行以下命令以检查 SSH 的运行状态。
# systemctl status sshd
The following output confirms that the service is running. Be sure to enable the service to autostart on boot.
以下输出确认服务正在运行。请确保启用该服务以在启动时自动启动。
# systemctl enable sshd
检查 SSH 服务器
To test remote connectivity to the server, first, check its IP address by running the ip command:
要测试对服务器的远程连接,请首先通过运行 ip 命令来检查其 IP 地址:
# ip a
Locate your active interface – in this case enp0s3 – and check the address prefixed by the ‘inet’ directive. In our case, the IP of our server is 192.168.2.113.
找到您的活动接口(在本例中为 enp0s3),并检查以“inet”开头的地址。在我们的例子中,服务器的 IP 地址是 192.168.2.113。
Next, head over to a remote PC within the LAN and try to ping the server. If you get positive ping results, this implies that the server is reachable.
接下来,在局域网内的远程计算机上尝试 ping 服务器。如果收到积极的 ping 结果,这意味着服务器是可达的。
Finally, you can use an SSH client like PuTTY to access and login into the server.
最后,您可以使用 SSH 客户端,如 PuTTY,来访问并登录服务器。
SSH 连接到远程无头服务器
Perfect! This confirms that we have successfully installed and set up a Headless Linux system. At this point, you can now disconnect the monitor, keyboard, and mouse since the server will now be accessed and managed remotely via the SSH service.
完美!这证实我们已成功安装并设置了一个无头 Linux 系统。此时,您可以断开显示器、键盘和鼠标,因为服务器将通过 SSH 服务进行远程访问和管理。
Conclusion
结论
In this guide, we have demonstrated how you can go about setting up a headless Linux system using Debian 11. The procedure for accomplishing the same feat is similar in other distributions, of course with slight variations here and there.
在本指南中,我们展示了如何使用 Debian 11 设置无头 Linux 系统。在其他发行版中完成相同任务的步骤大致相似,当然会有一些细微的差异。
But rest assured, the minimal ISO provided by a Linux distribution is all you need to successfully install and set up a headless server.
但请放心,Linux 发行版提供的最小化 ISO 镜像足以成功安装并设置无头服务器。
在 ubuntu 服务器上使用 Chrome Headless
Shuangquan IP 属地:湖北 2017.08.24 12:08:31
Install Google Chrome
几个月前发布的 Chrome 59 beta 推出了 headless 模式。原生的 Chrome,更好的通用性,更快的速度…… 似乎是时候和 Phantomjs、Ghost 们说再见了。搜索一圈发现大多数人都是在桌面版的 linux 或者 mac 上尝了鲜,然而要将 Chrome 用于 web2.0 爬虫的话,还是得在服务器版的 linux 中运行。
安装 chrome
测试环境: Ubuntu 16.04 如果是桌面版的 ubuntu,直接到官网下载最新版 chrome 安装就好。对于服务器版的 chrome,只能用命令行安装服务器版本 Chrome _
sudo apt-get install libxss1 libappindicator1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb # Might show "errors", fixed by next line
sudo apt-get install -f
测试
启动 chrome
google-chrome –headless –remote-debugging-port=9222 https://chromium.org –disable-gpu
这里是使用 headless 模式进行远程调试,ubuntu 上大多没有 gpu,所以 –disable-gpu 以免报错。之后使用另一个命令行访问本地的 9222 端口:
curl http://localhost:9222
能够看到调试信息应该就是装好了。
下载 chromedriver
chromedriver 提供了操作 chrome 的 api,是 selenium 控制 chrome 的桥梁。[下载链接] https://sites.google.com/a/chromium.org/chromedriver/downloads] 查看最新的 Chrome 版本。下载并解压:
wget https://chromedriver.storage.googleapis.com/2.31/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
运行 这里我们直接用 selenium 来控制 chrome 在 headless 模式下运行:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('–headless')
chrome_options.add_argument('–disable-gpu')
client = webdriver.Chrome(chrome_options=chrome_options, executable_path='/home/chromedriver') # 如果没有把 chromedriver 加入到 PATH 中,就需要指明路径
client.get("https://jiayi.space") # 替换为实际的 URL
content = client.page_source.encode('utf-8')
print(content)
client.quit()
当然这里是打印出了页面的内容。之后我找了一个以前爬过的网站来试试,它做了 js 加密重定向,而且检测到 phantomjs 请求直接丢弃。结果 Chrome headless 成功拿到渲染后的页面。基于简单的测试后,感觉一切都还很完美。还不知道在生产环境下有没有什么 bug,遇到再更新咯。
最后编辑于 :2018.06.05 11:02:44
via:
-
How to Install and Set Up Headless Linux Server https://www.tecmint.com/setup-headless-linux-server/
-
在 ubuntu 服务器上使用 Chrome Headless – 简书 https://www.jianshu.com/p/4ede64b7ccdb
评论前必须登录!
注册