Vous êtes sur la page 1sur 21

Syntax

http://www.tutorialspoint.com/unix/index.htm
Linux Keyboard Shortcuts
1. Useful Command Line Keyboard Shortcuts
The following keyboard shortcuts are incredibly useful and will save you loads of time:

ALT + F - Jump forward to next space


ALT + B - Skip back to previous space
ALT + Backspace - Delete previous word
Shift + Insert - Pastes text into terminal

Ctrl + A Return to the start of the command you're typing


Ctrl + E Go to the end of the command you're typing
Ctrl + U Cut everything before the cursor to a special clipboard, erases the whole line
Ctrl + K Cut everything after the cursor to a special clipboard
Ctrl + Y Paste from the special clipboard that Ctrl + U and Ctrl + K save their data to
Ctrl + T Swap the two characters before the cursor (use this to transport a character from the left to the right!)
Ctrl + W Delete the word / argument left of the cursor in the current line

Ctrl + D Log out of current session, similar to exit

Just so that the commands above make sense, look at the next line of text.
sudo apt-get install programname
if I have a spelling error and for the command to work I would need to change "intall" to "install".
Imagine the cursor is at the end of the line. There are various ways to get back to the word install to change it.
I could press ALT + B twice which would put the cursor in the following position (denoted by the ^ symbol):
sudo apt-get^intall programname
Now you could press the cursor key and insert the ''s' into install.
Another useful command is "shift + insert" especially If you need to copy text from a browser into the terminal.

2. SUDO

sudo
Solution to the words "permission denied" appear. How do you use sudo? Simply imagine you have entered the following command:
apt-get install ranger
The words "Permission denied" will appear unless you are logged in with elevated privileges.
sudo runs the previous command as sudo. So the previous command now becomes:
sudo apt-get install ranger
If you don't know what sudo is start here.

3. Pausing Commands and Running Commands in The Background


I have already written a guide showing how to run terminal commands in the background.

CTRL + Z - Pauses an application


fg - Returns you to the application

So what is this tip about? Imagine you have opened a file in nano as follows:
sudo nano abc.txt
Halfway through typing text into the file you realize that you want to quickly type another command into the terminal but you can't because nano
is opened in foreground mode.
You may think your only option is to save the file, exit nano, run the command and then re-open nano.
All you have to do is press CTRL + Z to pause the foreground application and be returned to the command line. You can then run any command
you like. To return to the paused session entering "fg" @ terminal + return.
An interesting thing to try out is to open a file in nano, enter some text and pause the session. Now open another file in nano, enter some text and
pause the session. If you now enter "fg" you return to the second file you opened in nano. If you exit nano and enter "fg" again you return to the
first file you opened within nano.

4. Use nohup To Run Commands after SSH Session Log Out


The nohup command is really useful if you use the ssh command to log onto other machines. So what does nohup do? Imagine you are logged on
to another computer remotely using ssh and you want to run a command that takes a long time and then exit the ssh session but leave the
command running even though you are no longer connected then nohup lets you do just that.
For instance I use my Raspberry PI to download distributions for review purposes. I never have my Raspberry PI connected to a display nor do I
have a keyboard and mouse connected to it. I always connect to the Raspberry PI via ssh from a laptop. If I started downloading a large file on
the Raspberry PI without using the nohup command then I would have to wait for the download to finish before logging off the ssh session and
before shutting down the laptop. If I did this then I may as well have not used the Raspberry PI to download the file at all. To use nohup all I
have to do is type the following command:
nohup wget http://mirror.is.co.za/mirrors/linuxmint.com/iso//stable/17.1/linuxmint-17.1-cinnamon64bit.iso &

Schedule tasks with at.


