linux用户管理sudo 磁盘分区

1.0 su命令

su切换用户
[root@linux1 ~]# su - centos
查看当前用户
[centos@linux1 ~]$ whoami
[centos@linux1 ~]$ id
加-为了彻底切换用户,加载用户的家目录。
指定普通用户去执行一条命令
[root@linux1 ~]# su - -c "touch /tmp/1.11" centos
查看文件
[root@linux1 ~]# ls -l /tmp/
总用量 64
-rw-rw-r--. 1 centos centos     0 9月   4 20:24 1.11
切换到没有家目录的用户
[root@linux1 ~]# su - user4
su: 警告:无法更改到 /home/user4 目录: 没有那个文件或目录
-bash-4.2$ 
发现命令行跟普通用户不一样,如何恢复?
回到root用户创建目录
root@linux1 ~]# mkdir /home/user4
拷贝配置文件
[root@linux1 ~]# cp /etc/skel/.bash* /home/user4/
更改所有者所属组
[root@linux1 ~]# chown -R user4:user4 /home/user4/
切换user4用户
[root@linux1 ~]# su - user4
上一次登录:三 9月  4 20:28:27 CST 2019pts/0 上
[user4@linux1 ~]$ 

2.0 sudo命令

visudo编辑
visudo 编辑sudo配置文件(相当于 vi /etc/sudoers,但是不建议用,注意检查语法)
visudo 编辑/etc/sudoers命令
visudo -c 检查语法命令,有报错可能导致系统起不来,所以编辑之前需要备份,编辑后最好检查一下用法
找到root这一行,表示允许用户使用任何命令
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
用户   主机=(用户) 所有的命令
添加用户拥有root权限执行命令
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
centos  ALL=(ALL)       /usr/bin/ls,/usr/bin/mv,/usr/bin/cat
切换到centos用户,第一次登陆需要输入密码
[root@linux1 ~]# su - centos
上一次登录:三 9月  4 20:24:01 CST 2019pts/0 上
[centos@linux1 ~]$ sudo ls

我们信任您已经从系统管理员那里了解了日常注意事项。
总结起来无外乎这三点:

    #1) 尊重别人的隐私。
    #2) 输入前要先考虑(后果和风险)。
    #3) 权力越大,责任越大。

[sudo] centos 的密码:
执行命令
[centos@linux1 ~]$ ls /root
ls: 无法打开目录/root: 权限不够
执行绝对路径
[centos@linux1 ~]$ sudo /usr/bin/ls /root/
123  1.log  1.txt  2.log  2.txt  3.txt	anaconda-ks.cfg
查看文件
[centos@linux1 ~]$ cat /root/1.txt
cat: /root/1.txt: 权限不够
[centos@linux1 ~]$ sudo /usr/bin/cat /root/1.txt
添加用户不用输入密码
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
centos  ALL=(ALL)       /usr/bin/ls,/usr/bin/mv,/usr/bin/cat
user4   ALL=(ALL)      NOPASSWD: /usr/bin/ls
切换user4用户
[root@linux1 ~]# su - user4
上一次登录:三 9月  4 20:32:33 CST 2019pts/0 上
[user4@linux1 ~]$ sudo ls /root
123  1.log  1.txt  2.log  2.txt  3.txt	anaconda-ks.cfg

3.0 限制root远程登录

设定用户alias,编辑sudo
# User_Alias ADMINS = jsmith, mikem
User_Alias HEIGUI = centos,user4
用户别名   别名         用户

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
centos  ALL=(ALL)       /usr/bin/ls,/usr/bin/mv,/usr/bin/cat
user4   ALL=(ALL)      NOPASSWD: /usr/bin/ls
HEIGUI  ALL=(ALL)      NOPASSWD: /usr/bin/su
切换到centos用户
[root@linux1 ~]# su - centos
上一次登录:三 9月  4 20:57:59 CST 2019pts/0 上
用centos登陆root用户
[centos@linux1 ~]$ sudo su - root
上一次登录:三 9月  4 20:19:43 CST 2019从 192.168.109.1pts/0 上

不需要密码直接登录,但是root还是可以远程登陆,如何限制root远程登录呢?

编辑配置文件/etc/ssh/sshd_config
#PermitRootLogin yes
改为
PermitRootLogin no
重启ssh服务
[root@linux1 ~]# systemctl restart sshd.service

root就不可远程登陆

4.0 磁盘管理df命令

[root@linux1 ~]# df
文件系统          1K-块    已用     可用 已用% 挂载点
/dev/sda3      18658304 1166900 17491404 7%    /
查看大小
[root@linux1 ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3      18G   1.2G  17G  7%     /

注:Byte字节,1000字节就是1kb  1000kb就是1M 1000M就是1GB 1000GB就是1T(正常的话是1024)

查看磁盘的inode
[root@linux1 ~]# df -i

5.0 du命令

du命令是查看文件大小的
[root@linux1 ~]# du -sh /root
60K	/root
[root@linux1 ~]# du -sh /etc/passwd
4.0K	/etc/passwd
[root@linux1 ~]# ls -lh /etc/passwd
-rw-r--r--. 1 root root 1.1K 9月   4 20:28 /etc/passwd

查看文件大小并没4kb那么大,涉及到块

6.0 磁盘分区

虚拟机添加一块10G硬盘

重启虚拟机
查看添加的硬盘有没有识别到
[root@linux1 ~]# fdisk -l

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000cba14

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
/dev/sda3         4605952    41943039    18668544   83  Linux

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
识别到,分区
[root@linux1 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0xf5ca90c2 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助): m 
命令(输入 m 获取帮助):m
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

命令(输入 m 获取帮助):
常用到的n(增加一个新的分区) p(列出来) w(保存) d (删除)  
输入n
命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
分区类型 p(主分区)e(扩展分区)最多划分4个主分区
划分一个主分区输入p
分区号 (1-4,默认 1):1  输入1
起始 扇区 (2048-20971519,默认为 2048):直接回车
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519):+2G     输入+2G
分区 1 已设置为 Linux 类型,大小设为 2 GiB

在添加一个主分区

命令(输入 m 获取帮助):n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
分区号 (2-4,默认 2):2
起始 扇区 (4196352-20971519,默认为 4196352):
将使用默认值 4196352
Last 扇区, +扇区 or +size{K,M,G} (4196352-20971519,默认为 20971519):+3G
分区 2 已设置为 Linux 类型,大小设为 3 GiB
 
命令(输入 m 获取帮助):p

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xf5ca90c2

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351    2097152  83  Linux
/dev/sdb2         4196352    10487807    3145728  83  Linux

注:分了两个主分区,主分区只能有4个,4个主分区就不能再划分分区了。需要划分3个主,一个扩展分区。还想再分区只能从扩展分区去分逻辑分区

继续划分1个主分区.1个逻辑分区
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xf5ca90c2

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352    10487807     3145728   83  Linux
/dev/sdb3        10487808    14682111     2097152   83  Linux
/dev/sdb4        14682112    16779263     1048576    5  Extended
/dev/sdb5        14684160    15093759      204800   83  Linux
删除分区输入d
命令(输入 m 获取帮助):d
分区号 (1-5,默认 5):1  
分区 1 已删除

命令(输入 m 获取帮助):q直接退出不保存

注:

fdisk命令只能划分2个T的磁盘,超过2T就不能使用此命令分区 

parted命令分区只能划分大于2T的磁盘

原文地址:https://www.cnblogs.com/yantou/p/13450116.html