Basic linux commands with syntax and examples
Basic linux commands with syntax and examples tutorial includes Linux administration commands and tasks which you have to do regularly. Yes, there is GUI available, but with commands things becomes more easier. People who are working as a Linux administrator will believe my words. They very well know the fact that GUI is simply not allowed in a Linux server and it is for security reasons. If you have just started with Linux administration then you need to master some important commands of Linux. This tutorial is about making you familiar with some Linux basic commands and task for newbie Linux Administrator. For advance Linux system administrator command and task, we will be covering it in the next article. Lets see some of the basic commands for Linux administrator.
1 Downloading packages
There are some applications or packages may not be available in your system. In that case you need to download those packages in your Linux server. wget command is used for downloading any packages. To download the below package, we need to give the path of the package with wget and Enter, the required package name and path to download it to the current directory.
# wget http://mirror.lihnidos.org/CentOS/6/os/i386/Packages/NetworkManager-0.8.1-43.el6.i686.rpm
The path of the package you want to download, you will get it on internet. You have to look for the required package on the web.
2 Installing and updating packages
Even if the application is available on your Linux server, you cannot use it until it is installed. YUM and apt tool is a very common basic Linux commands available for updating and installing packages in Linux. Below are the examples given to use YUM and apt in Red hat and Debian respectively.
Red hat based
To update packages on Red hat based system
# yum update
# yum install <package name>
To update packages on Debian based system
# apt-get update
Command to download any package on Debian based system
# apt-get install <package name>
3 Copying files and Directories
Use cp command to copy any files or directories. For example:
# cp file1 /Directory1/
The above command will copy file1 to the destination Directory1
For more details check Copy command in Linux with examples
4 Removing files and Directories
To remove files in Linux use rm command and to remove directory use rmdir.
# rm file1
You should be very careful when using 'rm' command, it will instantly remove the target file from system. The bad thing about rm command is that the deleted file doesn't move to recycle bin. So, my suggestion would be using the 'i' option with rm command, It will ask for confirmation before removing any file.
# rm -i file1
To remove directory use rmdir
# rmdir directry1
5 Renaming files and Directories
Use mv command to rename a file.
# mv file1 file2
The above command will rename file1 to file2. Similarly you can rename any directory with the mv command.
6 Adding repository
What are repository? In a simple language Repositories are central location in which data is stored and managed. When you install any application on your Linux server , it connects with these repository and look for the defined application in the repository. If the application is not available in the repository, you installation will failed. Therefore, it is very important to add right repository on your Linux server. So that you won't be looking here and there to install the particular package. :-) Adding repository command is different for both Debian based and Red hat based OS. Red hat based system
# yum-config-manager --add-repo http://www.somesite.com/some.repo
Debian based system
# add-apt-repository <repository path >
7 Seeing the permission of files
With this command you will be able to see the list of files and directories. You can see the read, write, execute permission of the file, user and group owner of file etc.
# ls -l
8 Changing permissions of files
You sometime require to change the permission of the file. For example you may want your file to have a write permission for a specific user. Have a look at the most basic Linux commands to change permissions of file and directory.
u = user g = group o = others
r = read w = write x = Execute
The above options can be used with chmod command to change the permission of the file for user, group and others.
Read, write and execute permission for user, group and others # chmod ugo+rwx file1 Assigning read and write permission to user # chmod u+rw file1 Assigning write and execute permission to group # chmod g+wx file1 Assigning only read permission to others # chmod o+r file1
9 Changing the ownership of file
Well, with chown you will be able to assigned new user or group owner to a file,.
You can add just a new user to a file.
# chown <user-owner> file1
You can add just a group owner to a file
# chown :<group-owner> file1
You can add user and group-owner to a file at the same time
# chown <user-owner>:<group-owner> file1 Note the colon : between user-owner and group-owner
10 Disk space check
As a system administrator you are required to check disk space every now and then, use 'df' to check disk space of your server. Regular disk space check will help you avoid crashing of your hard disk.
# df -h use -h option with df to see disk space in GB format
Just using df command without -h option will show you space in KB format, which is very tough to analyze.
11 Starting and stopping services
When any services is newly configured or installed in your Linux server, you need to manually start the service to allow the service to be in action.
In Red hat based system:-
To know the status of the service # service <service name> status To start the service # service <service name> start To stop the service # service <service name> stop
In Debian based system:-
To know the status of the service # /etc/init.d/<service name> status To start the service # /etc/init.d/<service name> start To stop the service # /etc/init.d/<service name> stop
12 Adding user and Group
To add new user use the command 'useradd'.
# useradd <user name>
For example create an user 'john'
# useradd john
Check your 'passwd' file to see your created users.
# cat /etc/passwd
To add new group use the command 'groupadd'.
# groupadd <group name>
For example create a group 'accountant'
# groupadd accountant
Check 'group' file to see the created groups.
# cat /etc/group
13 Assigning users and groups password expiry date
If you are assigning users with a password expiration date, then you are making a very wise choice. Weather it's a server root password or a normal user password, password must be changed with a regular interval.
# chage -l <user name>
Use the above command to know the password expiration date and other information about the particular user. To see the password expiration date of user 'john' do the following.
# chage -l john
You can see in the above screen shot that the password of 'john' expires never. Hence, lets set the password expiration date of 'john'.
# chage -M 30 john
This command will set the password expiration date of the user to 30 days.
14 Changing user and group password
To change the password of any user use or group, use the command 'passwd'
# passwd <user name>
# passwd <group name>
15 Monitoring last login
The data of any successful as well as unsuccessful login into your system is generated in a log file of your system. It is very important to look this log files everyday to monitor your system. Any login from a suspicious IP address into your system can be a real havoc. After studying these logs you can block these IP addresses so that it won't be able to enter into your system the next time. Use 'last ' command to see the last logins in your server.
16 Monitoring load on the server
You can monitor the average load on the server with command w
The 'w' command also shows you the number of users currently logged in onto your Linux server.
17 Changing directory
To move from current directory to another directory use cd command.
# cd / To move to the root directory
Similarly you can move to any directory with cd command, you have to provide the correct path of the directory.
# cd .. To move one directory back ward
18 Know what process are running
To know the number of processes are running use ' ps' command
There are various options available that can be used with ps.
19 Know the current directory
To know the current directory use pwd
pwd stands for Present Working Directory
Cat commands are used to view files, to append something more in your file, and to just write something in your file.
To see what is written inside the file.
# cat file1 # cat > file1
The above command that is 'cat > file1' will open a blank space for you to write something in it. Write what you need then press CTRL+D to save the file.
To add something some more text to your file that is appending in your file.
# cat >> file1
21. Nmap command
Nmap command is used to scan all ports and identify open and closed ports in a computer. It is helpful to detect all active machines in a network. To scan an IP in a given network, see the Nmap scan all ports command given below.
# nmap 192.168.0.21
That's it with the article Basic linux commands with syntax and examples tutorial.
Also see tutorials on Sed command examples.