5. Running A Linux Command 'at' A Specific Time
The 'nohup' command is good if you are connected to an SSH server and you want the command to remain running after logging out of the SSH
session. Imagine you want to run that same command at a specific time.
The 'at' command allows you to do just that. 'at' can be used as follows.
at 10:38 PM Fri
at> cowsay 'hello'
at> CTRL + D
The above command will run the program cowsay at 10:38 PM on Friday night.
The syntax is 'at' followed by the date and time to run.
When the at> prompt appears enter the command you want to run at the specified time.
The CTRL + D returns you to the cursor.

Many date and time formats check the man pages for more ways to use 'at'.

Colourful MAN pages.


6. Man Pages
Man pages give you an outline of what commands are supposed to do and the switches that can be used with them.
The man pages are kind of dull on their own. However, you can make man usage more appealing.
export PAGER=most
You will need to install 'most; for this to work but when you do it makes your man pages more colorful.
You can limit the width of the man page to a certain number of columns using the following command:
export MANWIDTH=80
Finally, if available you can open any man page in the default browser by using the -H switch as follows:
man -H <command>
NB this works only if you have a default browser set up within the $BROWSER environment variable.

View Processes With htop.


7. Use htop To View and Manage Processes
Which command do you currently use to find out which processes are running on your computer? My bet is that you are using 'ps' and that you
are using various switches to get the output you desire. Install 'htop'. It is definitely a tool you will wish that you installed earlier. htop provides a
list of all running processes in the terminal much like the file manager in Windows. You can use a mixture of function keys to change the sort
order and the columns that are displayed. You can also kill processes from within htop. To run htop simply type the following into the terminal
window:
htop

Command Line File Manager - Ranger.


8. Navigate File System Using ranger
If htop is immensely useful for controlling the processes running via the command line then ranger is immensely useful for navigating the file
system using the command line. Install ranger to use it but once installed you can run it simply by typing the following into the terminal:
ranger

The command line window will be much like any other file manager but it works left to right rather than top to bottom meaning that if you use
the left arrow key you work your way up the folder structure and the right arrow key works down the folder structure. It is worth reading the man
pages before using ranger so that you can get used to all keyboard switches that are available.

Cancel Linux Shutdown.


9. Cancel Shutdown
So you started the shutdown either via the command line or from the GUI and that you really didn't want to do that.

shutdown -c

Note that if the shutdown has already started then it may be too late to stop the shutdown.
Another command to try is as follows:

pkill shutdown

Kill Hung Processes With XKill.


10. Killing Hung Processes the Easy Way
Imagine you are running an application and for whatever reason it freezes.
You could use 'ps -ef' to find the process and then kill the process or you could use 'htop'.
There is a quicker and easier command that you will love called xkill.
Simply type the following into a terminal and then click on the window of the application you want to kill.
xkill
What happens though if the whole system is hanging?
Hold down the 'alt' and 'sysrq' same as prtsc keys on your keyboard and whilst they are held down type the following slowly:
REISUB
This will restart your computer without having to hold in the power button

Linux Terminal Command Reference


System Info
date Show the current date and time
cal Show this month's calendar
uptime Show current uptime
w Display who is online
whoami Who you are logged in as
finger user Display information about user
uname -a Show kernel information
cat /proc/cpuinfo CPU information
cat /proc/meminfo Memory information
df -h Show disk usage
du Show directory space usage
free Show memory and swap usage
Keyboard Shortcuts
Enter Run the command
Up Arrow Show the previous command
Ctrl + R Allows you to type a part of the command you're looking for and finds it
Ctrl + Z Stops the current command, resume with fg in the foreground or bg in the background
Ctrl + C Halts the current command, cancel the current operation and/or start with a fresh new line
Ctrl + L Clear the screen
command | less Allows the scrolling of the bash command window using Shift + Up Arrow and Shift + Down Arrow
!! Repeats the last command

command !$ Repeats the last argument of the previous command


Esc + . (a period) Insert the last argument of the previous command on the fly, which enables you to edit it before executing the command

Learn the Commands


apropos subject List manual pages for subject
man -k keyword Display man pages containing keyword
man command Show the manual for command
man -t man | ps2pdf - > man.pdf Make a pdf of a manual page
which command Show full path name of command
time command See how long a command takes
whereis app Show possible locations of app
which app Show which app will be run by default; it shows the full path
Searching
grep pattern files Search for pattern in files
grep -r pattern dir Search recursively for pattern in dir
command | grep pattern Search for pattern in the output of command
locate file Find all instances of file
find / -name filename Starting with the root directory, look for the file called filename
find / -name *filename* Starting with the root directory, look for the file containing the string filename
locate filename Find a file called filename using the locate command; this assumes you have already used the command updatedb (see next)
updatedb Create or update the database of files on all file systems attached to the Linux root directory
which filename Show the subdirectory containing the executable file called filename
grep TextStringToFind /dir Starting with the directory called dir, look for and list all files containing TextStringToFind
File Permissions
chmod octal file Change the permissions of file to octal, which can be found separately for user, group, and world by adding: 4 read (r), 2
write (w), 1 execute (x)
Examples:
chmod 777 read, write, execute for all
chmod 755 rwx for owner, rx for group and world
For more options, see man chmod.
File Commands
ls Directory listing
ls -l List files in current directory using long format
ls -laC List all files in current directory in long format and display in columns
ls -F List files in current directory and indicate the file type
ls -al Formatted listing with hidden files
cd dir Change directory to dir
cd Change to home
mkdir dir Create a directory dir
pwd Show current directory
rm name Remove a file or directory called name
rm -r dir Delete directory dir
rm -f file Force remove file
rm -rf dir Force remove an entire directory dir and all its included files and subdirectories (use with extreme caution)
cp file1 file2 Copy file1 to file2
cp -r dir1 dir2 Copy dir1 to dir2; create dir2 if it doesn't exist
cp file /home/dirname Copy the filename called file to the /home/dirname directory
mv file /home/dirname Move the file called filename to the /home/dirname directory
mv file1 file2 Rename or move file1 to file2; if file2 is an existing directory, moves file1 into directory file2

ln -s file link Create symbolic link link to file


touch file Create or update file
cat > file Places standard input into file
cat file Display the file called file
more file Display the file called file one page at a time, proceed to next page using the spacebar
head file Output the first 10 lines of file
head -20 file Display the first 20 lines of the file called file
tail file Output the last 10 lines of file
tail -20 file Display the last 20 lines of the file called file
tail -f file Output the contents of file as it grows, starting with the last 10 lines
Compression
tar cf file.tar files Create a tar named file.tar containing files
tar xf file.tar Extract the files from file.tar
tar czf file.tar.gz files Create a tar with Gzip compression
tar xzf file.tar.gz Extract a tar using Gzip
tar cjf file.tar.bz2 Create a tar with Bzip2 compression
tar xjf file.tar.bz2 Extract a tar using Bzip2
gzip file Compresses file and renames it to file.gz
gzip -d file.gz Decompresses file.gz back to file
Printing
/etc/rc.d/init.d/lpd start Start the print daemon
/etc/rc.d/init.d/lpd stop Stop the print daemon
/etc/rc.d/init.d/lpd status Display status of the print daemon
lpq Display jobs in print queue
lprm Remove jobs from queue
lpr Print a file
lpc Printer control tool
man subject | lpr Print the manual page called subject as plain text
man -t subject | lpr Print the manual page called subject as Postscript output
printtool Start X printer setup interface
Network
ifconfig List IP addresses for all devices on the local machine
iwconfig Used to set the parameters of the network interface which are specific to the wireless operation (eg: the frequency)
iwlist used to display some additional information from a wireless network interface that is not displayed by iwconfig
ping host Ping host and output results
whois domain Get whois information for domain
dig domain Get DNS information for domain
dig -x host Reverse lookup host
wget file Download file
wget -c file Continue a stopped download
SSH
ssh user@host Connect to host as user
ssh -p port user@host Connect to host on port port as user
ssh-copy-id user@host Add your key to host for user to enable a keyed or passwordless login
User Administration
adduser acctname Create a new user call acctname
passwd accntname Give acctname a new password
su Log in as superuser from current login/switch user
exit Stop being superuser and revert to normal user

