Android 为什么我在使用 adb 时访问数据文件夹被拒绝?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1043322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:37:53  来源:igfitidea点击:

Why do I get access denied to data folder when using adb?

androidadb

提问by gregm

I connected to my live device using the adb and the following commands:

我使用 adb 和以下命令连接到我的实时设备:

C:\>adb -s HT829GZ52000 shell
$ ls
ls
sqlite_stmt_journals
cache
sdcard
etc
system
sys
sbin
proc
logo.rle
init.trout.rc
init.rc
init.goldfish.rc
init
default.prop
data
root
dev
$ cd data
cd data
$ ls
ls
opendir failed, Permission denied

I was surprised to see that I have access denied. How come I can't browse around the directories using the commandline like this?

我很惊讶地看到我的访问被拒绝。为什么我不能像这样使用命令行浏览目录?

How do I get root access on my phone?

如何在我的手机上获得 root 访问权限?

采纳答案by Reto Meier

There are two things to remember if you want to browse everything on your device.

如果您想浏览设备上的所有内容,请记住两件事。

  1. You need to have a phone with root access in order to browse the data folder on an Android phone. That means either you have a developer device (ADP1or an IONfrom Google I/O) or you've found a way to 'root' your phone some other way.
  2. You need to be running ADB in root mode, do this by executing: adb root
  1. 您需要拥有具有 root 访问权限的手机才能浏览 Android 手机上的数据文件夹。这意味着要么您拥有开发者设备(ADP1或Google I/O的ION),要么您已经找到了以其他方式“root”您的手机的方法。
  2. 你需要在 root 模式下运行 ADB,通过执行: adb root

回答by Idolon

Starting from API level 8 (Android 2.2), for the debuggable application (the one built by Android Studio all the times unless the release build was requested), you can use the shell run-ascommand to run a command or executable as a specific user/application or just switch to the UIDof your application so you can access its datadirectory.

从 API 级别 8(Android 2.2)开始,对于可调试应用程序(除非请求发布版本,否则一直由 Android Studio 构建的应用程序),您可以使用 shellrun-as命令以特定用户/应用程序的身份运行命令或可执行文件或者只是切换到您的应用程序的UID,以便您可以访问其数据目录。

List directory content of yourapp:

列出您的应用程序的目录内容:

run-as com.yourapp ls -l /data/data/com.yourapp

Switch to UID of com.yourappand run all further commands using that uid (until you call exit):

切换到com.yourapp 的UID并使用该 uid 运行所有进一步的命令(直到您调用exit):

run-as com.yourapp
cd /data/data/com.yourapp
ls -l
exit

 
Note 1: there is a known issuewith some HTC Desire phones. Because of a non-standard owner/permissions of the /data/datadirectory, run-ascommand fails to run on those phones.

 
注 1:某些 HTC Desire 手机存在一个已知问题。由于/data/data目录的非标准所有者/权限,run-as命令无法在这些手机上运行。

Note 2: As pointed in the comments by @Avio: run-ashas issues also with Samsung Galaxy S phones running Cyanogenmod at any version (from 7 to 10.1) because on this platform /data/datais a symlink to /datadata. One way to solve the issue is to replace the symlink with the actual directory (unfortunately this usually requires root access).

注 2:正如@Avio 在评论中指出的那样: run-as在任何版本(从 7 到 10.1)上运行 Cyanogenmod 的三星 Galaxy S 手机也存在问题,因为在这个平台上/data/data是到/datadata. 解决问题的一种方法是用实际目录替换符号链接(不幸的是,这通常需要 root 访问权限)。

回答by abbood

before we start,do you have a rootedphone? if not, I strongly suggest that it's time you make the jump. 99% of the tutorials that help you to do this require that you have a rooted phone (I know b/c I spent about an hour searching for a way to do it without having a rooted phone.. couldn't find any..) also if you think about it, your iPhone also has to be rooted to do this same task. So it's totally reasonable. More about rooting at end of answer.

在我们开始之前,你有一个root 的手机吗?如果没有,我强烈建议您是时候跳槽了。99% 的教程可以帮助你做到这一点,要求你有一个有 root 权限的手机(我知道 b/c 我花了大约一个小时寻找一种没有 root 手机的方法......找不到任何...... ) 此外,如果您考虑一下,您的 iPhone 也必须扎根才能执行相同的任务。所以这是完全合理的。有关在答案末尾生根的更多信息。

