Double century! Celebrating 200 posts!

Here we are on the 200th block! Kerneltalks journey of 10 months! Crawling web of Linux, Unix & scripts for the goodness of sysadmin!

200 posts! Yay!

And here we are on another milestone 200 posts! Six months ago we hit 100 posts milestone and now double century!

KernelTalks grew big and steadily past few months. Our pageviews increased which almost totaled to 160K as I am writing this article. Our social reach extended too in terms of fan page followers, likes, shares, comments on, or posts. We are also observing daily email subscribers signing up for our newsletters which indicates the reader’s interest in upcoming articles! All these numbers are available here.

In terms of traffic, we observe a significant rise in organic search traffic from search engines which is almost 15-20% of total traffic. SEO tuning, SSL implementation, and speed-boosting with CDN helped us to climb the search engine ladder. Alexa ranking is leaping day by day confirming increasing traffic to blog. Currently holding 380K rank globally. KernelTalks also featured in the top 60 Linux blog list by Feedspot.

KernelTalks gonna complete one year in a couple of months! Considering millions of blogs on the internet and budding new ones every other day, this progress is pretty good for the first year. Hope to see some more good numbers on our first anniversary! Till then have a happy read!


                                                  *   )    )                  )
    +------------------------------------+      ` )  /( ( /(     )         ( /(   (            (
    |                                    |       ( )(_)))\()) ( /(   (     )\())  )\ )   (    ))\
    |    +------> 200 posts! <------+    |      (_(_())((_)\  )(_))  )\ ) ((_)\  (()/(   )\  /((_)
    |                                    |      |_   _|| |(_)((_)_  _(_/( | |(_)  )(_)) ((_)(_))(
    |         www.kerneltalks.com        |        | |  | ' \ / _` || ' \))| / /  | || |/ _ \| || |
    |                                    |        |_|  |_||_|\__,_||_||_| |_\_\   \_, |\___/ \_,_|
    +------------------------------------+                                        |__/



XFS filesystem in Linux

Learn about the XFS filesystem in Linux. An article explaining what is xfs, features, and commands of XFS. Also, how to migrate from EXT to the XFS file system.

Learn XFS filesystem

What is xfs?

XFS is a high-performance file system designed by Silicon Graphics Inc in 1993. It is the default file system in RHEL7. XFS supports parallel IO operations because of its allocation group structure which makes it high performance operating file system. Journaling features helps it in faster data recovery in case of a crash. Lets quickly walk through a few aspects of XFS for your understanding.

Features of XFS

  • Its 64-bit file system with a max file system size of 8EB.
  • Supports metadata journaling which helps in faster data recovery in case of a system crash.
  • It can be extended while mounted and active.
  • It is internally partitioned into allocation groups. 4 different allocation groups available.
  • Directory quotas help to limit quota over the directory tree.
  • Quota journaling avoids quota consistency checks after the crash and hence quicker recovery
  • Extended attributes associated with each file. Those are additional name/value pairs.
  • Online de-fragmentation is supported.
  • It has native backup (xfsdump) & restore (xfsrestore) utilities.

How to upgrade EXT to XFS

The obvious question is how to upgrade from ext4 to xfs? or upgrade from ext3 to xfs etc. We have different ways to upgrade ext file systems but there is no full-proof way to upgrade ext to xfs. You can have below approach to migrate from ext to xfs filesystem –

  1. Create a new xfs file system
  2. Copy over data from old ext file system to xfs using copy or rsync
  3. Remove old ext file system

Commands for XFS file system management

  • Create xfs file system : mkfs.xfs command.
  • Mount xfs file system : No extra switch for mount command
  • Quota management : xfs_quota command
  • Extending file system : xfs_growfs command
  • Repair file system : xfs_repair command
  • Suspend write on file system : xfs_freeze command (-f for suspend, -u for resume)
  • Bakcup : xfsdump command
  • Restore : xfsrestore command
  • Print file system information : xfs_info command
  • De-fragmentation : xfs_fsr command with no argument
  • Debug mode : xfs_db command

We covered few of these commands in detail in our next article.

Conclusion

XFS is a high-performance filesystem available in most Linux distro and as a default in some. We will see the difference between ext3, ext4, and xfs in upcoming articles along with the XFS command in detail.

Upgrade EXT filesystem in Linux (RHEL, CentOS, Ubuntu)

