Linux check root directory

How Do I check Permissions of Root Folder (/ Folder, not /root)?

Is there a way to check the permissions of the root folder, /? I mean the folder’s permissions, not its content’s (/var, /usr, etc.) permissions? Running ls /.. shows the content’s permissions.

Yes, it shows the contents; I wanted the contents of the outer folder, which doesn’t technically exist. The question is already answered anyway.

Can you please edit your title to not include the text «/root», because web searched for «/root » come here and that doesn’t make any sense. Perhaps you could just say (/), and then lower down in the text clarify that you are not referring to /root. Thanks

If I did that, people would come here looking for /root folder permissions, which also does not make sense. I’ll be damned if I do, damned if I don’t.

-1 Because you have not yet changed the title to simply /, I came here looking for permission of /root (because that’s how google works.)

3 Answers 3

You can also use the -d switch of ls :

$ ls -ld / drwxr-xr-x 28 root root 126976 Mar 20 17:11 / 
 -l use a long listing format -d, --directory list directory entries instead of contents, and do not derefer‐ ence symbolic links 

It will give you the permissions.

Use the -a switch of ls to include hidden files as well as . and .. in the listing and the -l switch for a «long» listing (which includes the permissions, among other information):

The line with a single . in the last column will contain information about the listed directory itself, i.e. / :

drwxr-xr-x 26 root root 4096 Mar 10 15:57 .

However if you only need information about / itself, terdon’s answer (using the -d switch) will probably be handier.

@trysis I routinely use ls -blah . It has everything you could possibly want to know about a file or directory.

This is not really a very good solution, it will list all files under / when all the OP wanted was / itself. See stat or ls -ld in the answers below.

@trysis You might want to accept terdon’s answer instead since it’s closer to what you originally wanted to achieve.

Читайте также:  Линукс задать пароль пользователя

Fair enough, the comment was not so much directed at you as to future users who might see this as the accepted answer and assume it is the Best Way® to do it.

. is not necessarily first. The list is sorted lexically. There are several characters that sort before . in many locales.

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

check if user can access /root directory before running command

I have a homework assignment that asks me to print certain things in a linux terminal with bash scripting. I have done most of them but I am stuck on the last thing I need to do. This is how my professor worded it

Checks to see if current user has access to /root -lists files in the root directory if so -Hint: try using 'ls' 

ls is what you need, but you’ll need to add some options to see permissions on that directory, so run man ls and take a look at the man page. If you don’t know how to read a man page, Google it. This is a skill you will want to have sooner rather than later.

Is it OK to exit with an error message if the /root directory can not be accessed? or is it supposed to print nothing if /root is inaccessible?

1 Answer 1

Conditional Expressions and Symbolic Permissions

The simplest solution is to use a Bash conditional expression to check directory read permissions. For example:

If you’d rather inspect the permissions visually, you can use ls to list the directory (rather than its contents) with the -d flag. For example:

$ ls -lad /root drwxr-x--- 6 root wheel 204 Oct 25 04:45 /root 

You can then parse the permissions using symbolic notation to see if the current user would have access to its contents, but doing it that way just seems like the long way around when you can just use a conditional or handle the exit status of ls directly:

ls /root || echo "Grade: 'F' for effort." > /dev/stderr 

Источник

How to find the disk where root / is on in Bash on Linux?

Question: how to find the disk where the Linux’s root(/) is on in Bash? The root may be on a LVM volume or on a raw disk.

# df -hT | grep /$ /dev/sda4 ext4 48G 32G 14G 71% / 
# df -hT | grep /$ /dev/mapper/fedora-root ext4 48G 45G 1.4G 98% / 

The it is a LVM volume in LVM group ‘fedora’:

# lvs | grep fedora | grep root root fedora -wi-ao---- 48.83g 

and ‘fedora’ is on the single disk partition ‘/dev/sda3’ of disk ‘/dev/sda’:

# pvs | grep fedora /dev/sda3 fedora lvm2 a-- 64.46g 4.00m 

For both cases above, we want to find out ‘/dev/sda’ (the root filesystem is on only one physical disk).

Читайте также:  Linux обновить python до последней версии

To solve this problem, one straightforward way is to check every possible cases and handle each case following the format for each case (raw partition, or LVM partition). But in Linux, there is a more convenient tool to handle this problem – using lsblk .

lsblk and PKNAME

lsblk can show one important value here:

For a raw partition, the parent kernel device is the disk, and for an LVM partition, the parent kernel device is the physical volume (the partition). Here are examples of output of lsblk .

