Get information about your Linux system

Execute ‘uname’ without any options to display the kernel name:

$ uname -s
Linux
 

Execute ‘uname’ with the -r option to display the kernel release...

$ uname -r
2.6.32-042stab078.27
 

Execute ‘uname’ with the -v option to display the kernel version:

$ uname -v
#1 SMP Mon Jul 1 20:48:07 MSK 2013
 

Execute ‘uname’ with the -m option to display the machine hardware:

$ uname -m
i686
 

Execute ‘uname’ with the -o option to display the operating system:

$ uname -o
GNU/Linux
 

 

Run in background

Sometimes when yoy enter a command(i.e. find file, copy file), start a script or a program the prompt gets blocked until you close the program or the command/script finishes it's job.
You can force the command,script or program to run in the background by adding '&' at the end

$ mc &

 

Next enter just 'fg' to bring it back to foreground

You can list of all background tasks iby typing 'jobs'

 

Redirect output to file

You can use '>' or '>>' in a command to redirect it's output to a file

$ ls > dir.txt
 

this will create a new file dir.txt with list of files which command returns instead of showing the list on screen

and this will show the content of the file:

$ cat dir.txt
 

if you use again but with '>>'

ls >> dir.txt
 

the output of the command appends to existing content

If you want to clear the contents of the file, simply type

$ > dir.txt
 

Big text files

If you have problem using 'cat' with big files you should view the file in page-per-screen mode

$ cat file.txt | less
 

To exit from this mode, just press Q.


or you may use other command:

 
$ more file.txt
 

If you want only display first 7 lines of the file:

$ head -7 file.txt
 

If you want only display first 12 bytes of the file:

$ head -c12 file.txt
 

The command for display the last contents is 'tail':

$ tail -7 file.txt
$ tail -c12 file.txt
 

Restart/shutdown system

$ reboot
$ shutdown
 

Only root can enter those commands so if you're not 'root' type

$ sudo reboot
$ sudo shutdown
 

 

Finding files

Find files that contain pattern in name

# find /etc -name "*mail*"
 

Finf files that are bigger than 200MB

# find / -type f -size 200M
 

Find files in current dir that were modified last x number days 

# find . -mtime 60
 

Filenames with .dots

You have noticed that there are files in Linux which names are starting with '.' a dot - those are system-wide or user-specific configuration information files and are hidden.

To display them in a listning you have to use 'ls -a' instead of 'ls'

Instead of typing 'ls' or 'ls -l' you may have use built-in aliases 'dir' and 'vdir' respectively

 

Other

How much memory apache is using

ps -ylChttpd --sort=rss
 

Who is connected to FTP?

netstat -tn | grep ':21'
 

 

YUM

Update the headers for your distros

yum -y check-update
 

Update the headers but also download and install any updates available

yum -y update
 

You can also specify a specific package to update by typing:

yum -y update PACKAGE_NAME
 

To install a new package type

yum install PACKAGE_NAME
 

To uninstall package type:

yum erase PACKAGE_NAME

More options type: man yum

 

Other commands:

$ who
List the users logged in on the machine. --

$ rwho -a
List all users logged in on your network. The rwho service must be enabled for this command to work.

$ finger user_name
System info about a user. Try: finger root last. This lists the users last logged-in on your system.

$ history | more
Show the last (1000 or so) commands executed from the command line on the current account. The | more causes the display to stop after each screen fill.

$ pwd
Print working directory, i.e. display the name of your current directory on the screen.

$ hostname
Print the name of the local host (the machine on which you are working).

$ whoami
Print your login name.

$ id username
Print user id (uid) and his/her group id (gid), effective id (if different than the real id) and the supplementary groups.

$ date
Print or change the operating system date and time. E.g., change the date and time to 2000-12-31 23:57 using this command

$ date 123123572000
To set the hardware clock from the system clock, use the command (as root)
setclock

$ time
Determine the amount of time that it takes for a process to complete+ other info. Don’t confuse it with date command. For e.g. we can find out how long it takes to display a directory content using time ls

$ uptime
Amount of time since the last reboot

$ ps
List the processes that are have been run by the current user.

$ ps aux | more
List all the processes currently running, even those without the controlling terminal, together with the name of the user that owns each process.

$ top
Keep listing the currently running processes, sorted by cpu usage (top users first).

$ free
Memory info (in kilobytes).

$ df -h
Print disk info about all the file systems in a human-readable form.

$ du / -bh | more
Print detailed disk usage for each subdirectory starting at root (in a human readable form).

$ lsmod
(as root. Use /sbin/lsmod to execute this command when you are a non-root user.) Show the kernel modules currently loaded.

$ set|more
Show the current user environment.

$ echo $PATH
Show the content of the environment variable PATH. This command can be used to show other environment variables as well. Use set to see the full environment.

$ dmesg | less
Print kernel messages (the current content of the so-called kernel ring buffer). Press q to quit less. Use less /var/log/dmesg to see what dmesg dumped into the file right after bootup. - only works on dedciated systems

Commands for Process control

$ ps
Display the list of currently running processes with their process IDs (PID) numbers. Use ps aux to see all processes currently running on your system (also those of other users or without a controlling terminal),
each with the name of the owner. Use top to keep listing the processes currently running.

$ fg
PID Bring a background or stopped process to the foreground.

$ bg
PID Send the process to the background. This is the opposite of fg. The same can be accomplished with Ctrl z

$ kill PID
Force a process shutdown. First determine the PID of the process to kill using ps.

 

$ killall -9 program_name
Kill program(s) by name.

$ xkill
(in an xwindow terminal) Kill a GUI-based program with mouse. (Point with your mouse cursor at the window of the process you want to kill and click.)

$ lpc
(as root) Check and control the printer(s). Type ??? to see the list of available commands.

$ lpq
Show the content of the printer queue.

$ lprm job_number
Remove a printing job job_number from the queue.

$ nice program_name
Run program_name adjusting its priority. Since the priority is not specified in this example, it will be adjusted by 10 (the process will run slower), from the default value (usually 0). The lower the number (of niceness to other users on the system), the higher the priority. The priority value may be in the range -20 to 19. Only root may specify negative values. Use top to display the priorities of the running processes.

$ renice -1 PID
(as root) Change the priority of a running process to -1. Normal users can only adjust processes they own, and only up from the current value (make them run slower).