Learn how to upgrade the ext filesystem in Linux like RHEL, Centos, Ubuntu, etc. Steps for upgrading ext2 to ext3, ext3 to ext4, and ext2 to ext4.

Upgrade filesystem from ext2 to ext3 to ext4

In our last article we saw the difference between ext2, ext3, and ext4 file systems in Linux. In this article we will walk through how to upgrade ext2 to ext3 or ext4 on RHEL, CentOS, or Ubuntu.

How to upgrade ext2 to ext3

To upgrade ext2 volume lets first format volume with ext2 filesystem.

# mke2fs /dev/vg01/lvol0
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 53248 1k blocks and 13328 inodes
Filesystem UUID: b3c39dcf-e00f-414c-8306-46d985674b6e
Superblock backups stored on blocks:
        8193, 24577, 40961

Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done

Lets confirm file system type which we just formatted.

# df -T /mydata
Filesystem             Type 1K-blocks  Used Available Use% Mounted on
/dev/mapper/vg01-lvol0 ext2     51559   842     48055   2% /mydata

You can see its ext2 type in above output under second column.

Now to upgrade it to ext3 you need to unmount it, so there is downtime involved. After un-mount use tune2fs command to upgrade and mount it.

# umount /mydata

# tune2fs -j /dev/vg01/lvol0
tune2fs 1.42.13 (17-May-2015)
Creating journal inode: done

# mount /dev/vg01/lvol0 /mydata

#  df -T /mydata
Filesystem             Type 1K-blocks  Used Available Use% Mounted on
/dev/mapper/vg01-lvol0 ext3     47463   859     43942   2% /mydata

Observe above outputs carefully. We did –

  • Un-mount filesystem
  • Run tune2fs command with -j switch
  • Mount file system

ext2 has been upgraded to ext3 which can be confirmed in last output.

Make sure you make changes in /etc/fstab to reflect proper file system type for your related mount point.

How to upgrade ext3 to ext4

To upgrade to ext4 filesystem you need to enable file system features on the file system with tune2fs command. Those are – dir_index (Speed up directory lookups), extents (Stores locations in inodes), uninit_bg (reduce e2fsck time).

After successful setting features of file system you need to run e2fsck to fix file system data structures on disk for new modified features. To run e2fsck you have to unmount the file system.

# tune2fs -O extents,uninit_bg,dir_index /dev/vg01/lvol0
tune2fs 1.42.13 (17-May-2015)

# e2fsck -pf /dev/vg01/lvol0
/dev/vg01/lvol0 is mounted.
e2fsck: Cannot continue, aborting.

# umount /mydata

# e2fsck -pf /dev/vg01/lvol0
/dev/vg01/lvol0: 11/13328 files (0.0% non-contiguous), 6644/53248 blocks

# mount /dev/vg01/lvol0 /mydata

# df -T /mydata
Filesystem             Type 1K-blocks  Used Available Use% Mounted on
/dev/mapper/vg01-lvol0 ext4     47463   859     42878   2% /mydata

In the above output, we tried to run FS check while volume is mounted and it gave us an error. Finally, the file system upgraded to ext4 which can be verified in the last output. Make a note that this is irreversible change and you can not go back to ext3 once you did this upgrade.

There is another way as well to have a file system of ext4 as below –

ext4 file system is backward compatible with ext2 and ext3. This means you can directly mount any ext2 or ext3 filesystem as ext4 without upgrading. You just simply un-mount filesystem and mount it as ext4 by supplying -t switch with mount command.

# umount /mydata

# mount -t ext4 /dev/vg01/lvol0 /mydata

# df -T /mydata
Filesystem             Type 1K-blocks  Used Available Use% Mounted on
/dev/mapper/vg01-lvol0 ext4     47463   859     43942   2% /mydata

In the above output, we un-mount out ext3 mount point and remounted it as ext4! This process is reversible means you can un-mount the ext4 file system and re-mount it as ext3 again.

How to upgrade ext2 to ext4

As we saw above you need to enable file system features to upgrade to ext4. While upgrading from ext2 to ext4 you need to enable has_journal extra features than the above list. Since you know ext2 doesn’t have journaling available.

# umount /mydata

# tune2fs -O extents,uninit_bg,dir_index,has_journal /dev/vg01/lvol0
tune2fs 1.42.13 (17-May-2015)
Creating journal inode: done

# e2fsck -pf /dev/vg01/lvol0
/dev/vg01/lvol0: 11/13328 files (0.0% non-contiguous), 6627/53248 blocks

