Launching the Linux command line on iOS

Launching the Linux command line on iOS

Did you know that you can run a Linux command line on an iOS device? You might be asking, “Why should I use text apps on iPhone?” Fair question. But if you're reading Opensource.com, you probably know the answer: Linux users want to be able to use it on any device and want to use their own settings.

But most of all they crave solving complex problems.

I have a seven year old iPad 2 Mini which is still good for ebook reading and other tasks. However, I also want to use it to access the command line of applications with my set of programs and scripts, without which I cannot work. I need an environment that I'm used to, as well as my standard development environment. And here is how I managed to achieve this.

Connecting to a keyboard

Working with the command line for programming through the on-screen keyboard of a phone or tablet is quite inconvenient. I recommend connecting an external keyboard, either via Bluetooth or using a camera connection adapter to connect a wired keyboard (I chose the latter). Connecting a Kinesis Advantage split keyboard to an iPhone 6 results in a weird device that looks like corporate cyberdeck from classical role play shadowrun.

Installing the shell on iOS

To run a fully functional Linux system on iOS, there are two options:

  • Secure shell (SSH) connected to a Linux machine
  • Running a virtual system using Alpine Linux with iSH, which is open source but must be installed using Apple's proprietary TestFlight application

Alternatively, there are two open source terminal emulator applications that provide the ability to work with open-source tools in a limited environment. This is the most stripped-down option - in fact, this is not how you run Linux, but Linux tools. There are severe limitations when working with these applications, but you get partial command line functionality.

Before moving on to complex solutions, I will consider the simplest way.

Option 1: Shell in the Sandbox

One of the easiest ways is to install the iOS app LibTerm. It open source sandbox command shell with support for over 80 commands at zero dollars. It comes with Python 2.7, Python 3.7, Lua, C, Clang and more.

Approximately the same functionality a-shell, described by the developers as "a test user interface for a platform with screen input." a-Shell sources posted open source, it's under active development, provides filesystem access, and ships with Lua, Python, Tex, Vim, JavaScript, C and C++, and Clang and Clang++. It even allows you to install Python packages with pip.

Option 2: SSH

Another step beyond downloading an app is setting up an SSH client. For a long time, we have been able to use any of the many iOS SSH client applications to connect to a server running Linux or BSD. The advantage of using SSH is that any distribution can run on the server with any software. You work remotely and the results of your work are simply transferred to the terminal emulator on your iOS device.

blink shell is a popular paid SSH application in open source. If you do not pay attention to the small screen of the device, then using this software is like connecting to the server through any other command line. The Blink terminal looks great, has many ready-made themes and the ability to create your own, including the ability to customize and add new fonts.

Option 3: Run Linux

Using SSH to connect to a server under Linux is a great way to access the command line, but it requires an external server and a network connection. This is not the biggest obstacle, but it cannot be completely ignored, so you may need to work with Linux without a server.

If this is your case, then you will need to take it one step further. TestFlight is a proprietary service for installing applications under development before they are released to the Apple App Store. You can install the TestFlight app from the App Store and then use the test apps. Applications in TestFlight allow a limited number of beta testers (typically up to 10) to work with them for a limited time. To download the test app, you need to access the link on your device, which is usually found on the test app developer's website.

Running Alpine Linux with iSH

ISH is an open-source TestFlight application that runs a virtual machine with a ready-made distribution Alpine Linux (with a little work, you can run other distributions).

Important feature: application experimental. Since iSH is currently a test application, don't expect consistent and reliable performance. TestFlight applications are time limited. My current build will only run 60 days. This means that after 60 days I will be expelled and will have to rejoin the next phase of iSH testing. Moreover, I will lose all my files if I don't export them with Files on iOS or copy them to a Git host or via SSH. In other words: Don't expect it all to continue to work! Don't put anything important to you into the system! Back up to a separate location!

iSH Installation

Start with installation TestFlight from the App Store. Then install iSH, getting a link to install from the application website. There is another way to install using AltStore, but I have not tried it. Or, if you have a paid developer account, you can download the iSH repository from GitHub and install it yourself.

Using the link, TestFlight will install the iSH app on your device. As with any other application, an icon will appear on the screen.

Package management

