10

Here is the free -m output

[prem@myserver: /home/prem]$ free -m
              total        used        free      shared  buff/cache   available
Mem:            991         218          85         267         687         360
Swap:             0           0           0

I have added swap space to my CentOS 7 machine by creating a swap file using following commands

dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Now the swap space has increased to 2GB

[prem@tuatahi: /home/prem]$ free -m
              total        used        free      shared  buff/cache   available
Mem:            991         284          69         265         638         292
Swap:          2047           5        2042

But I guess in order to make these changes permanent, I need to add fstab entry for my swap space. Here are the contents of fstab

UUID=ef6ba050-6cdc-416a-9380-c14304d0d206 /                 xfs     defaults        0 0

I am not sure how to add the swap space in terms of UUID.

1

2 Answers 2

15

There is no UUID for a file. Simply enter it as:

/swapfile none swap defaults 0 0

Since it's directly on the root filesystem, there's no worry about the mounting order.

1
0

...and to append that line to fstab in one-shot as a non-root user:

$ echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab

The sudo and tee on the right side of the pipe (|) works around the somewhat surprising behavior of the redirect (>>) not preserving sudo.[1]

$ sudo echo '/swapfile swap swap defaults 0 0' >> /etc/fstab
-bash: /etc/fstab: Permission denied

tee bifurcates the output, with one version going to stdout and the other to the file. The -a flag appends rather than replaces similar to >> appending and > replacing.

[1] https://stackoverflow.com/questions/82256/how-do-i-use-sudo-to-redirect-output-to-a-location-i-dont-have-permission-to-wr

2
  • 3
    Welcome to the site, and thank you for your contribution. Please consider if your answer should rather have been an edit to the other answer. Also, readers may wonder why you used the pipe+tee construct instead of executing echo ' ... ' >> /etc/fstab as root user or via sudo; perhaps you could add some explanation.
    – AdminBee
    Feb 26, 2021 at 8:07
  • Thank you for the constructive feedback! I've expanded my answer accordingly.
    – Dan Dye
    Feb 28, 2021 at 3:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .