Commands: Sudo: Executing Commands With Elevated Privileges

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

Commands

sudo: Executing Commands with Elevated Privileges


• Most of the following commands will need to be prefaced with
the sudo command if you will be working with directories or files not owned by
your account. Please see RootSudofor information on using sudo.

File & Directory Commands


• pwd: The pwd command will allow you to know in which directory you're
located ( pwd stands for "print working directory"). Example: "pwd" in the
Desktop directory will show "~/Desktop". Note that the Gnome Terminal also
displays this information in the title bar of it's window - see the example
screenshot at the top of this page.
• ls: The ls command will show you the files in your current directory. Used
with certain options, you can see sizes of files, when files where made, and
permissions of files. Example: "ls ~" will show you the files that are in your
home directory.
o cd: The cd command will allow you to change directories. When
you open a terminal you will be in your home directory. To move around the
file system you will use cd. Examples:
 To navigate into the root directory, use "cd /"
 To navigate to your home directory, use "cd" or "cd ~"
 To navigate up one directory level, use "cd .."
 To navigate to the previous directory (or back), use "cd -"
 To navigate through multiple levels of directory at once,
specify the full directory path that you want to go to. For example, use, "cd
/var/www" to go directly to the /www subdirectory of /var/. As another
example, "cd ~/Desktop" will move you to the Desktop subdirectory inside
your home directory.
• cp: The cp command will make a copy of a file for you. Example: "cp file
foo" will make a exact copy of "file" and name it "foo", but the file "file" will still
be there. When you use mv that file would no longer exist, but when you
use cp the original file stays and a new copy is made.
• mv: The mv command will move a file to a different location or will
rename a file. Examples are as follows: "mv file foo" will rename the file "file"
to "foo". "mv foo ~/Desktop" will move the file "foo" to your Desktop
directory but will not rename it. You must specify a new file name to rename a
file.
o To save on typing, you can substitute '~' in place of the home
directory.
o Note that if you are using mv with sudo you will not be able to use
the ~ shortcut, but will have to use the full pathnames to your files. This is
because when you are working as root, ~ will refer to the root account's home
directory, not your own.
• rm: Use this command to remove or delete a file in your directory. It will
not work on directories which have files in them (use rmdir instead).
• mkdir: The mkdir command will allow you to create directories.
Example: "mkdir music" will create a music directory.
• man: The man command is used to show you the manual of other
commands. Try "man man" to get the man page for man itself. See the
" Man & Getting Help" section down the page for more information.

System Information Commands


• df: The df command displays filesystem disk space usage for all
partitions. " df -h" is probably the most useful - it uses megabytes (M) and
gigabytes (G) instead of blocks to report. ( -h means "human-readable")
• free: The free command displays the amount of free and used memory in
the system. "free -m" will give the information using megabytes, which is
probably most useful for current computers.
• top: The top command displays information on your Linux system,
running processes and system resources, including CPU, RAM & swap usage and
total number of tasks being run. To exit top, press "q".
• uname -a: The uname command with the -a option prints all system
information, including machine name, kernel name & version, and a few other
details. Most useful for checking which kernel you're using.
• lsb_release -a: The lsb_release command with the -a option prints
version information for the Linux release you're running, for example:

user@computer:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 6.06 LTS
Release: 6.06
Codename: dapper

• ifconfig reports on your system's network interfaces.


Adding A New User
• "adduser newuser" command will create a new general user called
"newuser" on your system, and to assign a password for the newuser account
use "passwd newuser".

sudo: Executing Commands with Elevated Privileges


• Most of the following commands will need to be prefaced with
the sudo command if you will be working with directories or files not owned by
your account. Please see RootSudofor information on using sudo.

File & Directory Commands


• pwd: The pwd command will allow you to know in which directory you're
located ( pwd stands for "print working directory"). Example: "pwd" in the
Desktop directory will show "~/Desktop". Note that the Gnome Terminal also
displays this information in the title bar of it's window - see the example
screenshot at the top of this page.
• ls: The ls command will show you the files in your current directory. Used
with certain options, you can see sizes of files, when files where made, and
permissions of files. Example: "ls ~" will show you the files that are in your
home directory.
o cd: The cd command will allow you to change directories. When
you open a terminal you will be in your home directory. To move around the
file system you will use cd. Examples:
 To navigate into the root directory, use "cd /"
 To navigate to your home directory, use "cd" or "cd ~"
 To navigate up one directory level, use "cd .."
 To navigate to the previous directory (or back), use "cd -"
 To navigate through multiple levels of directory at once,