# mount /dev/vg01/lvol0 /mydata

# df -T /mydata
Filesystem             Type 1K-blocks  Used Available Use% Mounted on
/dev/mapper/vg01-lvol0 ext4     47463   842     42895   2% /mydata

Make a note that this is irreversible change and you can not go back to ext3 once you did this upgrade.

Another method would be directly mounting FS as ext4 as below –

As stated earlier, the ext4 file system is backward compatible with ext2 and ext3, means you can directly mount any ext2 or ext3 filesystem as ext4 without upgrading

# df -T /mydata
Filesystem             Type 1K-blocks  Used Available Use% Mounted on
/dev/mapper/vg01-lvol0 ext2     51559   842     48055   2% /mydata

# umount /mydata

# mount -t ext4 /dev/vg01/lvol0 /mydata

# df -T /mydata
Filesystem             Type 1K-blocks  Used Available Use% Mounted on
/dev/mapper/vg01-lvol0 ext4     51559   842     48055   2% /mydata


Previously ext2 file system is mounted as ext4 in the above output without upgrading!

How to check if Linux server is physical or VM or cloud

Learn how to check if the Linux server is a physical or VM or cloud server by observing ‘dmidecode’ command output fields.

Identify physical or VM or cloud server

Recently one of my friends came across a situation where he didn’t know logged in the server is physical server, virtual machine, or cloud server. So here in this article we will walk you through how to check if the server is a physical server or virtual machine. Since cloud servers are also VM there is a very thin line between identifying them as different. It takes some infrastructure knowledge and sense to differentiate between VM and cloud-hosted servers.

dmidecode command helps you through this process. This command helps you to identify the manufacturer of the system. Let’s look at its output :

root@kerneltalks# dmidecode -t system
# dmidecode 3.0
Scanning /dev/mem for entry point.
SMBIOS 2.4 present.

Handle 0x0100, DMI type 1, 27 bytes
System Information
        Manufacturer: Xen
        Product Name: HVM domU
        Version: 4.2.amazon
        Serial Number: ec27c3cb-8e3b-8a30-4325-3f388b0fxxxx
        UUID: EC27C3CB-8E3B-8A30-4325-3F388B0Fxxxx
        Wake-up Type: Power Switch
        SKU Number: Not Specified
        Family: Not Specified
-----output trimmed-----

This is output from the AWS cloud server. In the above output you have to observe the System Information‘ section.

Manufacturer :

If you see Xen, VMware, Red Hat, etc here its a VM (virtual machine). VM host KVM also shows the same manufacturers. Anything other than these like Dell Inc, HP, etc (hardware manufacturing companies name) shows that you are on real physical hardware means its physical server.

Product Name :

This parameter also helps further to confirm it’s physical or VM. Here HVM denotes its virtual machine clearly. For a physical server you see the physical server model name here.

Version :

Its again narrowing down your search. Word amazon here denotes its a VM from Amazon (most likely AWS). So as I said if you know your infrastructure well, so must be knowing you have AWS servers or not. And hence you can be sure here that it’s AWS cloud server.

Even serial numbers can be a confirmation but you should have in-depth knowledge of hardware serial number nomenclature used by industry manufacturers. Mostly, physical hardware has much short, clean and human-readable serial number whereas VM has long serial which starts with virtual service name (eg, ec2 for AWS cloud, VMWare for VMware VM, etc.) This can be added confirmation and not the primary parameter for validation.

For more understanding lets look at output from virtual machine :

root@kerneltalks_vm1# dmidecode -t system
# dmidecode 2.12
SMBIOS 2.4 present.

Handle 0x0001, DMI type 1, 27 bytes
System Information
        Manufacturer: VMware, Inc.
        Product Name: VMware Virtual Platform
        Version: None
        Serial Number: VMware-56 4d c7 47 54 09 d6 39-xx xx xx xx xx xx xx xx
        UUID: 564DC747-xxxx-D639-xxxx-xxxxxxxxxx
        Wake-up Type: Power Switch
        SKU Number: Not Specified
        Family: Not Specified
-----output trimmed-----

In above output,

The manufacturer is VMware Inc. which denotes its a VM. Product Name VMware Virtual Platform clearly states its VM

Like the cloud server above we do not have version value here. But the above two values are sufficient to confirm its a VM. These values put up by hardware or system image manufacturer hence can be blank at times.