Process Management
ps Display your currently active processes
top Display all running processes
kill pid Kill process id pid
killall proc Kill all processes named proc (use with extreme caution)
bg Lists stopped or background jobs; resume a stopped job in the background
fg Brings the most recent job to foreground
fg n Brings job n to the foreground
Installation from source
./configure
make
make install
dpkg -i pkg.deb install a DEB package (Debian / Ubuntu / Linux Mint)
rpm -Uvh pkg.rpm install a RPM package (Red Hat / Fedora)
Stopping & Starting
shutdown -h now Shutdown the system now and do not reboot
halt Stop all processes - same as above
shutdown -r 5 Shutdown the system in 5 minutes and reboot
shutdown -r now Shutdown the system now and reboot
reboot Stop all processes and then reboot - same as above
startx Start the X system
%disk commands
blkid
fdisk
parted
lsblk
df
Recommended reading:
Cheat-Sheets.org All cheat sheets, round-ups, quick reference cards, quick reference guides and quick reference sheets in one page. The only
one you need.
Tutorial: The best tips & tricks for bash, explained Linux Tutorial Blog / Quality Linux tutorials without clutter
LinuxCommand.org Learning the shell, Writing shell scripts, Script library, SuperMan pages, Who, What, Where, Why
LinuxManPages.com General commands, System calls, Subroutines, Special files, File formats, Games, Macros and conventions, Maintenence
commands, Most Popular Man Pages
Linux Man Pages from die.net Man pages are grouped into sections, to see the full list of Linux man pages for a section, pick one. Or you can
browse Linux man pages by name; choose the first letter of the name of the Linux command, function, or file you are interested in.
Linux Newbie Guide: Shorcuts and Commands Linux essential shortcuts and sanity commands; Common Linux commands - system info;
Basic operations, network apps, file (de)compression; Process control; Basic administration commands, accessing drives/partitions; Network
administration tools, music-related commands, graphics-related commands.
Sudo Manual Pages Sudo (su "do") allows a system administrator to delegate authority to give certain users (or groups of users) the ability to
run some (or all) commands as root or another user while providing an audit trail of the commands and their arguments. For more information,
see the introduction to Sudo. Sudo is free software, distributed under an ISC-style license.

Navigation

Commands to help you navigate:

file: Determines file types

which: Locates an executable in your PATH

whereis: Locates binaries and man page

find: Find the file

grep: Search for text/string in the named file name

strings: Find text string in a binary file


The which command
It is useful to locate a command. Some operating systems such as Solaris/HP-UX (even linux) have multiple homes. So you wanna find out which
version you are going to use by order of the directories in your PATH variable. Try out following commands:
$ which ls
$ which vi
$ which vim
The file command
You would like to find out if a command is a shell script or a binary file or simply cannot recognize file by its extension then use file command to
determine file type.
$ file /usr/sbin/useradd
Output:
/usr/sbin/useradd: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.0,
dynamically linked (uses shared libs), stripped
Let us try another example:
# file /etc/shadow
Output:
/etc/shadow: ASCII text
But wait a second, you dont have to type full command path:
$ file $(which adduser)
Output:
/usr/sbin/adduser: perl script text executable
The whereis command
It locates binaries and man pages. When you get message command not found then use whereis command to locate binary file. For example
ifconfig command:
$ ifconfig
Output:
bash: ifconfig: command not found
Now to locate ifconfig binary, enter:
$ whereis -b ifconfig
Output:
ifconfig: /sbin/ifconfig
So let us try the full path, enter:
$ /sbin/ifconfig
The grep command
The grep command can search for text or strings such as IP address, domain names and lots of other stuff inside a text file. Often new Linux sys
admin forgets to configuration file names. However, you can use grep to find out those configuration file name. For example, find out the file
containing IP address 192.168.1.1
# grep -R "192.168.1.1" /etc/* | less