For a ‘/’ on a raw partition:

root@vm0:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT . sdc 8:32 0 1.8T 0 disk ├─sdc1 8:33 0 512M 0 part /boot/efi └─sdc2 8:34 0 1.8T 0 part /

For a ‘/’ on an LVM partition:

root@vm1:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 512M 0 part /boot/efi ├─sda2 8:2 0 1K 0 part └─sda5 8:5 0 19.5G 0 part ├─vgubuntu-root 253:0 0 18.5G 0 lvm / └─vgubuntu-swap_1 253:1 0 976M 0 lvm [SWAP]

PKNAME for raw or LVM partition backed root ‘/’

From lsblk ‘s manual, we can see it supports various output format. Here, we use the key/value pair format which is easy to parse in Bash.

# lsblk -oMOUNTPOINT,PKNAME -P | grep 'MOUNTPOINT="/"' MOUNTPOINT="/" PKNAME="sdc"
# lsblk -oMOUNTPOINT,PKNAME -P | grep 'MOUNTPOINT="/"' MOUNTPOINT="/" PKNAME="sda5"

We can easily get rid of the trailing numbers (so we get the disk device name) by

This can be applied to both cases. For the raw partition case, the sed command simply does not have any additional effect.

Wrap up to be a bash statement

Now, we can wrap all up into a bash statement using some Bash grammar and techniques

dev=$(eval $(lsblk -oMOUNTPOINT,PKNAME -P -M | grep 'MOUNTPOINT="/"'); echo $PKNAME | sed 's/9*$//')

For the raw partition case, we get

root@vm0:~# dev=$(eval $(lsblk -oMOUNTPOINT,PKNAME -P -M | grep 'MOUNTPOINT="/"'); echo $PKNAME | sed 's/9*$//') root@vm0:~# echo $dev sdc

For the LVM partition case, we get

root@vm1:~# dev=$(eval $(lsblk -oMOUNTPOINT,PKNAME -P | grep 'MOUNTPOINT="/"'); echo $PKNAME | sed 's/7*$//') root@vm1:~# echo $dev sda

Источник

Читайте также:  First IP

How to check root partition with fsck?

I installed Linux Mint 12 KDE, and I would like to check the root partition for any errors. How do I check the root partition with fsck at boot time?

6 Answers 6

This works, but for whatever reason, systemd recommends to pass fsck.mode=force on the kernel command line instead. (A warning appears in journalctl -xb .)

@SeppoEnarvi: the forcefsck is (was?) natively supported only by system V init, but neither upstart nor systemd. The support for it may be added by the distro (Debian and, therefore, Ubuntu, do). The solution you mention is distro-independent. Too bad it’s not a simple business to pass anything on the kernel command line on a headless server or a cloud VM.

You can use shutdown command for this too.

The -F flag means ‘force fsck’.
This only creates an advisory file /forcefsck which can be tested by the system when it comes up again. The boot rc file can test if this file is present, and decide to run fsck(1) with a special `force’ flag so that even properly unmounted file systems get checked. After that, the boot process should remove /forcefsck.

I tried this with Linux Mint 15 MATE and it didn’t cause a check when rebooting. But sudo touch /forcefsck worked when I did that before sudo reboot .

shutdown supplied with Upstart does not support the -F option any more. You should use sudo touch /forcefsck instead. See for example Why was -F removed from /sbin/shutdown? and Bug #74139: shutdown missing -F (force fsck) option.

Here is another way to do this:

tune2fs -C 2 -c 1 /dev/THEDEVTHATROOTIS

then the filesystem will be checked, and once all is good you should do

tune2fs -c 60 /dev/THEDEVTHATROOTIS

I have assumed that the max-mount-count was set to 60, you should find out before issuing the first command with

dumpe2fs /dev/THEDEVTHATROOTIS |grep «Maximum mount count»

your answer is good and . should work most of the time (I mean on most of standard installed Linux) BUT, you ASSUME that root partition is ext2,3,4 formatted, what if is something else like xfs or reiserfs ? 🙂

On my systems (several x86 notebooks and a Banana Pi Pro), saying sudo shutdown now brings me to runlevel 1 (aka maintenance mode) where I can safely check my root FS:

mount -o remount,ro /dev/rootpartition fsck /dev/rootpartition reboot 

There’s no need to alter /etc/fstab to do this, and I have the opportunity to run fsck with whatever options that may be needed to fix a tricky case.

Note: /forcefsck and tune2fs tricks work on x86, but not on Banana Pi.

Источник

Оцените статью
Adblock
detector