Finally, before signing off lets check output from physical machine as well –

root@kerneltalks_phy # dmidecode -t system
# dmidecode 2.11
SMBIOS 2.7 present.

Handle 0x0001, DMI type 1, 27 bytes
System Information
        Manufacturer: Cisco Systems Inc
        Product Name: UCSB-B200-M3
        Version: 1
        Serial Number: XXXXXXXXXX
        UUID: 00000000-0000-0000-0000-000000000000
        Wake-up Type: Other
        SKU Number:
        Family:
-----output trimmed-----

Here,

The manufacturer is Cisco System Inc (who is hardware manufacturer) confirming its server hosted on bare metal.

Product Name is a UCSB-B200-M3 which is the model name of Cisco Blade. Confirms server is Cisco Blade hardware (Physical server)

Checks submitted by readers

Few readers posted these commands on social media comments.

lscpu: One can observe hypervisor vendor determine if its virtual or physical server

# lscpu |grep -i hypervisor
Hypervisor vendor:     Xen

hostnamectl: Works on a new distro like RHEL7. The virtualization field gives you an idea about if its physical or virtual.

root@kerneltalks # hostnamectl status |grep -i virtual
    Virtualization: xen

dmesg: Check for words like KVM, hvm, or virtual in boot messages. Here Xen virtualization detected confirming its a virtual box.

# dmesg |grep -i virtual
[    0.000000] Booting paravirtualized kernel on Xen HVM
[    1.403040] systemd[1]: Detected virtualization xen.
[    1.889088] xen_netfront: Initialising Xen virtual ethernet driver

Conclusion

System information like manufacturer, product name, version, the serial number can be used to observe and understand whether logged in the server is a physical server or VM (physically hosted or cloud).

If you have any other tricks or solid methods to confirm the server is physical or virtualized, please let us know in the comments below.

Difference between ext2, ext3 and ext4

List of differences between ext2, ext3, and ext 4 Linux file systems. One of the Linux interview questions answered in this article!

Comparison : ext2 vs ext3 vs ext4

This is another Linux interview question. What is the difference between ext2, ext3, and ext4 file systems? Or Explain Linux file system ext2 vs ext3 vs ext4? In this article we will walk through these differences and lastly I will present you all of them in tabular format so that they are easy to quickly read during your preparations.

Let’s see each file system’s features and lastly their comparison with other file systems.

EXT2 file system

  • It’s a second extended file system that was created to overcome limitations of the EXT file system.
  • Introduced in 1993 by Remy Card. It was the first commercial-grade filesystem for Linux
  • Does not supports Journaling
  • Fit for SD cards & USB drives since it has high performance and low writes (as journaling is not available). USB and SD storage are limited with write cycles hence its best fit for them.
  • Limits: Individual file size 16GB to 2TB. File system size 2TB to 32TB.

Limits are calculated based on block size used.  Block size varies from 1KB to 8KB. For example,  If 1KB block size is used max file size can go up to 16GB and for 8KB it’s 2TB. Middle range sizes being 2KB and 4KB which has file size limits of 256GB & 2TB (not mentioned in above limits) respectively. The same applies to the File system size limits defined above.

EXT3 file system

  • It’s third extended file system was created to overcome limitations of the EXT2 file system.
  • Introduced in 2001 by Stephen Tweedie. It was the most common filesystem in any Linux distro.
  • Supports Journaling
  • Journaling keeps track of file changes which helps in fast recovery and reduce chances of data loss in case of a system crash
  • Limits: Individual file size 16GB to 2TB. File system size 4TB to 32TB.
  • Upgrading FS from ext2 to ext3 is an online process without downtime.

EXT4 file system

  • It’s the fourth extended file system that was created to overcome limitations of the EXT3 file system.
  • Introduced in 2008 by a team of developers. Its most the latest filesystem in ext family.
  • Supports Journaling
  • Lots of new features introduced. Extents, Backward compatibility, Persistent pre-allocation, Delayed allocation, Unlimited number of subdirectories, Journal checksum, Faster FS check, Transparent encryption.
  • Limits: Individual file size 16GB to 16TB. File system size up to 1EB.
  • Upgrading FS not needed. Due to backward compatibility, ext2, ext3 can be directly mounted as ext4.

All above points can be formatted in tabular format as below :

ParameterEXT2EXT3EXT4
Introduced year

199320012008
Developed by

Remy Card Stephen Tweedie Team of developers
Journaling Not available Available Available
Individual file size

16GB to 2TB 16GB to 2TB 16GB to 16TB
File system size

2TB to 32TB 4TB to 32TB up to 1EB
Upgrade Can be done online to EXT3. Can be mounted as EXT4. No upgrade needed Can be mounted as EXT4. No upgrade needed NA

Get your Linux ISO download from these URLs

List of all Linux ISO download links. The latest and old releases of various Linux distro can be downloaded using links listed on this page.

Linux download links

Many times I come across questions from people or readers that where I can download RHEL iso? Where to download Ubuntu Linux? etc. Actually these download links are very easy to get from Google but still I thought of publishing them all in one place.

In this post, I will list all possible Linux download websites. Please note that all Linux distro’s server copy is taken into consideration for choosing download links.

  1. Red Hat Enterprise Linux RHEL latest release download link.  Old releases download link. You have to create a developer account here to get your ISO copy.
  2. CentOS Linux latest release download link. Old releases download link. No signup needed.
  3. Fedora Linux download link. No signup needed.
  4. Debian Linux download link. There are country wise FTP mirrors listed. Choose your nearest one and download it.
  5. Ubuntu Linux Latest release download link. Old releases download link
  6. Arch Linux download link. Country-wise HTTP mirror list.
  7. Kali Linux latest release download link. Old releases download link
  8. Linux mint latest release download link. Old releases download link
  9. OpenSUSE Linux latest release download link
  10. SUSE Linux 60 days trial download link
  11. Open Mandriva Linux download link
  12. Kubuntu Linux download link
  13. Bodhi Linux download link
  14. Gentoo Linux download link

The list will go on since Linux is open source and there are many distros floating around the internet! Report broken links and any additions in the comments below.

7 new features in RHEL7!

Listing new features in RHEL7. These 7 new features making RHEL7 stand out from its predecessor. 

New features in RHEL7

Its been a while RHEL7 is launched and nicely accommodated in Linux world by now. What’s new in RHEL7? What is the difference between RHEL7 and its precedence versions? these kinds of questions are flowing through interviews these days. So thought of jotting them down. In this post I will quickly walk through some new key features launched in RHEL7 by Red Hat.

What’s new in RHEL7?

RHEL7 officially released in June 2014 with codename Maipo. It took time for the market to absorb this new release since there are many new features launched in this release. Red Hat launched many new ways, commands to do traditional stuff. We are going to see them now –

  1. The default file system is XFS. RHEL6 was launched with EXT4 as the default file system. XFS is a highly scalable, high-performance file system. XFS supports metadata journaling, which helps in quicker crash recovery. This means file system checks will take very little time. The XFS file system can be defragmented and extended while mounted. This makes it more admin friendly in a production environment since it avoids downtime of file systems for activities. Supports only 64 bit systems.
  2. Introduction of systemctl. A new way to manage services on RHEL7 is systemctl. Older service and chkconfig command is being replaced with systemctl.
  3. Run levels being called as targets. In RHEL7 run levels are called targets. Default target (run-level) is defined in /etc/systemd/system/default.target
  4. Fast boot. RHEL7 boots faster than its predecessors. This is achieved by simultaneously starting services that are not dependent on each other. In older versions, services used to start one after another. So if one service stuck or delays to start it subsequently delays the following process and boot time. This hurdle is removed in RHEL7 allowing it to boot much faster.
  5. New system service manager: systemd. The former init process is no more PID 1 or first process. Systemd is introduced which controls standard base init scripts.
  6. Bigger filesystem limits. RHEL7 now supports filesystem size up to 500TB. This limit is also the same for an individual file. This is due to the XFS file system on a 64-bit machine. RHEL6 supports the 16TB filesystem.
  7. New firewall identity. Former firewall iptables now replaces by firewalld (Firewall Dynamic). iptables still exist in the system and you can disable firewalld and use iptables.

These are key differences in the new RHEL7 release. apart from this there are many new things added in RHEL7. The whole list is compiled by Red Hat here.

Let us know your suggestions/feedback in comments section below!

How to unmount NFS when the server is offline

Learn how to unmount NFS when the server is gone. Dead NFS mounts can be un-mounted using forceful and lazy umount command.

Unmount NFS share when server is gone

