Running the Linux Command Line on iOS

Running the Linux Command Line on iOS

Did you know that you can run the Linux command line on an iOS device? You might ask, "Why would I want to use text applications on my iPhone?" That's a fair question. But if you're reading Opensource.com, you probably know the answer: Linux users want to be able to work with it on any device and want to use their own settings.

But most of all, they crave solutions to complex tasks.

I have a seven-year-old iPad 2 Mini, which is still quite good for reading e-books and other tasks. However, I want to use it for accessing the command line of applications with my set of programs and scripts that I can’t work without. I need an environment I’m accustomed to, along with my standard development environment. Here’s how I managed to achieve this.

Connecting a Keyboard

Working with the command line for programming via the on-screen keyboard of a phone or tablet is quite inconvenient. I recommend connecting an external keyboard, either via Bluetooth or by using a camera connection adapter for a wired keyboard (I chose the latter). When connecting a split Kinesis Advantage keyboard to an iPhone 6, it turns into a strange device reminiscent of a corporate cyberdeck from a classic role-playing game Shadowrun.

Setting Up the Shell on iOS

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

  • Secure Shell (SSH), connecting to a Linux computer
  • Running a virtual system using Alpine Linux with iSH, which is open source but must be installed using Apple's proprietary TestFlight app

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 restricted option — you're not actually running Linux, but Linux tools. Using these applications comes with strict limitations, however, you do gain partial command line functionality.

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

Option 1: Shell in a 'sandbox'

One of the easiest ways is to install an iOS application LibTerm. This is open source A command shell in a "sandbox" supporting over 80 commands for the price of zero dollars. It comes bundled with Python 2.7, Python 3.7, Lua, C, Clang, and much more.

It has approximately the same functionality as a-Shell, described by its developers as "a test user interface for screen input platforms." The a-Shell source code is available open source, it is actively developed, provides access to the file system, and comes with Lua, Python, Tex, Vim, JavaScript, C, and C++, as well as Clang and Clang++. It even allows you to install Python packages using pip.

Option 2: SSH

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

Blink shell — is a popular paid SSH app on open source. If you ignore the small screen of the device, using this software is similar to connecting to a server through any other command line. The Blink terminal looks great, has many ready-made themes, and features for creating your own, including the ability to customize and add new fonts.

Option 3: Running Linux

Using SSH to connect to a Linux server 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 can't be completely ignored, so you might need to work with Linux without a server.

If this is your case, you'll need to take another step forward. TestFlight — is a proprietary service for installing developed applications before they are released in the Apple App Store. The TestFlight app can be installed from the App Store, and then you can use test applications. Apps in TestFlight allow a limited number of beta testers (usually up to 10,000) to work with them for a limited time. To download a test application, you need to navigate from your device to a link that is usually found on the developer's website of the test application.

Launching Alpine Linux with iSH

iSH — is an open-source TestFlight application that runs a virtual machine with a ready distribution Alpine Linux (with a little effort, other distributions can also be run).

Important feature: the application is experimental. Since iSH is currently a test application, do not expect constant and reliable operation. TestFlight applications are time-limited. My current build will only work for 60 days. This means that after 60 days, I will be excluded and will have to rejoin the next testing phase of iSH. Moreover, I will lose all my files unless I export them using Files on iOS or copy them to a Git host or via SSH. In other words: Do not hope that all this will continue to work! Do not place anything important to you in the system! Make backups in a separate place!

Installing iSH

Start by installing TestFlight from the App Store. Then install iSH, getting an installation link from the application’s website. There is also another installation method using AltStore, but I haven't tried it. Or, if you have a paid developer account, you can download the iSH repository from GitHub and install it yourself.

Using the TestFlight link will install the iSH application 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 less than 5 MB in size. I worked with Alpine for the first time, so I thought that such minimalism would be annoying, but I actually liked it very much.

Running the Linux Command Line on iOS
Alpine uses a package manager apk, which is simpler than even apt or pacman.

How to install a 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

The default text editor in Alpine is Vi, but I prefer Vim, so I installed it:

apk add vim

If desired, you can install Nano or Emacs.

Changing the shell

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

Installing fish:

apk add fish

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

apk add bash bash-doc bash-completion

The minimalist ideology of Alpine usually means that some programs which are a single package in other distributions will be split into several smaller packages. It also means you can customize and reduce the size of the system just the way you want.

You can learn more about installing Bash from this tutorial.

Changing the default shell

After installing fish, you can temporarily switch to it by typing fish and entering the shell. But I want to make fish the default shell, and the command chsh, which I used in other distributions, didn't work.

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

which fish

Here's what I got:

/usr/bin/fish

Next, let's change the login shell to fish. You can use any editor you prefer. If you're a beginner, install Nano (with the command apk add nano), so you can edit configuration files and save them with CTRL+X, confirm, and exit.

But I used Vim:

vim /etc/passwd

My first line looked like this:

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 am sure there is a good way to change the shell path so that it can be used immediately. But I don't know it, so I recommend going back to the application browser, force-logging out of the shell, and for reliability, turn off and restart the iPad or iPhone. Open iSH again and now, besides the message 'Welcome to Alpine!' and the startup information with apk, you will see the standard welcome login message from fish: Welcome to fish, the friendly interactive shell. Hooray!

Running 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 programs in Python. Let's install it:

apk add python3

Although Python 2.x is obsolete, you can install it as well:

apk add python

Let's install a Python package manager called pip and setuptools:

python3 -m ensurepip --default-pip

It will take some time to install and set up the package manager, so just wait.

Then you will be able to download a file transfer tool over the network curl:

apk add curl

Reading manuals

Fish uses built-in autocomplete based on man pages. Like other command line users, I use the manual man, but it is not installed in Alpine. So I installed it with the terminal pager less:

apk add man man-pages less less-doc

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

I installed it using pip:

pip install tldr

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

Of course, all this setup work can be automated with dotfiles or an installation script, but really that doesn't align very well with Alpine’s philosophy — setting up a minimal installation tailored to your specific needs. Besides, this did take so much time, right?

Additional information

In the iSH Wiki, there is a page 'what works' with reports on which packages currently work. By the way, it seems that npm is currently not working.

On another wiki page, it explains how to access iSH files from the iOS Files app. This is one way you can move and copy files.

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

Learn more about iSH from these links:

Advertising

VDSina offers virtual servers on Linux or Windows. We use only branded equipment, the best-in-class proprietary server management panel, and some of the best data centers in Russia and the EU. Hurry to order!

Running the Linux Command Line on iOS

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster