Tag Archives: command line

Manage PDF files

Merge multiple files into single PDF

I’m sure that we all had the need to send a single PDF file, maybe a signed contract. Yes, those 20 or more pages that you need to return, probably with just two of them filled up and signed.

Some PDF give you the ability to digitally sign them. But in my experience, most of them aren’t so modern.

So, what do I do?

I print ONLY the pages that I need to sign, scan them and here I am, with the need to “rebuild” the PDF, replacing the pages signed.

Example.
You have the file contract.pdf, with 20 pages and you need to sign page 10 and page 20.
The scan has a different resolution (or, even worse, it’s a different format, like jpg).

Here the command to make the magic happen:

The bit before -resize is pretty self explanatory. The bit after is a way to have the size of all pages fitting an A4 format, with a good printable resolution.

Of course, to make this happen, you need Linux (or WSL on Windows 10) and imagemagick installed.

Another way is using ghostscript.

A simple Ghostscript command to merge two PDFs in a single file is shown below:

What about a quick onliner to reduce and convert to grayscale your pdf?

PDF size reduce

Sometimes instead, you need to reduce the size of an existing PDF. Here a handy oneliner, using ghostscript:

Other options for PDFSETTINGS:

  • /screen selects low-resolution output similar to the Acrobat Distiller “Screen Optimized” setting.
  • /ebook selects medium-resolution output similar to the Acrobat Distiller “eBook” setting.
  • /printer selects output similar to the Acrobat Distiller “Print Optimized” setting.
  • /prepress selects output similar to Acrobat Distiller “Prepress Optimized” setting.
  • /default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.

Happy PDF’ing šŸ™‚


Sources:
https://stackoverflow.com/questions/23214617/imagemagick-convert-image-to-pdf-with-a4-page-size-and-image-fit-to-page
https://www.shellhacks.com/merge-pdf-files-linux-command-line/

https://gist.github.com/firstdoit/6390547

TOP – memory explanation

(just few notes – to avoid to forget)

  • VIRT: not really relevant nowadays. It’s the memory that the process could use. But the OS loads only what needed, so rarely really used. On 32bit OS, it could be the only time when you need to keep an eye as the OS can allocate up to 2-3GB only.
  • RES: Resident Set Size memory – this is the actual memory in RAM. On low used machines, it might still show high usage even if not utilised as the process to free-up the memory costs more than leaving it. In fact, Linux OS tends to use as much memory available (“unused memory is wasted memory“).
  • SH: this is the shared memory which generally contains libraries etc

LVM – How to

Intro

LVM is a very powerful technology, and can really help the Sysadmin’s life.
However, this is something that we generally setup at the beginning (most of the time now it’s automatically setup during the installation process), and it’s well know… when we stop using something, we tend to forget how to use it.

This is why I’m writing this how to, mostly to keep track of the major features and commands, in case I will need them again in the future šŸ˜‰

Before proceeding, please digest the following journey of this poor physical device that gets abstracted up to usableĀ pieces.

 

 

Prepare partions

First of all, we need to find which device(s) we want to setup for LVM

fdisk -l

We can see 3 md devices, probably RAID devices. These are the ones that we are going to use for our LVM exercise.

Now, let’s create an LVM partition.

fdisk <device>Ā => n , p , 1 , (enter) , (enter) , t , 8e , w

Do the same for all the devices that you want to use for LVM. In my example, I’ve done this for /dev/md1, /dev/md2 and /dev/md3.

Shortcut (risky but quicker) šŸ™‚

All seems now good to go: we have Linux LVM partitions!

Time to start to configure LVM

Configure LVM

First of all, we need to make these Linux LVM partition able to be part of a group (vg). I always find tricky to remember the logic behind. Let’s imagine that the device itself now is just labelled “Linux LVM” but we need toĀ initiateĀ it in somehow.

pvcreate <dev>

Now theseĀ guys are ready to be part of a group. In this case a Virtual Group (vg).
Let’s check that it’s actually true:
pvs

Time to create a group with these devices (this could be done also with just a single device):

vgcreate <lvmgroupname> <dev> <dev> …

Now, let’s check again with pvs and vgs

