linux 报错:Permission denied——如何切换到 root 身份

在Linux环境中,非root用户执行`fdisk -l`会遇到权限问题。解决方法是切换到root权限,如使用`sudo su`。成功切换后,可以查看所有磁盘信息,包括新添加的硬盘。对于新硬盘的分区,需要进一步进行分区操作,如创建主分区或逻辑分区,并格式化为合适的文件系统。

1. 问题:在一般用户身份下使用fdisk -l遭到拒绝:Permission denied

suer@suer-virtual-machine:~$ fdisk -l
fdisk: cannot open /dev/ram0: Permission denied
fdisk: cannot open /dev/ram1: Permission denied
fdisk: cannot open /dev/ram2: Permission denied
fdisk: cannot open /dev/ram3: Permission denied
fdisk: cannot open /dev/ram4: Permission denied
fdisk: cannot open /dev/ram5: Permission denied
fdisk: cannot open /dev/ram6: Permission denied
fdisk: cannot open /dev/ram7: Permission denied
fdisk: cannot open /dev/ram8: Permission denied
fdisk: cannot open /dev/ram9: Permission denied
fdisk: cannot open /dev/ram10: Permission denied
fdisk: cannot open /dev/ram11: Permission denied
fdisk: cannot open /dev/ram12: Permission denied
fdisk: cannot open /dev/ram13: Permission denied
fdisk: cannot open /dev/ram14: Permission denied
fdisk: cannot open /dev/ram15: Permission denied
fdisk: cannot open /dev/fd0: Permission denied
fdisk: cannot open /dev/sda: Permission denied
fdisk: cannot open /dev/sdb: Permission denied

2. 解决:切换到 root 身份

有以下三种方式切换到 root 身份:

方法1:输入su,然后按照提示输入相应的root密码,就可登录到root权限下。

方法2:输入sudo su,然后按照提示输入相应的root密码,就可登录到root权限下(这个命令下,一般不需要输入相应的root密码)。

方法3:输入su root,然后按照提示输入相应的root密码,就可登录到root权限下。

附:su用来在用户间切换,也可以是从root身份切换到普通用户:su - (用户名)
(注意: - 两边都有空格)

  • su = switch user 切换用户(默认切换成root用户),不切换工作环境
    但是直接使用su给用户的权限太大,增加了风险,为了限制用户的权限出现了sudo
  • sudo = superuser do ( /etc/sudoers )
  • 一般的使用情况下,最好使用sudo来执行命令,避免自己误操作破坏了系统。
    如果是存在多用户使用同一个系统的话,主用户可以使用root权限来配置 /etc/sudoers 文件来指派不同用户不同的权限,从而保证系统的安全。

使用方法二的代码sudo su成功:

suer@suer-virtual-machine:~$ sudo su
root@suer-virtual-machine:/home/suer# fdisk -lu
root@suer-virtual-machine:/home/suer# fdisk -lu
Disk /dev/ram0: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram1: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram2: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram3: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram4: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram5: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram6: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram7: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram8: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram9: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram10: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram11: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram12: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram13: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram14: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/ram15: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes






Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7aeec698

Device     Boot    Start      End  Sectors Size Id Type
/dev/sda1  *        2048 67108863 67106816  32G 83 Linux
/dev/sda2       67110910 83884031 16773122   8G  5 Extended
/dev/sda5       67110912 83884031 16773120   8G 82 Linux swap / Solaris


Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

3. 接下来的问题:如何给新添加的硬盘重新分区

可以参考热乎的文章:Linux 如何给新加的硬盘重新添加分区(纯新手)

参考:

  1. Ubuntu 终端切换root 操作
  2. linux中su与sudo与su-的理解
/bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:28:33+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:28:33+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:29:34+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:29:34+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:32:26+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:32:27+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:32:27+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:32:28+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:32:28+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:32:28+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:32:29+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:32:29+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:32:31+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:32:31+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:32:33+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:32:33+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:32:37+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:32:37+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:32:44+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:32:44+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:32:58+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:32:58+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied 2024-08-26 20:33:24+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server started. 2024-08-26 20:33:24+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' /bin/bash: /usr/local/bin/docker-entrypoint.sh: Permission denied
最新发布
12-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值