Saturday, October 29, 2016

Create a Directory using C on Linux

To create a directory on Linux using C, take a look at the following code.

#include <stdio.h>
#include <sys/stat.h>

int main(void) {
    char *foldername = "test_dir";

    int status = mkdir(foldername, S_IRUSR | S_IWUSR | S_IXUSR);

    if(status == 0) {
        printf("Folder successfully created.\n");
    } else {
        printf("Error: Folder creation failed.\n");
    }

    return 0;
}

How it works:

First I declared a variable foldername. This variable stores the name of the folder that I want to create.

Then I used the mkdir(foldername, permission) library functions from sys/stat.h. This function will return 0 if a folder is created or -1 on failure. I stored the return value on a variable status.

Then I checked the status variable to see if a folder is created(status == 0) or not(status == -1).


Here is used, S_IRUSR | S_IWUSR | S_IXUSR as permission. It means, the newly created directory will have read, write, and execute permission of the owner of the directory.

You can find a whole list of permission constants here. I am writing some common easy to remember ones here.


S_IRUSR - Read permission of the owner of the directory.
S_IWUSR - Write permission of the owner of the directory.
S_IXUSR - Execute permission of the owner of the directory.
S_IRGRP - Read permission of the group.
S_IWGRP - Write permission of the group.
S_IXGRP - Execute permission of the group.
S_IROTH - Read permission to others.
S_IWOTH - Write permission to others.
S_IXOTH - Execute permission to others.

Thursday, October 20, 2016

Change the Password of Any Linux User from the Terminal on Linux

If you're a system administrator and you want to change the password of a user of your Linux system, follow this tutorial.

Let's say I have a user 'linda' on my system. I want to change the password of linda. I would run the following command,

sudo passwd linda
Changing password for shovon.
(current) UNIX password:

Enter your current accounts password and press <Enter>. Then you will see the following prompt:

Enter new UNIX password: 

Enter the password that you want 'linda' to have and press <Enter>, Then you will see the following prompt:

Retype new UNIX password: 

Retype the new password for 'linda' and press <Enter>. You will see the following line.

passwd: password updated successfully 


Congrats! You have successfully changed the password of 'linda' from Linux terminal.

Change Your Password on Linux

If you want to change your password from the terminal on Linux, follow this tutorial.

To change your password on linux, run the following command

passwd
Changing password for shovon.
(current) UNIX password:

Enter your current accounts password and press <Enter>. Then you will see the following prompt:

Enter new UNIX password: 

Enter the password that you want your account to have and press <Enter>, Then you will see the following prompt:

Retype new UNIX password: 

Retype your new password and press <Enter>. You will see the following line.

passwd: password updated successfully 


Congrats! You have successfully changed your password from Linux terminal.

Enable root account on Linux

To enable root account, you must have administrative privileges. Usually what that means is, you must be a sudoer. sudoers can run sudo command.

To enable root account run the following command,

sudo passwd
[sudo] password for shovon: 

Enter your current accounts password and press <Enter>. Then you will see the following prompt:

Enter new UNIX password: 

Enter the password that you want root account to have and press <Enter>, Then you will see the following prompt:

Retype new UNIX password: 

Retype the root password and press <Enter>. You will see the following line.

passwd: password updated successfully 


Congrats! You have successfully enabled root account.

Change root Password of Linux

To change the root password run the following command,

sudo passwd
[sudo] password for shovon: 

Enter your current account's password and press <Enter>. Then you will see the following prompt:

Enter new UNIX password: 

Enter the password that you want root account to have and press <Enter>, Then you will see the following prompt:

Retype new UNIX password: 

Retype the root password and press <Enter>. You will see the following line.

passwd: password updated successfully 


Congrats! You have successfully changed the root password.

Monday, October 17, 2016

See Which Processes are Consuming Most of Your Physical Memory/RAM on Linux

To view the processes that are using your RAM use the following command,

sudo ps aux | awk '{print $2 " " $6}' | sort -k2,2 -nr | grep -v 0 | grep -v RSS
1963 82432
2928 39884
3634 32112
3253 28792
2868 28764
3247 27952
3255 27524
3257 18116
2994 15688
2867 14196
1769 13988
1883 12744
2827 9656
1762 8228
2775 8116
2784 7232
2797 6436
3225 6424
1 5676
3354 5484
617 5184
3217 5112
3287 4884
1645 4836
2525 4296
3976 3984
3977 3384
1731 3332
571 3272
1766 3196
1738 2988
2869 2984
1997 2484
2251 1864
2496 1832
1761 1276
3979 936
1771 856
3978 768
2547 316