This article will help you to un-mount NFS share from the client when the NFS server is gone or offline or un-available or decommissioned. We have seen how to configure the NFS server and how to handle NFS stale file error. But what if your NFS server is gone and its shares are still mounted on clients. Normally, before shutting down the NFS server, all clients should be notified and advised to unmount NFS shares they are using from this server.

But, in case if any of the clients have still NFS mounted when the server goes down, then the client should forcefully un-mount it. Normal mount operation won’t be effective in such cases. When the NFS server is down, you observe below things on the client who has NFS share still mounted.

  1. df command hangs since it tries to fetch NFS information but the NFS server is not responding.
  2. Tools, utilities who use/check mount point information like Ignite backup shows below error.
    NFS server xyz not responding still trying
  3. fuser command hangs when running for the NFS mount point.
  4. umount (normal) command fails with the below error.
    root@kerneltalks # umount /data nfs umount: nfs_unmount: /data: is busy umount: return error 1. 

In this case, you need to use forceful (-f switch) and lazy umount (-l switch) to un-mount this dead NFS mount point. Lazy un-mount detach the said mount point from file system tree and cleans its all references once it’s not busy anymore.

Lazy un-mount is available in most Linux distributions. If not, you should be fine with only forceful un-mount too. In HPUX lazy un-mount is not available.

root@kerneltalks # umount -f -l /data

Conclusion

You can identify dead NFS share by df, fuser commands that appear to hang, failing to umount command. Such a dead NFS mount point can be un-mounted using forceful and lazy umount command.

How to replay Linux session recorded by the script command

Learn how to replay the Linux session recorded by script command. Visual Linux session recording along with timing information plays past session recording in real-time.

Replay Linux session like video!

In our last article we learned how to record Linux sessions using the script’ command. In this article we will walk through steps to replay recorded sessions by script command. Normally, script command saves recording in the plain text log file which can be viewed using cat, more, less, or vi commands. That would be only plain text having commands and their outputs in the order you executed them while recording.

If you want to view your recorded output as it is being played on the terminal you can do it using scriptreplay command. It will play your output just as you are typing it on the terminal! scriptreplay needs time logs as well to play recorded sessions. This time logs can be generated using –timing switch with the script command. Let’s walk through these steps.

How to record Linux session with timing

We will use script command with --timing switch followed by filename in which all timing logs will be saved.

[root@kerneltalks ~]#  script --timing=time.log capture.txt
Script started, file is capture.txt
[root@kerneltalks ~]# date
Thu Jul 27 02:42:46 EDT 2017
[root@kerneltalks ~]# hostname
kerneltalks
[root@kerneltalks ~]# echo "I love kerneltalks"
I love kerneltalks
[root@kerneltalks ~]# exit
exit
Script done, file is capture.txt

here we are saving timing information in time.log file and session recording in capture.txt file. Both are plain text files and can be viewed. If you look at time.log file :

[root@kerneltalks ~]# cat time.log
0.001666 53
0.009220 1
3.980549 1
0.103633 1
0.191978 1
0.096629 2
0.127128 75
0.000920 1
1.671676 1
0.143421 1
0.080831 1
0.152510 1
----- output trimmed----

It has two columns of data in it. The first column denotes the number of seconds elapsed after the last display action. The second column is the number of characters printed on the screen.

That’s it. You have recorded your Linux session along with timing logs. This recording (capture.txt) can be replayed using scriptreplay command since its timing information is available.

How to replay recorded Linux session

Now both logs timing and recording need to feed scriptreplay command to let the show begin! The format and switch would be the same. Command used to replay session will be :

# scriptreplay --timing=time.log capture.txt

To see it in action, I captured it in the GIF file below. See how to actually replay as if the user is typing in the terminal as it was at the time of recording!

‘scriptreplay’ command in action

It replays exactly with the same time difference between two commands as you did at the time of recording! It’s like watching what the user has done in his session in real-time. It’s more of a visual record of Linux session while script was textual records.

Record Linux session using the script command

Learn how to record Linux sessions using script command. This will help to keep a record of your commands and their outputs printed on the terminal at the time of execution.

Record your Linux session using ‘script’ command

In our last article we saw how to save PuTTY session output in a file on your desktop. In this article we will learn how to record session output in a file on the Linux server itself. And yes, it’s recording. It can be replayed later at any time and view commands being run and output being shown as it was done in real-time at the time of recording.

There are two utilities used for this task. script command used to record session and scriptreply is used to replay the recorded session from the file. In this article we will see Linux session recording using script command. scriptreply command is covered in this article.

