4

I am trying to find a way to determine the user name of the current shell user from within a shell / terminal on an Android device.

The Android environment that I have in /system/bin and /system/xbin does not have the unix user command. Overall, it appears that the concept doesn't quite exist on Android, but at least, on my rooted device, there is a user root and also there are groups such as shell and everybody, based on the permissions of files on the system.

The background is the following:
I want to run an ssh command (I installed a version of OpenSSH also) on the Android that uses a local private key. The key has file permissions and those are required to be restrictive by OpenSSH for good reason (the UNIX group of the file should not have permissions to access the key, only the owner).

Since I know only the user root, I can set the owner of the key to root and run ssh as root. However, I don't really want to run ssh as root.

I am issuing the shell command from within Automate which has that option. Apparently, it's possible to execute the command without root permissions. However, if I add the key file to the ssh command, it can't get access to the file that is accessible only by root. Thus I would like to set the owner in the UNIX file permissions of the key to the shell user used by Automate, but I do not know how to find out who that is.

EDIT:

~# grep automate /data/system/packages.list
com.llamalab.automate 10106 0 /data/user/0/com.llamalab.automate default:targetSdkVersion=24 3002,3003,3001

I assume 10106 is the UID, but is that a static value? If yes, could I add a line to /etc/passwd by using adduser command from busybox or Termux?

0

1 Answer 1

3

Android inherits from Linux the concept of shell, which is the core component of Linux login process. But there is no concept of Linux console login on Android (1) because it boots directly to GUI. However it makes use of Discretionary Access Control (DAC) of Linux kernel which is based on UIDs / GIDs and permission mode. Every installed app is considers as a *NIX user and is assigned a unique UID at install time. See this answer for more details on this.
You can get UID of Automate app from file /data/system/packages.list or by executing id -u from within app:

~# awk '/^com.llamalab.automate/ {print $2}' /data/system/packages.list
10106

Since you want a remote login from local machine using a ssh binary (built from standard Linux source) which is to be executed by Automate app i.e. with UID 10106, the key file must also be owned by UID 10106. The only place on device which is owned by UID 10106 is the directory /data/data/com.llamalab.automate, so this should be set as $HOME for ssh and where the key should be placed under directory .ssh owned by UID/GID 10106:

~# mkdir /data/data/com.llamalab.automate/.ssh
~# ssh-keygen -f /data/data/com.llamalab.automate/.ssh/id_rsa
~# chmod 0700 /data/data/com.llamalab.automate/.ssh
~# chown -R 10106.10106 /data/data/com.llamalab.automate/.ssh

Create file /etc/passwd with a single line: user:x:10106:10106::/data/data/com.llamalab.automate:/system/bin/sh where user is any username of your choice. You can also use adduser:

~# touch /etc/passwd
~# busybox adduser -D -H -h /data/data/com.llamalab.automate -s /system/bin/sh -u 10106 user

Also add -o StrictHostKeyChecking=no to ssh command so that new server is added to known_hosts without user interaction.

However if the ssh binary is built specifically for Android to be used with an app, it must have its $HOME already hard-coded which can't be changed using /etc/passwd. In this case you can try something like ssh -i <path/to/key> -o UserKnownHostsFile=<path/to/known_hosts> <user>@<IP>. Permissions must be set and both paths must be accessible by executing app.

PS:

Please note that UID 10106 must be member of group aid_inet (GID 3003) for ssh to be able to make connections. All apps with android.permission.INTERNET are member of this group. Or you can add a line to /etc/group: internet:x:3003:user. Or using addgroup:

~# touch /etc/group
~# busybox addgroup -g 3003 internet
~# busybox addgroup user internet

However this is useful only for programs which perform login (setgroups()) like su, sshd etc.

For more details on how Android lacks Linux's login mechanism, see this answer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .