How to change ownership of a file Linux (Ubuntu | Debian | Centos) CLI

This is a quick reference guide on how to change ownership of a file on Linux Based Operating Systems.

1. Change ownership of a file using chown command

[root@vps1 ~]# chown root file1
[root@vps1 ~]# ls -l file1
-rw-r--r-- 1 root root 0 Jun  4 11:06 file1
[root@vps1 ~]# chown vpsuser file1
[root@vps1 ~]# ls -l file1
-rw-r--r-- 1 vpsuser root 0 Jun  4 11:06 file1

2. Check the file permission using ls -l

root@vps:~# ls -l file1
-rwxr-x--x 1 root root 0 Jun  3 08:17 file1

A dash (-) indicates that the file is a regular file.

The letter (d) indicates that the file is a directory as per below:

A letter (l) preceding indicates a special file type called a symlink. A symlink is a pointer to another location in the file system.

drwxr-xr-x 3 root root       4096 Oct 18  2018 ufw

The first three letters above refer to the permission of the user so in this case rwx, so read, write, and execute.

The next three letters is for group r-x, so read and execute, but not write as indicated by the dash.

The next three letters is for other r-x, so read and execute, but not write as indicated by the dash. (so the same as group in this example)

The number 3 in this example after the permissions indicates the number of files or sub-directories contained within this particular directory.

The first name after the number (directory and file number) in the example below is the user or file owner. “vpsuser”

The second name refers to the files group. “group1”

-rwxr-x--x 1 vpsuser group1 0 Jun  3 08:17 file1
root@vps:~#

The remainder of the file details are the size of the file, the date and time that the file was created or last modified, and the file name.

Thank you for reading and please feel free to leave any feedback.

How to check file permissions Linux (Ubuntu | Debian | Centos) CLI

This is a quick reference guide on how to check file permissions on Linux Based Operating Systems.

1. Check the file permission using ls -l

root@vps:~# ls -l file1
-rwxr-x--x 1 root root 0 Jun  3 08:17 file1

A dash (-) indicates that the file is a regular file.

The letter (d) indicates that the file is a directory as per below:

A letter (l) preceding indicates a special file type called a symlink. A symlink is a pointer to another location in the file system.

drwxr-xr-x 3 root root       4096 Oct 18  2018 ufw

The first three letters above refer to the permission of the user so in this case rwx, so read, write, and execute.

The next three letters is for group r-x, so read and execute, but not write as indicated by the dash.

The next three letters is for other r-x, so read and execute, but not write as indicated by the dash. (so the same as group in this example)

The number 3 in this example after the permissions indicates the number of files or sub-directories contained within this particular directory.

The first name after the number (directory and file number) in the example below is the user or file owner. “vpsuser”

The second name refers to the files group. “group1”

-rwxr-x--x 1 vpsuser group1 0 Jun  3 08:17 file1
root@vps:~#

The remainder of the file details are the size of the file, the date and time that the file was created or last modified, and the file name.

Thank you for reading and please feel free to leave any feedback.

How to use chmod command Linux (Ubuntu | Debian | Centos) CLI

This is a quick reference guide on to use the chmod command on Linux Based Operating Systems.

  1. chmod General
chmod options permissions file name
chmod u=rwx,g=rx,o=r file1

u = user

g = group

o = other

r = read

w = write

x = execute

So in the example above – the user has read, write, and execute permissions, group has read and execute and other have read only.

2. chmod Octal

chmod 751 file1

4 = read

2 = write

1 = execute

0 = no permission

So in the example above – the user has read, write, and execute permissions, group has read and execute and other have execute only. (user) 4+2+1= 7, (group), 4+1=5, (other) 1 = 1

3. Check file permission using ls -l

root@vps:~# chmod 751 file1
root@vps:~# ls -l file1
-rwxr-x--x 1 root root 0 Jun  3 08:17 file1

A dash (-) indicates that the file is a regular file.

The letter (d) indicates that the file is a directory as per below:

A letter (l) preceding indicates a special file type called a symlink. A symlink is a pointer to another location in the file system.

drwxr-xr-x 3 root root       4096 Oct 18  2018 ufw

The first three letters above refer to the permission of the user so in this case rwx, so read, write, and execute.

The next three letters is for group r-x, so read and execute, but not write as indicated by the dash.

The next three letters is for other r-x, so read and execute, but not write as indicated by the dash. (so the same as group in this example)

The number 3 in this example after the permissions indicates the number of files or sub-directories contained within this particular directory.

The first name after the number (directory and file number) in the example below is the user or file owner. “vpsuser”

The second name refers to the files group. “group1”

-rwxr-x--x 1 vpsuser group1 0 Jun  3 08:17 file1
root@vps:~#

The remainder of the file details are the size of the file, the date and time that the file was created or last modified, and the file name.

Thank you for reading and please feel free to leave any feedback.

How to change the root password Linux (Ubuntu | Debian | Centos) CLI

This is a quick reference guide on how to change the root password on Linux Based Operating Systems.

To change the root password on Linux Operating Systems (Ubuntu, Debian and Centos)

root@VPS:~# sudo passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Thank you for reading and please feel free to leave any feedback.

How to search for a file or directory on Linux (Ubuntu | Debian | Centos) CLI

This is a quick reference guide on how to find a file or directory on Linux Based Operating Systems.

To search by name – case sensitive.

root@VPS:~# find / -name sshd
/run/sshd
/etc/pam.d/sshd
/usr/sbin/sshd

To search by iname – not case sensitive.