specify the full directory path that you want to go to. For example, use, "cd
/var/www" to go directly to the /www subdirectory of /var/. As another
example, "cd ~/Desktop" will move you to the Desktop subdirectory inside
your home directory.
• cp: The cp command will make a copy of a file for you. Example: "cp file
foo" will make a exact copy of "file" and name it "foo", but the file "file" will still
be there. When you use mv that file would no longer exist, but when you
use cp the original file stays and a new copy is made.
• mv: The mv command will move a file to a different location or will
rename a file. Examples are as follows: "mv file foo" will rename the file "file"
to "foo". "mv foo ~/Desktop" will move the file "foo" to your Desktop
directory but will not rename it. You must specify a new file name to rename a
file.
o To save on typing, you can substitute '~' in place of the home
directory.
o Note that if you are using mv with sudo you will not be able to use
the ~ shortcut, but will have to use the full pathnames to your files. This is
because when you are working as root, ~ will refer to the root account's home
directory, not your own.
• rm: Use this command to remove or delete a file in your directory. It will
not work on directories which have files in them (use rmdir instead).
• mkdir: The mkdir command will allow you to create directories.
Example: "mkdir music" will create a music directory.
• man: The man command is used to show you the manual of other
commands. Try "man man" to get the man page for man itself. See the
" Man & Getting Help" section down the page for more information.

System Information Commands


• df: The df command displays filesystem disk space usage for all
partitions. " df -h" is probably the most useful - it uses megabytes (M) and
gigabytes (G) instead of blocks to report. ( -h means "human-readable")
• free: The free command displays the amount of free and used memory in
the system. "free -m" will give the information using megabytes, which is
probably most useful for current computers.
• top: The top command displays information on your Linux system,
running processes and system resources, including CPU, RAM & swap usage and
total number of tasks being run. To exit top, press "q".
• uname -a: The uname command with the -a option prints all system
information, including machine name, kernel name & version, and a few other
details. Most useful for checking which kernel you're using.
• lsb_release -a: The lsb_release command with the -a option prints
version information for the Linux release you're running, for example:

user@computer:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 6.06 LTS
Release: 6.06
Codename: dapper

• ifconfig reports on your system's network interfaces.

System Information Commands


• df: The df command displays filesystem disk space usage for all
partitions. " df -h" is probably the most useful - it uses megabytes (M) and
gigabytes (G) instead of blocks to report. ( -h means "human-readable")
• free: The free command displays the amount of free and used memory in
the system. "free -m" will give the information using megabytes, which is
probably most useful for current computers.
• top: The top command displays information on your Linux system,
running processes and system resources, including CPU, RAM & swap usage and
total number of tasks being run. To exit top, press "q".
• uname -a: The uname command with the -a option prints all system
information, including machine name, kernel name & version, and a few other
details. Most useful for checking which kernel you're using.
• lsb_release -a: The lsb_release command with the -a option prints
version information for the Linux release you're running, for example:

user@computer:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 6.06 LTS
Release: 6.06
Codename: dapper

• ifconfig reports on your system's network interfaces.

Useful Commands in Ubuntu


Posted by Nikesh Jauhari

A comprehensive list of commands needed when using Ubuntu:

Command privileges.

sudo command - run command as root


sudo su – root shell open
sudo su user – open shell as a user
sudo -k – forget your password sudo
gksudo command – sudo visual dialog (GNOME)
kdesudo command – sudo visual dialog (KDE)
sudo visudo – edit / etc / sudoers
gksudo nautilus – root file manager (GNOME)
kdesudo konqueror – root file manager (KDE)
passwd – change your password

Command Network

ifconfig – displays information network


iwconfig – displays information from wireless
sudo iwlist scan – scan wireless networks
sudo /etc/init.d/networking restart – reset the network
(file) /etc/network/interfaces – manual configuration
ifup interface – bring online interface
ifdown interface – disable interface

Commands Display

sudo /etc/init.d/gdm restart – reset X (Gnome)


sudo /etc/init.d/kdm restart – reset X (KDE)
(file) /etc/X11/xorg.conf – show Configuration
sudo dpkg-reconfigure - reconfigure xserver-xorg-phigh - reset configuration X
Ctrl+Alt+Bksp – X display reset if frozen
Ctrl+Alt+FN – switch to tty N
Ctrl+Alt+F7 – switch back to X display

Commands Service System.

start service – service to start work (Upstart)


