{"id":1160,"date":"2018-09-03T13:08:47","date_gmt":"2018-09-03T07:38:47","guid":{"rendered":"http:\/\/jaipurhosting.com\/blog\/?p=1160"},"modified":"2019-04-27T07:41:45","modified_gmt":"2019-04-27T02:11:45","slug":"how-to-manage-linux-disk-partitions-using-fdisk-commands-2","status":"publish","type":"post","link":"https:\/\/www.jaipurhosting.com\/blog\/how-to-manage-linux-disk-partitions-using-fdisk-commands-2\/","title":{"rendered":"How to manage Linux Disk Partitions using fdisk Commands"},"content":{"rendered":"<p><strong>Description<\/strong><\/p>\n<p>fdisk stands (for \u201c<strong>fixed disk<\/strong> or <strong>format disk<\/strong>\u201c) is an most commonly used command-line based disk manipulation utility for a Linux\/Unix systems.<\/p>\n<p>With the help of fdisk command you can view, create, resize, delete, change, copy and move partitions on a hard drive using its own user friendly text based menu driven interface.<\/p>\n<p>This tool is very useful in terms of creating space for new partitions, organising space for new drives, re-organising an old drives and copying or moving data to new disks.<\/p>\n<p>It allows you to create a maximum of four new primary partition and number of logical (extended) partitions, based on size of the hard disk you have in your system.<\/p>\n<p>This article explains 10 basic fdisk commands to manage a partition table in Linux based systems. You must be root user to run fdisk command, otherwise you will get a \u201ccommand not found\u201d error.<\/p>\n<p><strong>1. View all Disk Partitions in Linux<\/strong><\/p>\n<p>The following basic command list all existing disk partition on your system.<\/p>\n<p>The \u2018-l\u2018 argument stand for (listing all partitions) is used with fdisk command to view all available partitions on Linux.<\/p>\n<p>The partitions are displayed by their device\u2019s names.<\/p>\n<p>For example: \/dev\/sda, \/dev\/sdb or \/dev\/sdc.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true\">[root@vps ~]# fdisk -l<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>2. View Specific Disk Partition in Linux<\/strong><\/p>\n<p>To view all partitions of specific hard disk use the option \u2018-l\u2018 with device name.<\/p>\n<p>For example, the following command will display all disk partitions of device \/dev\/sda.<\/p>\n<p>If you\u2019ve different device names, simple write device name as \/dev\/sdb or \/dev\/sdc.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk -l \/dev\/sda<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>3. Check all Available fdisk Commands<\/strong><\/p>\n<p>If you would like to view all commands which are available for fdisk.<\/p>\n<p>Simply use the following command by mentioning the hard disk name such as \/dev\/sda as shown below.<\/p>\n<p>The following command will give you output similar to below.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@tecmint ~]# fdisk \/dev\/sda<\/pre>\n<p>Type \u2018m\u2018 to see the list of all available commands of fdisk which can be operated on \/dev\/sda hard disk.<\/p>\n<p>After, I enter \u2018m\u2018 on the screen, you will see the all available options for fdisk that you can be used on the \/dev\/sda device.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk \/dev\/sda<\/pre>\n<p><strong>4. Print all Partition Table in Linux<\/strong><\/p>\n<p>To print all partition table of hard disk, you must be on command mode of specific hard disk say \/dev\/sda.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk \/dev\/sda<\/pre>\n<p>From the command mode, enter \u2018p\u2018 instead of \u2018m\u2018 as we did earlier.<\/p>\n<p>As I enter \u2018p\u2018, it will print the specific \/dev\/sda partition table.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">Command (m for help): p\nDisk \/dev\/sda: 637.8 GB, 637802643456 bytes\n255 heads, 63 sectors\/track, 77541 cylinders\nUnits = cylinders of 16065 * 512 = 8225280 bytes\nDevice Boot Start End Blocks Id System\n\/dev\/sda1 * 1 13 104391 83 Linux\n\/dev\/sda2 14 2624 20972857+ 83 Linux\n\/dev\/sda3 2625 4582 15727635 83 Linux\n\/dev\/sda4 4583 77541 586043167+ 5 Extended\n\/dev\/sda5 4583 5887 10482381 83 Linux\n\/dev\/sda6 5888 7192 10482381 83 Linux\n\/dev\/sda7 7193 7845 5245191 83 Linux\n\/dev\/sda8 7846 8367 4192933+ 82 Linux swap \/ Solaris\n\/dev\/sda9 8368 77541 555640123+ 8e Linux LVM\nCommand (m for help):<\/pre>\n<p><strong>5. How to Delete a Partition in Linux<\/strong><\/p>\n<p>If you would like to delete a specific partition (i.e \/dev\/sda9) from the specific hard disk such as \/dev\/sda. You must be in fdisk command mode to do this.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk \/dev\/sda<\/pre>\n<p>Next, enter \u2018d\u2018 to delete any given partition name from the system.<\/p>\n<p>As I enter \u2018d\u2018, it will prompt me to enter partition number that I want to delete from \/dev\/sda hard disk.<\/p>\n<p>Suppose I enter number \u20184\u2018 here, then it will delete partition number \u20184\u2018 (i.e. \/dev\/sda4) disk and shows free space in partition table.<\/p>\n<p>Enter \u2018w\u2018 to write table to disk and exit after making new alterations to partition table.<\/p>\n<p>The new changes would only take place after next reboot of system.<\/p>\n<p>This can be easily understood from the below output.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk \/dev\/sda\nWARNING: DOS-compatible mode is deprecated. It's strongly recommended to\nswitch off the mode (command 'c') and change display units to\nsectors (command 'u').\nCommand (m for help): d\nPartition number (1-4): 4\nCommand (m for help): w\nThe partition table has been altered!\nCalling ioctl() to re-read partition table.\nWARNING: Re-reading the partition table failed with error 16: Device or resource busy.\nThe kernel still uses the old table. The new table will be used at\nthe next reboot or after you run partprobe(8) or kpartx(8)\nSyncing disks.\nYou have new mail in \/var\/spool\/mail\/root<\/pre>\n<p><strong>Warning<\/strong> : Be careful, while performing this step, because using option \u2018d\u2018 will completely delete partition from system and may lost all data in partition.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>6. How to Create a New Partition in Linux<\/strong><\/p>\n<p>If you\u2019ve free space left on one of your device say \/dev\/sda and would like to create a new partition under it. Then you must be in fdisk command mode of \/dev\/sda.<\/p>\n<p>Type the following command to enter into command mode of specific hard disk.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk \/dev\/sda<\/pre>\n<p>After entering in command mode, now press \u201cn\u201d command to create a new partition under \/dev\/sda with specific size.<\/p>\n<p>This can be demonstrated with the help of following given output.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk \/dev\/sda\nWARNING: DOS-compatible mode is deprecated. It's strongly recommended to\nswitch off the mode (command 'c') and change display units to\nsectors (command 'u').\nCommand (m for help): n\nCommand action\ne extended\np primary partition (1-4)\ne<\/pre>\n<p>While creating a new partition, it will ask you two options \u2018extended\u2018 or \u2018primary\u2018 partition creation. Press \u2018e\u2018 for extended partition and \u2018p\u2018 for primary partition. Then it will ask you to enter following two inputs.<\/p>\n<ul>\n<li>First cylinder number of the partition to be create.<\/li>\n<li>Last cylinder number of the partition to be created (Last cylinder, +cylinders or +size).<\/li>\n<\/ul>\n<p>You can enter the size of cylinder by adding \u201c+5000M\u201d in last cylinder.<\/p>\n<p>Here, \u2018+\u2018 means addition and 5000M means size of new partition (i.e 5000MB).<\/p>\n<p>Please keep in mind that after creating a new partition, you should run \u2018w\u2018 command to alter and save new changes to partition table and finally reboot your system to verify newly created partition.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">Command (m for help): w\nThe partition table has been altered!\nCalling ioctl() to re-read partition table.\nWARNING: Re-reading the partition table failed with error 16: Device or resource busy.\nThe kernel still uses the old table. The new table will be used at\nthe next reboot or after you run partprobe(8) or kpartx(8)\nSyncing disks.<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>7. How to Format a Partition in Linux<\/strong><\/p>\n<p>After the new partition is created, don\u2019t skip to format the newly created partition using \u2018mkfs\u2018 command. Type the following command in the terminal to format a partition. Here \/dev\/sda4 is my newly created partition.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# mkfs.ext4 \/dev\/sda4<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>8. How to Check Size of a Partition in Linux<\/strong><\/p>\n<p>After formatting new partition, check the size of that partition using flag \u2018s\u2018 (displays size in blocks) with fdisk command. This way you can check size of any specific device.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk -s \/dev\/sda2\n5194304<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>9. How to Fix Partition Table Order<\/strong><\/p>\n<p>If you\u2019ve deleted a logical partition and again recreated it, you might notice \u2018partition out of order\u2018 problem or error message like \u2018Partition table entries are not in disk order\u2018.<\/p>\n<p>For example, when three logical partitions such as (sda4, sda5 and sda6) are deleted, and new partition created, you might expect the new partition name would be sda4.<\/p>\n<p>But, the system would create it as sda5.<\/p>\n<p>This happens because of, after the partition are deleted, sda7 partition had been moved as sda4 and free space shift to the end.<\/p>\n<p>To fix such partition order problems, and assign sda4 to the newly created partition, issue the \u2018x\u2018 to enter an extra functionality section and then enter \u2018f\u2018 expert command to fix the order of partition table as shown below.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk \/dev\/sda\nWARNING: DOS-compatible mode is deprecated. It's strongly recommended to\nswitch off the mode (command 'c') and change display units to\nsectors (command 'u').\nCommand (m for help): x\nExpert command (m for help): f\nDone.\nExpert command (m for help): w\nThe partition table has been altered!\nCalling ioctl() to re-read partition table.\nWARNING: Re-reading the partition table failed with error 16: Device or resource busy.\nThe kernel still uses the old table. The new table will be used at\nthe next reboot or after you run partprobe(8) or kpartx(8)\nSyncing disks.<\/pre>\n<p>After, running \u2018f\u2018 command, don\u2019t forget to run \u2018w\u2018 command to save and exit from fdisk command mode. Once it fixed partition table order, you will no longer get error messages.<\/p>\n<p><strong>10. How to Disable Boot Flag (*) of a Partition<\/strong><\/p>\n<p>By default, fdisk command shows the boot flag (i.e. \u2018*\u2018) symbol on each partition.<\/p>\n<p>If you want to enable or disable boot flag on a specific partition, do the following steps.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk \/dev\/sda<\/pre>\n<p>Press \u2018p\u2018 command to view the current partition table, you see there is a boot flag (asterisk (*) symbol in orange color) on \/dev\/sda1 disk as shown below.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps ~]# fdisk \/dev\/sda\nWARNING: DOS-compatible mode is deprecated. It's strongly recommended to\nswitch off the mode (command 'c') and change display units to\nsectors (command 'u').\nCommand (m for help): p\nDisk \/dev\/sda: 637.8 GB, 637802643456 bytes\n255 heads, 63 sectors\/track, 77541 cylinders\nUnits = cylinders of 16065 * 512 = 8225280 bytes\nDevice Boot      Start         End      Blocks   Id  System\n\/dev\/sda1   *           1          13      104391   83  Linux\n\/dev\/sda2              14        2624    20972857+  83  Linux\n\/dev\/sda3            2625        4582    15727635   83  Linux\n\/dev\/sda4            4583       77541   586043167+   5  Extended\n\/dev\/sda5            4583        5887    10482381   83  Linux\n\/dev\/sda6            5888        7192    10482381   83  Linux\n\/dev\/sda7            7193        7845     5245191   83  Linux\n\/dev\/sda8            7846        8367     4192933+  82  Linux swap \/ Solaris\n\/dev\/sda9            8368       77541   555640123+  8e  Linux LVM<\/pre>\n<p>Next enter command \u2018a\u2018 to disable boot flag, then enter partition number \u20181\u2018 as (i.e. \/dev\/sda1) in my case. This will disable boot flag on the partition \/dev\/sda1. This will remove the asterisk (*) flag.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">Command (m for help): a\nPartition number (1-9): 1\nCommand (m for help): p\nDisk \/dev\/sda: 637.8 GB, 637802643456 bytes\n255 heads, 63 sectors\/track, 77541 cylinders\nUnits = cylinders of 16065 * 512 = 8225280 bytes\nDevice Boot      Start         End      Blocks   Id  System\n\/dev\/sda1               1          13      104391   83  Linux\n\/dev\/sda2              14        2624    20972857+  83  Linux\n\/dev\/sda3            2625        4582    15727635   83  Linux\n\/dev\/sda4            4583       77541   586043167+   5  Extended\n\/dev\/sda5            4583        5887    10482381   83  Linux\n\/dev\/sda6            5888        7192    10482381   83  Linux\n\/dev\/sda7            7193        7845     5245191   83  Linux\n\/dev\/sda8            7846        8367     4192933+  82  Linux swap \/ Solaris\n\/dev\/sda9            8368       77541   555640123+  8e  Linux LVM\nCommand (m for help):<\/pre>\n<p>&nbsp;<\/p>\n<p>We hope you\u2019ve found this useful!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description fdisk stands (for \u201cfixed disk or format disk\u201c) is an most commonly used command-line based disk manipulation utility for a Linux\/Unix systems. With the help of fdisk command you can view, create, resize, delete, change, copy and move partitions on a hard drive using its own user friendly text based menu driven interface. This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1285,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1160","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1160"}],"version-history":[{"count":7,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1160\/revisions"}],"predecessor-version":[{"id":1226,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1160\/revisions\/1226"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media\/1285"}],"wp:attachment":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}