iSH runs an x86 emulator with Alpine Linux. Alpine is a tiny distribution that is less than 5MB in size. This was my first time working with Alpine, so I thought the minimalism would be annoying, but I really liked it.

Launching the Linux command line on iOS
Alpine uses a package manager apkwhich is easier than even apt or pacman.

How to install the package:

apk add package

How to remove a package:

apk del package

How to find out other commands and information:

apk --help

Package manager update:

apk update
apk upgrade

Installing a text editor

Alpine's default text editor is Vi, but I prefer Vim, so I installed it:

apk add vim

Optionally, you can install Nano or Emacs.

Shell change

I don't know about you, but I needed fish shell. Other people prefer Bash or zsh. However, Alpine uses ash! Ash is a fork of the Dash shell, which is itself a fork of the original ash, or Almquist shell. Her priority is speed. I decided to trade speed for the built-in auto-completion, colors, Vim key management, and syntax highlighting that I love and know from the fish shell.

fish installation:

apk add fish

If you need Bash with its autocompletion and man pages, then install them:

apk add bash bash-doc bash-completion

Alpine's minimalist ideology usually means that some programs that are a single package on other distributions will be split into several smaller packages. It also means that you can tune and downsize the system just the way you want it.

For more information on installing Bash, see this tutorial.

Changing the default shell

Once fish is installed, you can temporarily switch to it by typing fish and going to the shell. But I want to make fish the default shell, and the command chsh, which I used in other distributions, did not work.

First, let's find out where fish is installed:

which fish

Here's what happened to me:

/usr/bin/fish

Next, change the login shell to fish. You can use any editor you like. If you are a beginner, then install Nano (with the command apk add nano) so that you can edit the configuration files and save them via CTRL+X, confirm and exit.

But I used Vim:

vim /etc/passwd

My first line was:

root:x:0:0:root:/root:/bin/ash

To make fish the default shell, change this line to the following:

root:x:0:0:root:/root:/usr/bin/fish

Then save the file and exit.

I'm sure there is a good way to change the shell path so that it can be used right away. But I don’t know him, so I recommend returning to the application browser, force exiting the shell, and to be sure, turn off and restart the iPad or iPhone. Open iSH again and now, in addition to the message “Welcome to Alpine!” and information about launching from the apk, you will see the standard fish login welcome message: Welcome to fish, the friendly interactive shell... Hooray!

Launching the Linux command line on iOS

Setting up Python and pip

I decided to add Python (version 3.x), not only to write code, but also because I use several Python programs. Let's install it:

apk add python3

Although Python 2.x is outdated, you can install it too:

apk add python

Install a Python package manager called pip and setuptools:

python3 -m ensurepip --default-pip

It will take some time to install and configure the package manager, so just be patient.

You can then download a tool to transfer files over the network curl:

apk add curl

Reading manuals

Fish uses built-in autocompletion based on man pages. Like other command line users, I use the manual man, and Alpine does not have it installed. So I installed it with a terminal pager moins:

apk add man man-pages less less-doc

In addition to man, I use the magnificent tldr pages project, which provides simplified and community-driven man pages.

I installed it with pip:

pip install tldr

Team tldr connects to the web to fetch pages when it encounters a request for a new page. If you need to know how to use a command, you can write something like tldr curl and get a description in plain English and good examples on how to use the command.

Of course, all this installation work can be automated using dotfiles or an installation script, but in fact this is not very consistent with Alpine's ideology - customizing the minimum installation clearly to your needs. Besides, it took so long, didn't it?

Additional Information

The iSH Wiki has a page "what works" with reports on which packages are currently running. By the way, it looks like npm is not working right now.

Another wiki page explains how access iSH files from the iOS Files app. This is one of the ways you can move and copy files.

You can also install Git (yes! apk add git ) and push your work to a remote repository or push it to the server via SSH. And, of course, you can download and run any number of great open-source projects from GitHub.

More information about iSH can be found at these links:

As advertising

Vdsina offers virtual servers on Linux or Windows. We use exclusively branded equipment, the best of its kind in-house server control panel and one of the best data centers in Russia and the EU. Hurry up to order!

Launching the Linux command line on iOS

Source: habr.com

Add a comment