stop service – service to stop working (Upstart)
status service – check if service is running (Upstart)
/etc/init.d/service start – start service (SysV)
/etc/init.d/service stop – stop service (SysV)
/etc/init.d/service status – check service (SysV)
/etc/init.d/service restart – reset service (SysV)
runlevel – get current runlevel

Commands for Firewall.

ufw enable – turn on the firewall


ufw disable – turn off the firewall
ufw default allow – allow all connections by default
ufw default deny – drop all connections by default
ufw status – current rules and
ufw allow port – to allow traffic on port
ufw deny port – port block
ufw deny from ip – ip block

Command System.

lsb_release -a – get the version of Ubuntu


uname -r – get kernel version
uname -a – get all the information kernel

Commands for Package Manager.

apt-get update – refresh updates available


apt-get upgrade – update all packages
apt-get dist-upgrade – version update
apt-get install pkg – installing pkg
apt-get remove pkg – uninstall pkg
apt-get autoremove – removing packages obsotletos
apt-get -f install – try to fix packages
dpkg –configure -a – try to fix a broken package
dpkg -i pkg.deb – install file pkg.deb
(file) /etc/apt/sources.list – list of repositories APT

Special Packages For commands.

ubuntu-desktop – Setting the standard Ubuntu


kubuntu-desktop – KDE Desktop
xubuntu-desktop – desktop XFCE
ubuntu-minimal – core earnings Ubuntu
ubuntu-standard – the standard utilities Ubuntu
ubuntu-restricted-extras – not free, but useful
kubuntu-restricted-extras – ditto KDE
xubuntu-restricted-extras – ditto XFCE
build-essential – packages used to compile
linux-image-generic – latest generic kernel image
linux-headers-generic – latest headlines

Applications commands.

nautilus – File Manager (GNOME)


dolphin – File Manager (KDE)
konqueror – Web browser (KDE)
kate – text editor (KDE)
gedit – text editor (GNOME)
Command Description
# arch show architecture of machine(1) [man]
# cal 2007 show the timetable of 2007 [man]
# cat /proc/cpuinfo show information CPU info [man]
# cat /proc/interrupts show interrupts [man]
# cat /proc/meminfo verify memory use [man]
# cat /proc/swaps show file(s) swap [man]
# cat /proc/version show version of the kernel [man]
# cat /proc/net/dev show network adpters and statistics [man]
# cat /proc/mounts show mounted file system(s) [man]
# clock -w save date changes on BIOS [man]
# date show system date [man]
# date 041217002007.00 set date and time -
MonthDayhoursMinutesYear.Seconds [man]
# dmidecode -q show hardware system components -
(SMBIOS / DMI) [man]
# hdparm -i /dev/hda displays the characteristics of a hard-disk
[man]
# hdparm -tT /dev/sda perform test reading on a hard-disk [man]
# lspci -tv display PCI devices [man]
# lsusb -tv show USB devices [man]
# uname -m show architecture of machine(2) [man]
# uname -r show used kernel version [man]

Command Description
# init 0 shutdown system(2) [man]
# logout leaving session [man]
# reboot reboot(2) [man]
# shutdown -h now shutdown system(1) [man]
# shutdown -h 16:30 & planned shutdown of the system at 16:30
[man]
# shutdown -c cancel a planned shutdown of the system
[man]
# shutdown -r now reboot(1) [man]
# telinit 0 shutdown system(3) [man]
Command Description
# cd /home enter to directory '/ home' [man]
# cd .. go back one level [man]
# cd ../.. go back two levels [man]
# cd go to home directory [man]
# cd ~user1 go to home directory [man]
# cd - go to previous directory [man]
# cp file1 file2 copying a file [man]
# cp dir/* . copy all files of a directory within the current
work directory [man]
# cp -a /tmp/dir1 . copy a directory within the current work
directory [man]
# cp -a dir1 dir2 copy a directory [man]
# cp file file1 outputs the mime type of the file as text
[man]
# iconv -l lists known encodings [man]
# iconv -f fromEncoding -t toEncoding converting the coding of characters from one
inputFile > outputFile format to another [man]
# find . -maxdepth 1 -name *.jpg -print batch resize files in the current directory and
-exec convert send them to a thumbnails directory (requires
convert from Imagemagick) [man]
# ln -s file1 lnk1 create a symbolic link to file or directory
[man]
# ln file1 lnk1 create a physical link to file or directory [man]
# ls view files of directory [man]
# ls -F view files of directory [man]
# ls -l show details of files and directory [man]
# ls -a show hidden files [man]
# ls *[0-9]* show files and directory containing numbers
[man]
# lstree show files and directories in a tree starting
from root(2) [man]
# mkdir dir1 create a directory called 'dir1' [man]
# mkdir dir1 dir2 create two directories simultaneously [man]
# mkdir -p /tmp/dir1/dir2 create a directory tree [man]
# mv dir1 new_dir rename / move a file or directory [man]
# pwd show the path of work directory [man]
# rm -f file1 delete file called 'file1' [man]
# rm -rf dir1 remove a directory called 'dir1' and contents
recursively [man]
# rm -rf dir1 dir2 remove two directories and their contents
recursively [man]
# rmdir dir1 delete directory called 'dir1' [man]
# touch -t 0712250000 file1 modify timestamp of a file or directory -
(YYMMDDhhmm) [man]
# tree show files and directories in a tree starting
from root(1) [man]