Find out kernel driver module bttv configuration file name, so that you can remove the driver:
# grep -R "bttv" /etc/* | less<
The strings Commands
The grep command is useful to search a text file, if you would like to find text string in a binary file then use strings command.
# strings /usr/bin/users
You might think this is stupid idea to search inside binary file for text string. Well, no it is not a stupid idea. For example, you would like to
quickly find out if internet service supports tcpd access control facility via /etc/hosts.allow and /etc/hosts.deny files (read as tcp wrappers) or not.
Let us find out if sshd server support tcpd or not:
# strings $(which sshd)| grep libwrap
libwrap.so.0
libwrap refuse returns
The find Command
Use find command to find the files. Find all files belonging to the user charvi:
# find / -user charvi
Remove all core dump files
# find / -name core -exec rm -i{}\;
Please see more find command examples here and here. For more info please read the man pages of find, grep, file, which.

In windows OS programs are installed using executable software. In a virtual environment this is not
possible, thus the need for package managers.
You can apply knowledge of Red Hat Enterprise Linux or Fedora to Ubuntu. The key differences are:
Administrative Tasks
By default In Red Hat Enterprise Linux and Fedora, each administrative user must know the root password, and their own password. In Ubuntu,
each user only has one password. Users - admin group can run command line and graphical applications with elevated privileges. Graphical
admin tools prompt for this password when run, and command line tools can be run with root-privileges using sudo.
Package Management
Ubuntu has more packages available than Fedora, so there is a better chance of finding repositories you want. As with Fedora graphical
applications link into the Applications menu.
Graphical Tools
The Synaptic package Manager is an excellent tool for finding, fetching and installing packages. Press System -> Administration -> Synaptic
Package Manager to start Synaptic.
Command Line Tools
Ubuntu uses apt-get instead of yum, or up2date to find, download, and install packages and their dependencies. Note that, unlike yum (uses rpm),
apt-get is only for packages available in repositories (therein sources.list) - it cannot handle packages you have already downloaded but the dpkg
command can.

Table of Equivalent Commands


For package management on Ubuntu/Debian and Red Hat/Fedora systems

Task

Red Hat/Fedora

Ubuntu/Debian

Adding, Removing and Upgrading Packages

Refresh list of available packages

yum refreshes after use

apt-get update

Install a package from a repository

yum install package_name

apt-get install package_name

Install a package file

yum install package.rpm


rpm -i package.rpm

dpkg --install package.deb

Remove a package

rpm -e package_name

apt-get remove package_name

Remove a package with configuration files

yum remove package_name

apt-get purge package_name

Check for package upgrades

yum check-update

apt-get -s upgrade
apt-get -s dist-upgrade

Upgrade packages

yum update
rpm -Uvh [args]

apt-get upgrade

Upgrade the entire system

yum upgrade

apt-get dist-upgrade

Package Information

Get information about an available package

yum search package_name

apt-cache search package_name

Show available packages

yum list available

apt-cache dumpavail

List all installed packages

yum list installed


rpm -qa

dpkg --list

Get information about a package

yum info package_name

apt-cache show package_name

Get information about an installed package

rpm -qi package_name

dpkg --status package_name

List files in an installed package

rpm -ql package_name

dpkg --listfiles package_name

List documentation files in an installed package

rpm -qd package_name

List configuration files in an installed package

rpm -qc package_name

dpkg-query --show -f '${Conffiles}\n'


package_name

Show the packages a given package depends on

rpm -qR package_name

apt-cache depends

Show other packages that depend on a


given package (reverse dependency)

rpm -q --whatrequires [args]

apt-cache rdepends

Package File Information

Get information about a package file

rpm -qpi package.rpm