Now pvs shows the VG group no longer empty but withĀ mylvmvg. And vgs tells us that the VG is about 14GB in size, fully free with no LV in it.

Good! Now, let’s make some LVs (logical volumes). These will be the newĀ “partitions/disks” that we will be actually able to format, mount and use! šŸ™‚

lvcreate -n <name> -L xGB <vg_group_name>

Some checks to verify:

A new LV appears inĀ vgs andĀ lvs shows the 2GB volume that we have created.

Let’s create another one, but this time, using the full remaining space (using -l 100%VGĀ option instead of -L xGB)

Magic!

Now, we have two devices, both ‘a’ -> active and ready to be formatted:
mkfs.ext4 <device>

I’ve done this for /dev/mylvmvg/part1 and /dev/mylvmvg/part2.

Let’s create the mount points and mount them:

As you can see, the devices are appearing now asĀ /dev/mapper/mylvmvg-partX. You can use eitherĀ /dev/mylvmvg/partX orĀ /dev/mapper/mylvmvg-partX. Theoretically, theĀ mapper one is recommended (my bad!).

Now the 2 devices are ready to be used as a typical disk/partition formatted with ext4 filesystem.


Resize Logical Volume

Now, imagine thatĀ part1 is too small, and you need more space. And luckily, yourĀ part2 volume has plenty. Is there any way to “steal” some space fromĀ part2Ā and give it toĀ part1?
Ooohh yesss! šŸ™‚

How?

  1. shrinkĀ part2 logical volumeĀ AND its filesystem
  2. expandĀ part1 logical volume AND its filesystem

Here the comments inline:

 

Move logical volume onto a new RAID array

Now, let’s imagine that one of the 3 initialĀ md devices are having problems, or simply we want to move on a faster/bigger raid array.
TheĀ magic of LVM is that we can actually do this with NO DOWNTIME!

How?

In this example we assume that a new /dev/md10 device is attached to our server and we want to removeĀ /dev/md2 device.

  1. We need to take the new device and go through all the previous steps:
    1. fdisk
    2. pvcreate
  2. After that, we need to add thisĀ initialised device in the existing volumeĀ group (vg)
  3. Move whatever is stored on the physical device
  4. Shrink the volume group
  5. Remove the device

Now where the new bits are starting:
pvmove, vgreduce, pvremove

 

In this example we have left LVM to decide where to put the data that was stored onĀ /dev/md2 device.
Just for reference, we could have specified the destination physical device (e.g. if we were thinking to remove more devices and make sure that the data was ending up on the new RAID and not sprat across the other disks):

Or, in case we just wanted to move a specific logical volume, let’s sayĀ part1

 

…happy LVM’ing! šŸ˜‰

Varnish – basic notes

 

Sophos antivirus notes

Generic checks

Scan

Example for multiple folders with final report:

(suggested to run in a screen session)

  1. Create a temporary folder:
  2. list allĀ directories that you want to scan (full path) into a file calledĀ list_folder.txtĀ within the temp folder;
  3. Run the following:
  4. Check report.txtĀ 

 

Plesk notes

 

 

Atop – notes

 

ftp/sftp – vsftpd

 

One liners to automatic creation of username and passwords

Automatic creation of users/passwords (FTP)

Manually create list.txt with user:doc_root
e.g.:

Get commands to create FTP users

 

Get commands to set FTP permissions (if doc_root exists already)

 

Generate and Assign random passwords to the users.

 


Create a list of vhosts’ paths: vhosts.txt

Example with only .com domains:
/var/www/domain1.com
/var/www/domain2.com
/var/www/domain3.com

Use a regex for sed to extract the vhost name, removing dots (example based on the example above)
This will return a list of PATH and VHOSTNAME. We will use VHOSTNAME as USER for that path

 

Print out the commands to run to add FTP users (no SSH)
Once checked the output, run these lines

(for sftp only):

 

This will print out commands to run to assign user:apache_group to the vhosts’ paths

(for sftp only):

 

Set g+s on vhosts to preserve directory owner
[TO CHECK]

[THIS EXECUTE]

 

Create list of random passwords using pwgen

 

Create list of random passwords using openssl

 

Apply these passwords automatically

 

Print output forĀ reference