script command takes a filename as an argument without any switch. It will write your commands along with their outputs in this file. Remember only commands ran after script command will be recorded and till you stop recording by hitting cntl+d key combination or typing exit. Make sure you have proper file permissions on the log file you are asking script to write into. Also, make sure you have enough space for recording since if your outputs are pretty lengthy then logfile gonna grow big.

Let’s start with an example. Here we are recording output in myoutputs.txt file. Command will be script myoutputs.txt:

[root@kerneltalks ~]# script myoutputs.txt
Script started, file is myoutputs.txt
[root@kerneltalks ~]# date
Thu Jul 27 01:31:39 EDT 2017
[root@kerneltalks ~]# hostname
kerneltalks
[root@kerneltalks ~]# w
 01:31:46 up 1 min,  2 users,  load average: 0.49, 0.21, 0.08
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ec2-user pts/0    59.184.148.123   01:31    2.00s  0.00s  0.19s sshd: ec2-user [priv]
root     pts/1                     01:31    2.00s  0.01s  0.01s w
[root@kerneltalks ~]# exit
Script done, file is myoutputs.txt

Now, observe above output.

  1. After script command execution, it notifies you (Script started, file is myoutputs.txt) that it has started recording your session and log file where it is recording.
  2. Then I punched in few commands for testing like date, hostname, w.
  3. I stopped recording by hitting the cntl+d key combination (exit command works too). script stopped and informed (Script is done, the file is myoutputs.txt) it has stopped recording.

Lets look at logfile myoutputs.txt

# cat myoutputs.txt
     Script started on Thu 27 Jul 2017 01:31:35 AM EDT
     [root@kerneltalks ~]# date
     Thu Jul 27 01:31:39 EDT 2017
     [root@kerneltalks ~]# hostname
     kerneltalks
     [root@kerneltalks ~]# w
      01:31:46 up 1 min,  2 users,  load average: 0.49, 0.21, 0.08
     USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
     ec2-user pts/0    59.184.148.123   01:31    2.00s  0.00s  0.19s sshd: ec2-user [priv]
     root     pts/1                     01:31    2.00s  0.01s  0.01s w
     [root@kerneltalks ~]# exit

     Script done on Thu 27 Jul 2017 01:31:51 AM EDT

Voila! All commands (including shell prompt PS), their outputs are there in the logfile. Notice that it also added timestamps at the top and bottom of the logfile indicating when the recording was started and stopped.

How to append script command recording in same logfile

Every time script command runs, it empties its logfile and adds content to it. If you want to append your recording to previously recorded file then you need to use -a (append) switch with it. This will keep data in a log file as it is and add new data to the bottom of the file.

[root@kerneltalks ~]# script -a myoutputs.txt
Script started, file is myoutputs.txt
[root@kerneltalks ~]# echo " I love kerneltalks.com"
 I love kerneltalks.com
[root@kerneltalks ~]# exit
exit
Script done, file is myoutputs.txt

I used -a switch in the above example to append new recording in the same file we created earlier. Tested one echo command. Let’s check the logfile.

[root@kerneltalks ~]# cat myoutputs.txt
     Script started on Thu 27 Jul 2017 01:31:35 AM EDT
     [root@kerneltalks ~]# date
     Thu Jul 27 01:31:39 EDT 2017
     [root@kerneltalks ~]# hostname
     kerneltalks
     [root@kerneltalks ~]# w
      01:31:46 up 1 min,  2 users,  load average: 0.49, 0.21, 0.08
     USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
     ec2-user pts/0    59.184.148.123   01:31    2.00s  0.00s  0.19s sshd: ec2-user [priv]
     root     pts/1                     01:31    2.00s  0.01s  0.01s w
     [root@kerneltalks ~]# exit

     Script done on Thu 27 Jul 2017 01:31:51 AM EDT
     Script started on Thu 27 Jul 2017 01:41:13 AM EDT
     [root@kerneltalks ~]# echo " I love kerneltalks.com"
      I love kerneltalks.com
     [root@kerneltalks ~]# exit
     exit

     Script done on Thu 27 Jul 2017 01:41:24 AM EDT

You can see there are two recordings in the same log file. You can distinguish them by their start and stop time (highlighted in output)

Conclusion :

script command can be used to log commands and their outputs on the Linux server itself. This command can be used in user profiles to monitor user activity on a server provided logfile mount point has huge free space to accommodate data generated by users.