dpkg --info package.deb

List files in a package file

rpm -qpl package.rpm

dpkg --contents package.deb

List documentation files in a package file

rpm -qpd package.rpm

List configuration files in a package file

rpm -qpc package.rpm

Extract files in a package

rpm2cpio package.rpm | cpio


-vid

dpkg-deb --extract package.deb dir-to-extract-to

Find package that installed a file

rpm -qf filename

dpkg --search filename

Find package that provides a particular file

yum provides filename

apt-file search filename

Misc. Packaging System Tools

Show stats about the package cache

apt-cache stats

Verify all installed packages

rpm -Va

debsums

Remove packages from the local cache directory

yum clean packages

apt-get clean

Remove only obsolete packages from the local cache


directory

apt-get autoclean

Remove header files from the local cache directory


(forcing a new download of same on next use)

yum clean headers

apt-file purge

General Packaging System Information

Package file extension

*.rpm

*.deb

Repository location configuration

/etc/yum.conf

/etc/apt/sources.list

Some of the information in this table was derived (with permission) from APT and RPM Packager Lookup Tables.
More technical information about Debian-style packaging can be found in Basics of the Debian package management system and the Debian
New Maintainers' Guide.
Services
Services on Ubuntu are managed in a broadly similar way to those on Red Hat.
Graphical Tools
Services can be configured by clicking System -> Administration -> Services. A tool called Boot-Up Manager is also available.
Command Line Tools
Below is a table of example commands for managing services. The apache/httpd service is used as an example.

Task

Immediate Start/stop services

Red Hat / Fedora

service httpd start

Ubuntu

invoke-rc.d apache start

Ubuntu
(with sysv-rc-conf or sysvconfig)

service apache start

Enabling a service at boot

chkconfig httpd on

update-rc.d apache defaults

sysv-rc-conf apache on

Disabling a service at boot

chkconfig httpd off

update-rc.d apache purge

sysv-rc-conf apache off

Default Boot servers

into runlevel 3

into runlevel 2

Note: The service and invoke-rc.d commands call init scripts to do the actual work. Also start/stop services by e.g.
Ubuntu: /etc/init.d/apache start
Red Hat/Fedora: /etc/init.d/httpd start

Differences between apt-get and yum


Switching between systems can be confusing. Here a list of apt-get commands and their yum equivalents that I hope will help someone out there.
Command for
Ubuntu/debian
Fedora/redhat
Install a package
apt-get install <package>
yum install <package>
Update a package
apt-get update <package>
yum update <package>
yum erase <package> or
Uninstall / remove a package
apt-get remove <package>
yum remove <package>
Reinstall a package
apt-get reinstall install <package>
yum reinstall <package>
Simulate installation of a package
apt-get -s install <package>
yum assumeno install <package>
Search for a package
apt-cache search <package>
yum search <package>
Update all installed packages
apt-get update && apt-get upgrade
yum upgrade
List all installed packages
dpkg l
yum list installed

A.

tar.gz also known as tarball, is an archive format for electronic data and software. Most Linux tarball contains a source code for
software. If you are new to Linux I recommend using apt-get, rpm and yum command to install all binary packages. Tarballs contain
group of files in one file. Tarball files have extensions- .tar.gz, .tgz or .tar.bz2. Most open source software use tarballs to distribute
programs/source codes.

1: Uncompress tarball
To uncompress them, execute the following command(s) depending on the extension:
$ tar zxf file.tar.gz
$ tar zxf file.tgz
$ tar jxf file.tar.bz2
$ tar jxf file.tbz2
Now change directory
$ ls
$ cd path-to-software/

2: Build and install software


Generally you need 3 commands for building and compiling software:
# ./configure; # make; # make install
where

./configure configures the software for your system to have functionality/libraries necessary to compile the
package
make compiles all the source files into executable binaries
make install installs the binaries/supporting files into the appropriate locations

3: Read INSTALL / README file