The first column is the PID(process ID) and the second column is the amount of RAM(in kilobytes) used by that process.



If you want to view the top 5 process from that big list, you can use the following command,

sudo ps aux | awk '{print $2 " " $6}' | sort -k2,2 -nr | grep -v 0 | grep -v RSS | head -n 5
1963 82432
2928 39884
3634 32112
3253 28792
2868 28764

Here 1963 is the PID(process ID) and it is using 82432KB of the physical memory(RAM).



If you want to know more about the process, you can use the PID like this,

sudo ps p 1963
 PID TTY      STAT   TIME COMMAND
1963 tty7     Ss+    0:37 /usr/lib/xorg/Xorg -core :0 -seat seat0 -auth /var/run/lightdm/root/:0

Here 1963 is the PID of the process.

See Which Process is Using Most of Your CPU on Linux

To view the processes that are using your CPU use the following command,

sudo ps aux | awk '{print $2 " " $3}' | sort -k 2,2 -nr | grep -v 0.0 | grep -v %CPU
3656 1.5
1963 1.4
3634 0.4
2548 0.3
3251 0.1
3247 0.1
1 0.1

The first column is the PID(process ID) and the second column is the percentage of CPU used by that process.

If you want to view only the process that is using most of the CPU, you can use the following command,

sudo ps aux | awk '{print $2 " " $3}' | sort -k 2,2 -nr | grep -v 0.0 | grep -v %CPU | head -n 1
3656 1.5

Here 3656 is the PID(process ID) and it is using 1.5% of the CPU. On a real server, percentage of CPU usage might be way higher than that.

Saturday, October 15, 2016

Unlock User Account (Enable User Login) on Linux

To unlock a user account that has been locked on linux, you can use the passwd command.

The command is,

sudo passwd -u username

For example, to unlock the user 'linda', so 'linda' can login to the system again,

sudo passwd -u linda
Unlocking password for user linda.
passwd: Success

Now you can try to login as 'linda' and you will see that login is working again.

Disable User Login (Lock Password) on Linux

To lock a user account on linux, you can use the passwd command.

The command is,

sudo passwd -l username

For example, to lock the user 'linda', so 'linda' can not login to the system anymore,

sudo passwd -l linda
Locking password for user linda.
passwd: Success

Now you can try to login as 'linda' and you will see that you login is disabled.

Display Information About User Password on Linux

To display password information ie.

  • password expire date
  • if the password is active or not
  • the number of days after the account expires
  • minimum number of days between password change
  • maximum number of days between password change
  • how many days to give warning before the password expires

The chage can be used like this,
chage -l username

For example, chage -l shovon
Last password change     : Oct 09, 2016
Password expires     : never
Password inactive     : never
Account expires      : never
Minimum number of days between password change  : 0
Maximum number of days between password change  : 99999
Number of days of warning before password expires : 7



Thursday, October 13, 2016

See The Users that have Running Processes on Linux System

Hi,

Today I am gonna write about how to see the users on your Linux system that are running some processes.

I have a YouTube video about this post. Check it out if you wish.

YouTube : See The Users that have Running Processes on Linux System



The command is,

sudo ps aux | awk '{print $1}' | sort | uniq | grep -v USER

Explanation

sudo ps aux is used to list the running processes of the system.

awk '{print $1}' is used to print only the first column from the output of sudo ps aux

sort is used to sort the output.

uniq is used to get only the unique lines.

grep -v USER is used to remove the line that contains the field name USER.

Thank you for the visit. :-)

Sunday, October 9, 2016

Generate Simple Relatively Easy to Remember Passwords using pwmake on Linux

To generate simple easy to remember passwords on Linux, you can use pwmake command. If you like to watch videos to learn then I got a YouTube video about this blog at Youtube: Generate Simple Relatively Easy to Remember Passwords using pwmake on Linux.

pwmake command accepts one parameter, that is an entropy. The entropy determines how hard the password is to crack. Simply more entropy means more security.

The minimum entropy value you should use is 56, but you can change it to anything you need.

You can install pwmake on Ubuntu using the following command.

sudo apt-get update
sudo apt-get install libpwquality-tools cracklib-runtime

Let's generate a simple password. Run the following command.

pwmake 56
Ytuq1yx-ilUl

The command returns Ytuq1yx-ilUl, a 12 character password. This is the output I got, the output you will get will definitely be different.

Now, let's generate a more secure password

pwmake 128
@tm0SOnrAf3vUpRuN4x&4ssAtry

That's a long password.

Anyway, I hope you got the point.

Thanks for vising my blog and happy Linuxing :-D