Display Linux system information
#uname -a
Display kernel release information
#uname -r
Show which version of redhat installed
# cat /etc/redhat-release
Show how long the system has been running + load
# uptime
Show system host name
# hostname
Display the IP addresses of the host
# hostname -I
Show system reboot history
# last reboot
Show the current date and time
# date
Show this month's calendar
# cal
You can see your previously run command by executing 'history' command
# history
Display who is online
# w
Who you are logged in as
# whoami
The pwd command displays the full path name of the current location, which helps determine appropriate syntax for reaching files using relative path names.
# pwd
The ls command lists directory contents for the specified directory, or if no directory is given for the current directory. as in if I enter "ls /etc" it will display the files and directories under /etc directory
# ls /etc
Use the cd command to change directories. As example if you run "cd /etc" it will change the directory to /etc
# cd /etc
Use the cd command to change working directory to logged in user home directory. "cd" and "cd ~" both command will change the working directory to the logged in users home dorectoy.
# cd
# cd ~
use the cd command with .. to change the working directory to parent directory. It works like the the up button of windows explorer. By default every users home directory situated in /home directory so if we execute the command "cd .." from any users directory it will take you the the directory /home.
# cd ..
use the cd command with - to change the working or current directory to previous working directory. This means if I change the directory to /etc from /home and then I executed "cd -" it will take back you to /home
# cd -
The ls command has multiple options for displaying attribute on files. The most common and useful are –l (long listing format), -a (all files includes hidden files) and –R (recursive)
# ls -l
# ls -la
# ls -R
use mkdir command to create directory
# mkdir dir
# ls
The mkdir command creates one or more directories or subdirectories, generating errors if the filename already exists or when attempting to create a directory in a parent directory that doesn’t exist. The –p parent option creates missing parent directories for the requested destination.
# mkdir dir2/subdir2
# mkdir dir2/subdir2 -p
#ls
use touch command to create directory
# touch file1
# ls
to create multiple command with command
# touch file1 file2 file3
# ls
# touch filename{1..5}
# ls
# touch file.txt /tmp/file.txt
# ls
# ls /tmp
The cp command copies one or more files to become new, independent files. Syntax allows copying an existing file to a new file in the current or another directory, or copying multiple files into another directory. In any destination, new file names must be unique. If the new file name is not unique, the copy command will overwrite the existing file. When copying multiple files with one command, the last argument must be a directory.
# ls
# cp file1 myfile.txt
# ls
# ls /tmp
# cp file1 /tmp/
# ls /tmp
The mv command renames files in the same directory, or relocates files to a new directory. File contents remain unchanged. Files moved to a different file system require creating a new file by copying the source file, then deleting the source file.
# ls
# mv file2 file2.txt
# ls
# ls
# mv file3 /tmp/
# ls
# ls /tmp
Default syntax for rm deletes files, but not directories. Deleting a directory, and potentially many subdirectories and files bellow it, require the –r recursive option. Using -i will interactively prompt for each deletion. This is essentially the opposite of –f which will force the deletion without prompting the user. There is no command-line undelete feature, nor a trash bin from which to restore.
# ls
# rm filename1
# ls
# ls
# rm -rf dir2
# ls
The rmdir directoris only if empty. Removed directories cannot be undelete.
# ls
# rmdir dir
# ls
To read a file one of the most popular command is "cat". cat followed by by the file name or path will display the file content in your shell
# cat myfile
To read a file in a reverse direction we can use "tac" command
# tac myfile
"head" is a command to read first 10 lines of a file you can specify the line number by using -n command followed by the desired line number
# head /etc/passwd
# head -n 3 /etc/passwd
"tail" is a command to read last 10 lines of a file you can specify the line number by using -n command followed by the desired line number. and we can use -f option to follow the file continiously, which is very useful to read logs
# tail /etc/passwd
# tail -n 3 /etc/passwd
# tail -f
"head" is a command which is used for reading big file can't read with single screen and have to scroll down. We can scroll the file downwards by pressing "Enter" or "Space" button. And it will take you out from the file when you scroll it down all the way to the end of the file.
# more /var/log/messages
"less" is a command which is used for reading big file can't read with single screen and have to scroll up and down. We can scroll the file upwards or downwards by pressing "Up Arrow" or "Down Arrow" button. And you have to press the button "q" to get out from the file.
# less /var/log/messages
We can add user in our system by using command "useradd" or "adduser" and we check users information with "id" command
# useradd user1
# id user1
# adduser user2
# id user2
We can add user group in our system by using command "groupadd" and we check group information by reading the file /etc/group
# groupadd mygroup
# tail -n 1 mygroup
We can change the users' default parameter with certain option added with the command "useradd" when we creating a new user. As like with -u option we can change the user id, -g the primary group, -G secondery group, -s shell, -d home directory of the user and with -c we can add comment for the user. We can use this option separately or at the same command. And we can see the users info by using "getent passwd" commad.
# useradd -g mygroup -G user1 -u 2000 -s /bin/sh -d /var/customuser -c "this is a user with custom parameters" customuser
# getent passwd customuser
To set a users password we can use the command "passwd".
# passwd user1
Every user can change their own password by using passwd. Note: only root can change other users password on behalf of them.
# passwd
With passwd command we can lock and unlock a user by usnig -l and -u option with it
# passwd -l user1
# passwd -u user1
We can expire a user passwd by using -e option with passwd command
# passwd -e user1
We can check the passwd aging with the command "chage" with a inclusion of option -l followed by the username
# chage -l user1
We can change the password aging with inclusion of several option as the minimum days between the password change with option -m, minimum days between the password change -M, warning given by before the password expire -W, make the password inactive -I and Expire the account with -E.
# chage -m 3 -M 30 -W 5 user1
# chage -l user1
To delete a user we can use the command "userdel". Note: we have to use -r option with userdel otherwise the file created for the user will remains on the system.
# userdel user2
# id user2
We can use "groupdel" to delete a group
# groupdel group1
To change a existing users parameter we can use "usermod" with options like with -u option we can change the user id, -g the primary group, -G secondery group, -s shell, -d home directory of the user and with -c we can add comment for the user
# usermod -g user1 -G mygroup -s /bin/bash -u 1005 -c "with changed parameter" customuser
# getent passwd customuser
Linux permision examples
PERMISSION EXAMPLE
U G W
rwx rwx rwx chmod 777 filename
rwx rwx r-x chmod 775 filename
rwx r-x r-x chmod 755 filename
rw- rw- r-- chmod 664 filename
rw- r-- r-- chmod 644 filename
U = User
G = Group
W = World
r = Read
w = write
x = execute
- = no access
r = 4
w = 2
x = 1
We can check a file's or directory's permission or ownership or group ownership by using "ls -l" command. And change the ownership to another user by using "chown" command
# ls -l file
# chown shibli file
# ls -l file
We can change the group ownership of a file or directory by using "chgrp" command
# ls -l file
# chgrp mygroup file
# ls -l file
We can change ownership and group ownership with a same command of "chown" in that case we have separate the user and group name by ":"
# ls -l file
# chown root:root file
# ls -l file
With "chmod" command we can change the permissions of a file as described in the note earlier. by using Numric value :
# ls -l file
# chmod 777 file
# ls -l file
# chmod 666 file
# ls -l file
# chmod 644 file
# ls -l file
# chmod 000 file
# ls -l file
Or we can change the value specificly
# ls -l file
# chmod u+r file
# ls -l file
# chmod u+w file
# ls -l file
# chmod g+r file
# ls -l file
# chmod o+r file
# ls -l file
# chmod u+x file
# ls -l file
# chmod ug=rw,o=r file
# ls -l file
"Set user ID" or "SUID" is one of the three special permissions, by using SUID bit we can run a file with its owners permission.
# ls -l file
# chmod u+s file
# ls -l file
"Set group ID" or "SGID" is one of the three special permissions, by using SGID on a directory we can pass on the group ownership to is child directorys'.
# ls -ld dir
# chmod g+s dir
# ls -ld dir
Sticky bit normally use for a common directory where everyone has all the permissions but because of the sticky bit one can't change or delete another users file.
# ls -l file
# chmod o+t file
# ls -l file