Each tarball has installation/build instructions. For more information:
Open INSTALL/README file
$ vi INSTALL

Since Kali Linux is based on Debian you cannot install RPM packages directly using apt or dpkg package managers. However, you can try a
utility called Alien which can convert one package format into other but this doesn't mean every RPM will work since packages link to another
packages for dependencies and this can cause troubles. To install Alien and necessary package, run this command
$ sudo apt-get install alien dpkg-dev debhelper build-essential
To convert a package from RPM to debian format, use this command:
$ sudo alien packagename.rpm
To install the package:
$ sudo dpkg -i packagename.deb
For standard-install routines for rpm and tar.gz files find out what the switches mean by executing the following
from the command line (CLI):
man rpm
rpm --help
man tar
tar help
Install a RPM package
rpm ivh packagename.rpm
Upgrade a RPM package
rpm Uvh packagename.rpm
Verify a RPM package was installed
1. rpm -qa | grep packagename
2. locate packagename
Create a tar file
tar cvf myfiles.tar mydir/

% unix format is tar

Create a tar file gzipped


Similar to above, but add a z for creating .tgz (.tar.gz) files)
tar cvzf myfiles.tgz mydir/
Standard install from source
A common way to install software from source will be done similarly to the below:
1. tar xvzf Apackage.tar.gz
2. cd Apackage
3. ./configure
4. make
5. make install
Benefits of Standard User in Kali:
Benefits as standard or non-root user in Kali
1.
2.
3.

Install and run Google Chrome


Install and run Gnome User and Groups manager (Install gnome-system-tools)
Use Kali as Primary Operating System without worrying about breaking it all the time.

Debian / Ubuntu
On Debian/Ubuntu systems, useradd is a command itself, and you can create users and define options to them using this command, and adduser
is a perl script that uses useradd to create the account, asking for password, full-name, phone etc.

ggarron@debian:~/tmp$ sudo adduser test