Command Description
# find / -name file1 search file and directory into root filesystem
from '/' [man]
# find / -user user1 search files and directories belonging to 'user1'
[man]
# find /home/user1 -name \*.bin search files with '. bin' extension within
directory '/ home/user1' [man]
# find /usr/bin -type f -atime +100 search binary files are not used in the last 100
days [man]
# find /usr/bin -type f -mtime -10 search files created or changed within 10 days
[man]
# find / -name *.rpm -exec chmod 755 search files with '.rpm' extension and modify
'{}' \; permits [man]
# find / -xdev -name \*.rpm search files with '.rpm' extension ignoring
removable partitions as cdrom, pen-drive, etc.
… [man]
# locate \*.ps find files with the '.ps' extension - first run
'updatedb' command [man]
# whereis halt show location of a binary file, source or man
[man]
# which halt show full path to a binary / executable [man]
Command Description
# fuser -km /mnt/hda2 force umount when the device is busy [man]
# mount /dev/hda2 /mnt/hda2 mount disk called hda2 - verify existence of the
directory '/ mnt/hda2' [man]
# mount /dev/fd0 /mnt/floppy mount a floppy disk [man]
# mount /dev/cdrom /mnt/cdrom mount a cdrom / dvdrom [man]
# mount /dev/hdc /mnt/cdrecorder mount a cdrw / dvdrom [man]
# mount /dev/hdb /mnt/cdrecorder mount a cdrw / dvdrom [man]
# mount -o loop file.iso /mnt/cdrom mount a file or iso image [man]
# mount -t vfat /dev/hda5 /mnt/hda5 mount a Windows FAT32 file system [man]
# mount /dev/sda1 /mnt/usbdisk mount a usb pen-drive or flash-drive [man]
# mount -t smbfs -o mount a windows network share [man]
username=user,password=pass
//WinClient/share /mnt/share
# umount /dev/hda2 unmount disk called hda2 - exit from mount
point '/ mnt/hda2' first [man]
# umount -n /mnt/hda2 run umount without writing the file /etc/mtab -
useful when the file is read-only or the hard
disk is full [man]

Command Description
# df -h show list of partitions mounted [man]
# dpkg-query -W -f='${Installed-Size;10}t$ show the used space by installed deb packages,
{Package}n' | sort -k1,1n sorting by size (debian, ubuntu and alike)
[man]
# du -sh dir1 estimate space used by directory 'dir1' [man]
# du -sk * | sort -rn show size of the files and directories sorted by
size [man]
# ls -lSr |more show size of the files and directories ordered by
size [man]
# rpm -q -a --qf '%10{SIZE}t%{NAME}n' | show the used space by rpm packages installed
sort -k1,1n sorted by size (fedora, redhat and alike) [man]

Command Description
# chage -E 2005-12-31 user1 set deadline for user password [man]
# groupadd [group] create a new group [man]
# groupdel [group] delete a group [man]
# groupmod -n moon sun rename a group from moon to sun [man]
# grpck check correct syntax and file format of
'/etc/group' and groups existence [man]
# newgrp - [group] log into a new group to change default group of
newly created files [man]
# passwd change password [man]
# passwd user1 change a user password (only by root) [man]
# pwck check correct syntax and file format of
'/etc/passwd' and users existence [man]
# useradd -c "User Linux" -g admin -d create a new user "user1" belongs "admin" group
/home/user1 -s /bin/bash user1 [man]
# useradd user1 create a new user [man]
# userdel -r user1 delete a user ( '-r' eliminates home directory)
[man]
# usermod -c "User FTP" -g system -d change user attributes as description, group and
/ftp/user1 -s /bin/nologin user1 other [man]

You might also like