Minggu, 26 November 2023

RESIZE-partisi ubuntu

 

How to Extend the Default Ubuntu LVM Partition

POV: You’re a sysadmin who set up a one-off Linux machine for an app you needed, and now it’s out of disk space.

You originally spun up a VM, installed a recent Ubuntu OS, and just hit Next, Next, Finish through the guided install. Linux is not your bread and butter, you usually deal in Windows, and you just need to get this done.

Approx Reading Time: 10 minutes

Note: In my examples, I’m using an “all-defaults” setup of Ubuntu 20.04 Server with a single 100GB disk.

 

Linux LVM Briefly Explained

If you followed the default settings in the Ubuntu installation, then the storage for your Linux OS is probably using the Logical Volume Manager (LVM). LVM is an abstraction framework which exists between your physical (or virtual) disks and your Linux file system (which is likely ext4). It is used to group separate block devices (partitions) together into Volume Groups (VGs), and then chop those VGs up into logical block devices, or Logical Volumes (LVs). LVs are the abstracted block devices upon which your usable file system resides.

Below is a good visualization of how LVM works. In this example, we have five different disks, each with a single partition mapped to Physical Volumes (PVs), all being grouped into a single Volume Group (VG). The Volume Group is chopped up into two different Logical Volumes (LVs), and each LV is being used for a filesystem.

Linux Ubuntu LVM diagram

Using a similar visualization, the below diagram shows how the Ubuntu installer (using all default options) divided up my 100GB disk.

Linux Ubuntu installer defaults

Ubuntu Installer Default Settings

When installing Ubuntu, it has you approve a storage layout in a couple different screens (shown below). By default this storage layout will have a couple small boot partitions, and a third partition, which will be used by your LVM to create your root filesystem. You should be able to see the consistency between the screens below and the diagram above.

Ubuntu installer storage layout partitions
Ubuntu installer storage configuration partitions

Use Your Default Free Space

As you can see above: the Ubuntu installer (by default) left almost half of my disk space unusable by the root file system! I’ve looked around to find an explanation on why these are the default settings, but can’t find anything. Before extending your underlying hypervisor disk or storage volume, you may want to see if you have free space available and ready to be used to extend your existing file system. If you used the Ubuntu defaults during installation, then there is a good chance you have this free space.

Start by checking your root filesystem free space with df -h. As you can see I am only using 14% of my ~49GB volume, but we’ll pretend I’m close to 100% and need to make that 49GB volume larger.

Ubuntu LVM check free space df-h

To check for existing free space on your Volume Group (where it is left by the installer default settings), run the command vgdisplay and check for free space. Here you can see I have 49.25GB of free space ready to be used. If you don’t have any free space, move on to the next section to use some free space from an extended physical (or virtual) disk.

Ubuntu LVM: check volume group space vgdisplay

To use up that free space on your Volume Group (VG) for your root Logical Volume (LV), first run the lvdisplay command and check the Logical Volume size, then run lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv to extend the LV to the maximum size usable, then run lvdisplay one more time to make sure it changed.

Ubuntu LVM: extend LV size

At this point you have increased the size of the block volume where your root filesystem resides, but you still need to extend the filesystem on top of it. First, run df -h to verify your (almost full) root file system, then run resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv to extend your filesystem, and run df -h one more time to make sure you’re successful.

Ubuntu LVM: extend root filesystem resize 2fs

And that’s it. You just allocated the free space left behind by the Ubuntu installer to your root filesystem. If this is still not enough space, continue on to the next section to allocate more space by extending an underlying disk.

Use Space from Extended Physical (or Virtual) Disk

First you need to increase the size of the disk being presented to the Linux OS. This is most likely done by expanding the virtual disk in KVM/VMWare/Hyper-V or by adjusting your RAID controller / storage system to increase the volume size. You can often do this while Linux is running; without shutting down or restarting. I’ve extended my 100GB disk to 200GB for my example machine.

Once that is done, you may need to get Linux to rescan the disk for the new free space. Check for free space by running cfdisk and see if there is free space listed, use “q” to exit once you’re done.

Linux increase disk size space cfdisk

If you don’t see free space listed, then initiate a rescan of /dev/sda  with echo 1>/sys/class/block/sda/device/rescan. Once done, rerun cfdisk and you should see the free space listed.

Linux free partition space scan

Select your /dev/sda3 partition from the list and then select “Resize” from the bottom menu. Hit ENTER and it will prompt you to confirm the new size. Hit ENTER again and you will now see the /dev/sda3 partition with a new larger size.

Select “Write” from the bottom menu, type yes to confirm, and hit ENTER. Then use “q” to exit the program.

Now that the LVM partition backing the  /dev/sda3 Physical Volume (PV) has been extended, we need to extend the PV itself. Run pvresize /dev/sda3 to do this and then use pvdisplay to check the new size.

Ubuntu extend physical volume pvresize

As you can see above, my PV has been increased from 98.5GB to 198.5GB. Now let’s check the Volume Group (VG) free space with vgdisplay.

Ubuntu LVM: check vg space vgdisplay

We can see above that the VG has 100GB of free space. Now let’s check the size of our upstream Logical Volume (LV) using lvdisplay, extend the LV to use up all the VG’s free space with lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv, and then check the LV one more time with lvdisplay to make sure it has been extended.