Adding user `test' ...
Adding new group `test' (1004) ...
Adding new user `test' (1003) with group `test' ...
Creating home directory `/home/test' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the default
Full Name []: gasf
Room Number []: asdg
Work Phone []: asdf
Home Phone []: asdf
Other []: asdf
%may leave the answers blank
Is the information correct? [Y/n] y
Add user in Kali Linux:

First confirm which version of Linux and Kernel is running:

uname a
lsb_release a

Now add a user/create new user


useradd -m nii
%note use lower caps

(Note: -m means create home directory which is usually /home/username)

Now set password for this user


passwd agroba
Enter desired password twice

Add user to sudo group (allows user to install software, print, use privileged mode etc.)
usermod -a -G sudo user1

(Note: -a means append or add and G mean to specified group/groups)

Change default shell of previously created user to bash(born-again shell)


chsh -s /bin/bash user1

(Note: chsh mean change login shell, -s is the specific shell you want for the user, in this case /bin/bash)
Logout and login back as our new Standard Non-root user (user1)
Login as new user

Log in, confirm from command line who you are:


whoami

Note user1@kali prompt also confirms who you are.

To check group affinity:


groups

I am part of user1 group (my primary) and sudo group. That means I can run privileged commands or just become root if required.

Become root!
sudo su

and type in user1s password to become root.

The prompt is now root@kali not user1@kali. Root get to run anything Kali offers.

Confirm new user by whoami command


whoami

Delete user in Kali Linux:

Log in as root user.

Open terminal and type:


userdel r <username= user1>
(Note: -r means delete all files and home directory for user1)

I have an error user1 is currently used by process 5866.


So process ID 5866 is being used by user1. (I know that its the gnomekeyring process running in background when I used sudo su
command earlier. (GnomeKeyring error is quite common in Debian when you install multiple Windows or Desktop Managers.
Theres a separate guide see How to fix Gnome-Keyring error. This demonstrates how to fix gnomekeyring error for root user). In
our case we are not worried as we just want to delete that user.

Type in the following in terminal to kill the process used by user1.


kill -9 5866
This kills the process immediately.

(Note: dont kill root or system processes unless you know what youre doing)

Now lets try to delete the user again.


userdel r user1

We have a message. userdel: user1 mail spool (/var/mail/user1) not found.


(Note: -r means delete all files and home directory for user1)
Are we going to worry about it? Not really, we never created a mailbox for user1.

Just to confirm everything for user1 was deleted list files in home directory
ls /home

Nothing.. thats good news, all files and folders were deleted for user1.

Want to double-check?
su user1

Perfect user1 was deleted successfully.

Howto: Linux Add User to Group


in BASH Shell, Linux, User Management
How can I add a user to a group under Linux operating system using command line options? How to add an existing
user into a group in Linux using command line options?
You can use the useradd or usermod commands to add a user to a group. The useradd command creates a new

user or update default new user information. The usermod command modifies a user account and it is useful to add
user to existing groups. There are two types of groups under Linux operating systems:
1. Primary user group.
2. Secondary or supplementary user group.
All user account related information is stored in the following files:
1.
2.

/etc/passwd Contains one line for each user account.


/etc/shadow Contains the password information in encrypted formatfor the systems accounts and
optional account aging information.
3. /etc/group Defines the groups on the system.
4. /etc/default/useradd This file contains a value for the default group, if none is specified by the useradd
command.
5. /etc/login.defs This file defines the site-specific configuration for the shadow password suite stored in
/etc/shadow file.
useradd Example Add a new user to secondary group
You need to the useradd command to add new users to existing group (or create a new group and then add user). If group does not exist, create it.
The syntax is as follows:
useradd -G {group-name} username
In this example, create a new user called vivek and add it to group called developers. First login as a root user (make sure group developers
exists):
# grep developers /etc/group
Output:
developers:x:1124:
If no output then you need to add group developers using the groupadd command:
# groupadd developers
Next, add a user called vivek to group developers: % user is lowercase
# useradd -G developers vivek
Setup user viveks password:
# passwd vivek
Ensure that user added properly to group developers:
# id vivek
Output:
uid=1122(vivek) gid=1125(vivek) groups=1125(vivek),1124(developers)
Please note that capital G (-G) option add user to a list of supplementary groups. Each group is separated from the next by a comma, with no
intervening whitespace. For example, add user jerry to groups admins, ftp, www, and developers, enter:
# useradd -G admins,ftp,www,developers jerry
useradd example Add a new user to primary group
To add a user tony to group developers use the following command:
# useradd -g developers tony
# id tony
Sample outputs:
uid=1123(tony) gid=1124(developers) groups=1124(developers)
Please note that small g (-g) option add user to initial login group (primary group). The group name must exist. A group number must refer to an
already existing group.

usermod example Add a existing user to existing group


Add existing user tony to ftp supplementary/secondary group with the usermod command using the -a option ~ i.e. add the user to the
supplemental group(s). Use only with -G option:
# usermod -a -G ftp tony
In this example, change tony users primary group to www, enter:
# usermod -g www tony
usermod command options
Option
-a
--append
-g GROUP
--gid GROUP
-G GRP1,GRP2
--groups GRP1,GRP2
A note about security

summary
Purpose
Add the user to the supplementary group(s). Use only with the -G option.
Use this GROUP as the default group.
Add the user to GRP1,GRP2 secondary group.

If you add or delete user to existing group, you must change the owner of any crontab files or at jobs manually. You must make any changes
involving NIS on the NIS server too.
A note about GUI tool
You will probably find the use of the GUI tool easy. KDE user can use KUser tool and the GNOME user can use users-admin tool called systemconfig-users:
# system-config-users
Sample outputs:

Fig.01: User Manager Tool in action (image credit Fedora project)


See also
For more information type the following command at the shell prompt:
$ man usermod
$ man useradd

Vous aimerez peut-être aussi