from your command line type:

从您的命令行输入:

adb shell

this takes you to your android shell comand line (you should see something like this: shell@android:/ $now type:

这会将您带到您的 android shell 命令行(您应该看到如下内容:shell@android:/ $现在输入:

shell@android:/ $run-as com.domain.yourapp

this should take you directly to the data directory of com.domain.yourapp:

这应该会将您直接带到以下数据目录com.domain.yourapp

shell@android:/data/data/com.domain.yourapp $ 

if it doesn't (ie if you get an error) then you probably don't have a rooted phone, or you haven't used your root user privileges. To use your root user privileges, type suon the adb command line and see what happens, if you get an error, then you're phone is not rooted. If it's not, root it first then continue these instructions.

如果没有(即如果您收到错误),那么您可能没有root 手机,或者您没有使用您的root 用户权限。要使用您的 root 用户权限,请su在 adb 命令行上输入并查看会发生什么,如果出现错误,则您的手机没有 root。如果不是,请先root,然后继续这些说明。

from there you can type lsand you'll see all the directories including the dbs:

从那里你可以输入ls,你会看到包括dbs在内的所有目录:

shell@android:/data/data/com.domain.yourapp $ ls

cache
databases
lib
shared_prefs   

after that you can use sqlite3 to browse the dbase.. if you don't have it installed (you can find it out by typing sqlite3, if you get command not foundthen you'll have to install it. To install sqlite, follow instructions here.

之后,您可以使用 sqlite3 浏览 dbase .. 如果您没有安装它(您可以通过键入找到它sqlite3,如果您command not found安装了,则必须安装它。要安装 sqlite,请按照此处的说明进行操作。

about rooting:if you've never rooted your phone before, and you're worried about it screwing your phone, I can tell you with full confidence that there is nothing to worry about. there are tonnes of quick and easy phone rooting tutorials for pretty much all the new and old models out there, and you can root your phone even if you have a mac(I rooted my s3 with my mac).

关于生根:如果您以前从未生根过手机,并且担心它会弄坏您的手机,我可以完全有信心地告诉您,没有什么可担心的。几乎所有新旧型号都有大量快速简便的手机生根教程,即使您拥有 Mac(我用我的 Mac 生根了我的 s3),您也可以生根您的手机

回答by HelloWorld

$ adb shell

$亚行外壳

$ cd /data

$ cd /数据

$ ls

$ls

opendir failed, Permission denied

opendir 失败,权限被拒绝



You should do this:

你应该做这个:

$ adb shell

$ cd /data

shell@android:/data $ run-as com.your.package 

shell@android:/data/data/com.your.package $ ls

OK!

好的!

回答by rytis

I had a similar problem when trying to operate on a rooted Samsung Galaxy S. Issuing a command from the computer shell

我在尝试在有根的三星 Galaxy S 上操作时遇到了类似的问题。 从计算机外壳发出命令

> adb root

fails with a message "cannot run as root in production builds". Here is a simple method that allows to become root.

失败并显示消息“无法在生产版本中以 root 身份运行”。这是一个允许成为root的简单方法。

Instead of the previous, issue the following two commands one after the other

一个接一个地发出以下两个命令,而不是前一个

> adb shell
$ su

After the first command, if the prompt has changed from '>' to '$' as shown above, it means that you have entered the adb shell environment. If subsequently the prompt has changed to '#' after issuing the second command, that means that you are now root. Now, as root, you can do anything you want with your device.

第一条命令后,如果提示如上图从'>'变成了'$',说明你已经进入了adb shell环境。如果随后在发出第二个命令后提示变为“#”,则意味着您现在是 root。现在,作为根用户,您可以对您的设备做任何想做的事情。

To switch back to 'safe' shell, issue

要切换回“安全”外壳,请发出

# exit

You will see that the prompt '$' reappears which means you are in the adb shell as a user and not as root.

您将看到提示 '$' 重新出现,这意味着您以用户身份而不是 root 身份在 adb shell 中。

回答by MSquare

I had a lot of trouble with this also. I still don't fully understand the permission and root run, but this worked for me (one of the previous answers partly) to copy database file from /data/data/[package name]/databases/my_db.db . Running shell root, or suin shell for some reason didn't work, nor did copying the db file (I could navigate to the directory though), nor did sqlite3.

我也遇到了很多麻烦。我仍然不完全理解权限和 root 运行,但这对我有用(以前的答案之一)从 /data/data/[package name]/databases/my_db.db 复制数据库文件。出于某种原因,在 shell 中运行shell rootsu不起作用,复制 db 文件也不起作用(尽管我可以导航到目录),sqlite3不起作用

So, this worked! In DOS command prompt:

所以,这奏效了!在 DOS 命令提示符下:

C:\Program Files\Android\android-sdk\platform-tools>adb shell
1|shell@android:/ $ run-as de.vogella.android.locationapi.maps
run-as de.vogella.android.locationapi.maps
1|shell@android:/data/data/de.vogella.android.locationapi.maps $ cd /data
cd /data
shell@android:/data $ cd data
cd data
shell@android:/data/data $ cd de.vogella.android.locationapi.maps
cd de.vogella.android.locationapi.maps
shell@android:/data/data/de.vogella.android.locationapi.maps $ cd databases
cd databases
shell@android:/data/data/de.vogella.android.locationapi.maps/databases $ ls
ls
bus_timetable_lines.db
bus_timetable_lines.db-journal
shell@android:/data/data/de.vogella.android.locationapi.maps/databases $ cat bus
_timetable_lines.db > /sdcard/db_copy.db
 bus_timetable_lines.db > /sdcard/db_copy.db                                   <
shell@android:/data/data/de.vogella.android.locationapi.maps/databases $exit   ^
exit
shell@android:/ $ exit
exit

C:\Program Files\Android\android-sdk\platform-tools>

Now go to SDCARD directory and get your file db_copy.db . Even that was hidden, but I managed to email it. Back in Windows, I was able to open db file with SQLite Database Browser. :)

现在转到 SDCARD 目录并获取您的文件 db_copy.db 。即使那是隐藏的,但我设法通过电子邮件发送了它。回到 Windows,我能够使用 SQLite 数据库浏览器打开 db 文件。:)

回答by VietHuong

Following are the two steps to get root access:

以下是获得root访问权限的两个步骤:

  1. Your android devices must be rooted.
  2. In ADB shell, type su and the android device will prompt y/n? You choose allow.
  1. 您的 android 设备必须已植根。
  2. 在 ADB shell 中,输入 su,android 设备会提示 y/n? 你选择允许。

回答by ashishsony

The problem could be that we need to specifically give adb root access in the developnent options in the latest CMs.. Here is what i did.

问题可能是我们需要在最新的 CM 的开发选项中专门授予 adb root 访问权限。这就是我所做的。

abc@abc-L655:~$ sudo adb kill-server
abc@abc-L655:~$ sudo adb root start-server * daemon not running. starting it now on port 5037 * * daemon started successfully * root access is disabled by system setting - enable in settings -> development options

abc@abc-L655:~$ sudo adb kill-server
abc@abc-L655:~$ sudo adb root start-server * 守护进程没有运行。现在在端口 5037 上启动它 * * 守护进程成功启动 * 系统设置禁用了 root 访问 - 在设置中启用 -> 开发选项

after altering the development options...

更改开发选项后...

abc@abc-L655:~$ sudo adb kill-server
abc@abc-L655:~$ sudo adb root start-server
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
restarting adbd as root
abc@abc-L655:~$ adb shell
root@android:/ # ls /data/ .... good to go.. 

回答by keith

production builds can't enter /data/app

生产版本无法进入 /data/app

drwxrwx--- system   cache             1970-01-01 08:00 cache
drwxrwxr-x root     system            1970-01-01 08:00 mnt 
drwxrwx--x system   system            1970-01-01 08:15 data

might need change to right owner to operate it.

可能需要更改为正确的所有者才能操作它。

回答by Mark

if you know the application package you can cddirectly to that folder..

如果你知道应用程序包,你可以cd直接到那个文件夹..

eg cd data/data/com.yourapp

例如 cd data/data/com.yourapp

this will drop you into a directory that is read/writableso you can change files as needed. Since the folder is the same on the emulator, you can use that to get the folder path.

这将使您进入一个目录,read/writable以便您可以根据需要更改文件。由于文件夹与 上的文件夹相同emulator,您可以使用它来获取文件夹路径。