Linux command to create out of memory killer (OOM)

In linux ,when virtual memory(physical memory + swap) is used up ,OOM will kill one process to free memory.You can try below command to simulate an OOM

tail /dev/zero

After a while above command will use up all your virtual memory and been killed by OOM ,like below

user01@host01$ tail /dev/zero
Killed
user01@host01$

All you can get the relevant log messages via command dmesg

Out of memory: Kill process 15297 (tail) score 947 or sacrifice child
Killed process 15297, UID 1000001 (tail) total-vm:21404752kB, anon-rss:15804388kB, file-rss:4kB

Shell command to create physical memory usage

No matter for what reason ,we as admin sometimes want to simulate memory usage . There have many ways to do this ,below one is to use shell command ,so we don’t need additional tools or code by specific program language.

yes |tr \\n a |head -c $((1024*1024*100)) | grep x

Above command will create 100M memory usage . 1024*1024 means M , and 1024*1024*1024 means G , you can specify what you want

In case you want to simulate use up all physical memory you can specify a number which is bigger than your total physical memory.

If you wan to let this occurs for period , you can try below command.

cat <(yes |tr \\n a |head -c $((1024*1024*1024*2)) ) <(sleep 100) |grep x

This command will simulate occupying 2G physical memory for 100 seconds.

Note: There is no SPACE between the “<” and “(“

Linux command to create 100% cpu loading

We can use below command to generate cpu loading in linux

yes > /dev/null &

Notes:

  • “yes” is a command to print “yes” to screen repeatedly
  • /dev/null is data sink ,which means the output of command “yes” will be discareded
  • “&” means let this command to be running in background
  • One above command will use up one cpu core (or Hyper Thread)

You can use command “cat /proc/cpuinfo” to check how many cpus you have, eg: if you have 4 cpus (probably 1 cpu with 2 cores and each core have 2 threads) , then you need to run 4 above command to make your cpu 100% busy

You can use below command to kill these commands

killall yes

Equivalent Solairs mkfile command in Linux

If you have Solaris administrator experience , you may know we can use mkfile to create a big empty file for some purpose.In linux there are some equivalent commands.

1 .truncate

For example ,we can use below command to make a 10G empty file

truncate -s 10G 10gfile

2. dd

Below command does the same thing as above truncate command

dd if=/dev/zero of=10gfile bs=1G seek=10 count=0

3. fallocate

Note: this command only been supported by some file systems like ext4 , xfs

fallocate -l 10G 10gfile

Disable automatic screen lock in Ubuntu 18.04

By default Ubuntu will lock your desktop automatically after screen blank off, below steps will show you how to disable it.

Method 1 : Via GUI

1.On your top left of your screen , click the Activities button , then search “privacy” and open it

2.On the right part ,click the “screen lock” , then you can disable the automatic screen lock

Methond 2 : Via commands

1.Open a terminal, run below command to check the status of automatic screen lock , true means enabled ,false means disabled

gsettings get org.gnome.desktop.screensaver lock-enabled

2.Then you can use below command to disable it

gsettings set org.gnome.desktop.screensaver lock-enabled false

3.In case of you want to enable it again , you can run below command

gsettings set org.gnome.desktop.screensaver lock-enabled true

Reset forgotten password in Ubuntu

  1. Power on your computer
  2. Press and hold your left SHIFT key , this will bring you into the grub menu
  3. After going into grub , press “e” to edit the boot command

4.Scroll down and find the line starts with “linux /boot/vmlinuz” and add “init=/bin/bash” to the end of this line , you can refer screenshot below

5.Press F10 to boot
6.Now you enter the command line, here can you run below command to remount the “/”

mount -rw -o remount /

and use below command to reset your password.(if you also forget your username ,try command “ls /home”)

passwd USER

7.If you are prompted with message like “password updated successfully” , you can reboot your computer by pressing Ctl+Alt+Del

Remove or Hide Trash Icon in Ubuntu

By default Ubuntu has Trash icon showed on desktop , if we want to remove this icon we need install third party tool like Gnome-Tweak-Tool

sudo apt install gnome-tweak-tool

Then we can run below command in terminal to remove Trash icon

gsettings set org.gnome.nautilus.desktop trash-icon-visible false

Now if you wan to check your Trash , you still can access it via file browser.

If you want to get the Trash icon back to your desktop , you can run below command in your terminal

gsettings set org.gnome.nautilus.desktop trash-icon-visible true

Move Show application to top of Dock/Launch bar in Ubuntu

In Ubuntu 18.04 , the Show Application icon is at the bottom of the Dock.If you have Windows experience you may feel better if it locates on the top but not bottom to make Ubuntu much like Windows

To move the Show Application icon to top , the simplest way is to use gsettings command . Maybe you hate command line ,but that what Ubuntu is , you need to install another tool or application without using command line.

Right-click your Ubuntu desktop then click Open Terminal (Or by pressing Ctl+Alt+t) , then input below command and press Enter , it’s done ,so simple.

gsettings set org.gnome.shell.extensions.dash-to-dock show-apps-at-top true

If you want to revert it back ,just issue below command

gsettings set org.gnome.shell.extensions.dash-to-dock show-apps-at-top false

Change Username in Ubuntu

1.(Optional) If it’s the only user account whose name you want to change , you need to enable root account in case something wrong and cause both new and old account can not login. To enable root account ,just open an terminal by pressing Ctl+Alt+t,then
issue below to commands to set a password for root

sudo -s
passwd

2.Logout the user account whose name you need to change

3.Press Ctl+Alt+F3(or F4 F5 F6) to switch to virtual console and login with root (Of course if you have another user account with admin access ,you can login gui with that account )

4.use below commands to change the username

usermod -c displayname -d /home/newname -l newname oldname

Eg: Here we assume account user01 ,whose home directory locates /home/ubuntu01 ,and the display name also is ubuntu01,below command with change all these field to ubuntu02

usermod -c ubuntu02 -d /home/ubuntu02 -l ubuntu02 ubuntu01
mv /home/ubuntu01 /home/ubuntu02

5.(Optional)Assume user01’s default unix group is also user01, we can use below command to rename group user01 to user02

groupmod -n user02 user01

6.Use command “exit” to exit current konsole and press Ctl+Alt+F2 to switch back to the GUI konsole ,now username ubuntu01 has been changed to ubuntu02

7.(Optional) You can use below command to disable root password

sudo -s
passwd -dl root