root@VPS:~# find / -name SSHD
root@VPS:~# find / -iname SSHD
/run/sshd
/etc/pam.d/sshd
/usr/sbin/sshd

You will see that the -name command above does not return anything when searching for SSHD but the -iname does.

To search for a file or directory.

root@VPS:~# find / -type d -name sshd
/run/sshd
root@VPS:~# find / -type f -name sshd
/etc/pam.d/sshd
/usr/sbin/sshd

In the above example you are searching for either just files or directories containing sshd. The -type d option is to search by directory and the -type -f option is to search for files.

To search for a file or directory containing certain words or characters using a wildcard.

root@VPS:~# find / -type f -name sshd
/etc/pam.d/sshd
/usr/sbin/sshd
root@VPS:~# find / -type f -name *sshd*
/run/sshd.pid
/etc/pam.d/sshd
/etc/ssh/sshd_config
/etc/ssh/sshd_config.ucf-dist
/usr/share/openssh/sshd_config
/usr/share/openssh/sshd_config.md5sum
/usr/share/man/man5/sshd_config.5.gz
/usr/share/man/man8/sshd.8.gz
/usr/share/vim/vim80/syntax/sshdconfig.vim
/usr/sbin/sshd
/var/lib/ucf/cache/:etc:ssh:sshd_config
/var/lib/systemd/deb-systemd-helper-enabled/sshd.service

As with the previous example when searching with just “sshd” that will return results with the exact name of the file or directory. When adding * on either side of the search term this will return results any file containing the word “sshd”.

Thank you for reading and please feel free to leave any feedback.

How to make a directory on Linux (Ubuntu | Debian | Centos) CLI

This is a quick reference guide on how to create a directory on Linux Based Operating Systems.

To make a directory within an existing directory

root@VPS:~# mkdir vps
root@VPS:~# ls
vps

To create multiple sub-directories using -p option.

rroot@VPS:~# mkdir -p vps/test/test1
root@VPS:~# ls
vps
root@VPS:~# cd vps/test/test1/
root@VPS:~/vps/test/test1#

Thank you for reading and please feel free to leave any feedback.

How to remove a directory on Linux (Ubuntu | Debian | Centos) CLI

This is a quick reference guide on how to remove a directory on Linux Based Operating Systems.

To remove an empty directory.

root@VPS:~/vps/test# ls
test1
root@VPS:~/vps/test# rmdir test1/
root@VPS:~/vps/test# ls
root@VPS:~/vps/test#

To remove multiple empty sub-directories using -p option.

root@VPS:~/vps/test# cd vps/test/test1
root@VPS:~/vps/test/vps/test/test1# cd
root@VPS:~# rmdir -p vps/test/vps/test/test1/

To remove a directory containing files or directories using -R option.

root@VPS:~# mkdir -p vps/test/test1
root@VPS:~# ls
vps
root@VPS:~# rm -R vps
root@VPS:~# ls

To remove multiple empty directories using a wildcard.

root@VPS:~/vps#
root@VPS:~/vps# mkdir vps1 vps2 vps3 abc
root@VPS:~/vps# ls
abc  vps1  vps2  vps3
root@VPS:~/vps# rmdir *vps*
root@VPS:~/vps# ls
abc  

In the above example use the *vps* to remove any directory that contains the word vps.

To remove multiple directories using a wildcard and rm -R option.

root@VPS:~/vps# ls
abc  file1.text  vps1  vps2  vps3
root@VPS:~/vps# rmdir *vps*
rmdir: failed to remove 'vps1': Directory not empty
rmdir: failed to remove 'vps2': Directory not empty
rmdir: failed to remove 'vps3': Directory not empty
root@VPS:~/vps# rm -R *vps*
root@VPS:~/vps# ls
abc  file1.text

Thank you for reading and please feel free to leave any feedback.

How to create a new user Linux (Ubuntu | Debian | Centos) CLI

This is a quick reference guide on how to create a new user on Linux Based Operating Systems.

Create a new user.

root@VPS:~# useradd vpsuser

Set the new user a password.

root@VPS:~# passwd vpsuser
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Change to the new user account.

root@VPS:~# su - vpsuser

Thank you for reading and please feel free to leave any feedback.

How to delete a user Linux (Ubuntu | Debian | Centos) CLI

This is a quick reference guide on how to delete a user on Linux Based Operating Systems.

Delete a user.

root@VPS:~# userdel vpsuser

Delete the users home directory and mail spool.

root@VPS:~# deluser vpsuser --remove-home
Looking for files to backup/remove ...
Removing files ...
Removing user `vpsuser' ...
Warning: group `vpsuser' has no more members.
Done.

Delete all user files.

root@VPS:~# deluser --remove-all-files vpsuser

Thank you for reading and please feel free to leave any feedback.

How to check the OS version Linux (Ubuntu | Debian | Centos) CLI

This is a quick reference guide on how to check the OS version on Linux Based Operating Systems.

cat /etc/os-release

root@VPS:~# cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.1 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.1 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

lsb_release -a

root@VPS:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic

hostnamectl

root@VPS:~# hostnamectl
   Static hostname: VPS
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 66b23fd91cd5631d9eab4ed3d86e2484
           Boot ID: 37438eb1e9e544ed9895163244f3738b
    Virtualization: kvm
  Operating System: Ubuntu 18.04.1 LTS
            Kernel: Linux 4.15.0-43-generic
      Architecture: x86-64

Thank you for reading and please feel free to leave any feedback.