Ubuntu LVM: check LV size lvdisplay

At this point, the block volume underpinning our root filesystem has been extended, but the filesystem itself has not been resized to fit that new volume. To do this, run df -h to check the current size of the file system, then run resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv to resize it, and df -h one more time to check the new file system available space.

Ubuntu LVM: extend filesystem resize2fs

And there you go. You’ve now taken an expanded physical (or virtual) disk and moved that free space all the way up through the LVM abstraction layers to be used by your (critically full) root file system. Time to check it off the to-do list and move on to the next IT emergency.

REDHAT8-install ftp

 

How to Install, Configure and Secure FTP Server in RHEL 8

FTP (stands for “File Transfer Protocol”) is a standard and old network protocol used for the transfer of files between a client and server on a computer network. It is built on a client-server model architecture, which offers access to files and directories via an FTP client, to upload files to the server as well as download files from it.

In our previous article, we have explained how to install, configure and secure FTP Server in CentOS/RHEL 7 for the transfer of computer files between a client and server on a computer network.

In this article, we will describe how to install, configure and secure an FTP server on RHEL 8 for basic file sharing between computers.

Install FTP Server on RHEL 8

1. To install secure FTP package, use the following dnf command.

# dnf install vsftpd
Install VsFTP in RHEL 8
Install VsFTP in RHEL 8

2. When the installation is complete, you need to start the vsftpd service for the meantime, enable it to start automatically at system boot and then verify the status using the following systemctl commands.

# systemctl start vsftpd
# systemctl enable vsftpd
# systemctl status vsftpd
Start and Verify FTP Service
Start and Verify FTP Service

3. Next, you need to open FTP port 21 on the system firewall to allow access to FTP services from external systems.

# firewall-cmd --zone=public --permanent --add-port=21/tcp
# firewall-cmd --zone=public --permanent --add-port=45073/tcp
# firewall-cmd --reload

Configure FTP Server on RHEL 8

4. To configure an FTP server, you need to take the backup of the main FTP configuration file /etc/vsftpd/vsftpd.conf using the following copy command.

# cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.orig

5. Then open the configuration file using your favorite command line editor.

# vi /etc/vsftpd/vsftpd.conf

Set the following parameters with these corresponding values (see man vsftpd.conf for meanings of the configuration parameters):

anonymous_enable=NO             
local_enable=YES		
write_enable=YES		
local_umask=022		        
dirmessage_enable=YES	        
xferlog_enable=YES		
connect_from_port_20=YES        
xferlog_std_format=YES          
listen=NO   			
listen_ipv6=YES		        
pam_service_name=vsftpd        

6. Next, you need to configure FTP to allow/deny users access to FTP services based on the user list file /etc/vsftpd.userlist.

By default, users listed in /etc/vsftpd.userlist file are denied access with the userlist_deny option set to YES, if userlist_enable=YES, it enables access.

But, setting the parameter userlist_deny=NO alters the setting, meaning that only users explicitly listed in userlist_file=/etc/vsftpd.userlist will be permitted to log in.

Therefore, add the following lines in your vsftpd.conf configuration file (or if there already exist, uncomment them and set their values as shown):

userlist_enable=YES                   # allow access to list of usernames from the userlist_file
userlist_file=/etc/vsftpd.userlist    # stores usernames.
userlist_deny=NO   

7. Now add the following lines in your vsftpd.conf configuration file to restrict FTP users to their Home directories.

chroot_local_user=YES		#means local users will be placed in a chroot jail, their home directory after login by default settings.
user_sub_token=$USER         	
local_root=/home/$USER/ftp   	

Save the changes in the file and close it.

8. Set the following SELinux boolean rule to enable FTP to read/write files of user’s home directory.

# semanage boolean -m ftpd_full_access --on

9. Finally restart the vsftpd service to affect all the changes we made so far above:

# systemctl restart vsftpd

Testing FTP Server on RHEL 8

10. To test if the above FTP setup is running fine, start by creating an FTP user with useradd command and create a password for that user.

# useradd -m -c "Tecmint HowTos" -s /bin/bash tecmint
# passwd tecmint

11. Then add the user tecmint to the file /etc/vsftpd.userlist using the echo command as follows.

# echo "tecmint" | tee -a /etc/vsftpd.userlist
# cat /etc/vsftpd.userlist

12. Then create the alternative local root directory for the user (tecmint, yours is probably different) and set the appropriate permissions on this directory.

# mkdir -p /home/tecmint/ftp
# chown nobody:nobody /home/tecmint/ftp
# chmod a-w /home/tecmint/ftp

13. Next, create a directory inside the local root location, where user’s will keep his/her files.

# mkdir /home/tecmint/ftp/files
# chown tecmint:tecmint /home/tecmint/ftp/files
# chmod 0700 /home/tecmint/ftp/files/

14. Now connect to the FTP server using any FTP client as follows.

# ftp tecmint@192.168.56.100
Sample Output
Connected to 192.168.56.100
220 Welcome to TecMint.com FTP service.
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls

That’s it! In this article, we described how to install, configure as well as secure an FTP server in RHEL 8. In our next article, we will show how to secure FTP server using SSL/TLS connections. Until then, stay with us.